// handle a new connection: // Steps: // 1. Read input from connection line by line. // 2. Parse each line into a metric. // 3. Validate the metric // 4. Process the metric. func (d *Detector) handle(conn net.Conn) { addr := conn.RemoteAddr() health.IncrNumClients(1) defer health.DecrNumClients(1) log.Infof("conn %s established", addr) scanner := bufio.NewScanner(conn) for scanner.Scan() { // Read line by line. if err := scanner.Err(); err != nil { // Close on read error. log.Errorf("read error: %v, closing conn..", err) break } line := scanner.Text() m, err := parseMetric(line) // Parse if err != nil { log.Errorf("parse error: %v, skipping..", err) continue } if err = m.Validate(); err != nil { log.Errorf("invalid metric: %v, skipping..", err) return } d.process(m, true) } conn.Close() log.Infof("conn %s disconnected", addr) }
// Handle a new connection, it will: // // 1. Read input from the connection line by line. // 2. Parse the lines into metrics. // 3. Validate the metrics. // func (d *Detector) handle(conn net.Conn) { // New conn established. addr := conn.RemoteAddr() health.IncrNumClients(1) log.Info("conn %s established", addr) // Read scanner := bufio.NewScanner(conn) for scanner.Scan() { // Read line by line. if err := scanner.Err(); err != nil { // Close conn on read error. log.Error("read error: %v, closing conn..", err) break } line := scanner.Text() // Parse metric. m, err := parseMetric(line) if err != nil { // Skip invalid input. log.Error("parse error: %v, skipping..", err) continue } // Validate metric. if err := validateMetric(m); err != nil { log.Error("invalid metric: %v, skipping..", err) continue } // Process d.process(m) } // Close conn. conn.Close() log.Info("conn %s disconnected", addr) health.DecrNumClients(1) }