Commit 1d3ac345 authored by Kamil Kisiel's avatar Kamil Kisiel

Merge pull request #2 from RangelReale/protocol-fix

* Allows lines not ending in \n, as it is not required by the protocol
parents e770009a 802c36d5
...@@ -21,6 +21,14 @@ type MetricSender interface { ...@@ -21,6 +21,14 @@ type MetricSender interface {
SendMetrics(MetricMap) error SendMetrics(MetricMap) error
} }
// The MetricSenderFunc type is an adapter to allow the use of ordinary functions as metric senders
type MetricSenderFunc func(MetricMap) error
// SendMetrics calls f(m)
func (f MetricSenderFunc) SendMetrics(m MetricMap) error {
return f(m)
}
// MetricAggregator is an object that aggregates statsd metrics. // MetricAggregator is an object that aggregates statsd metrics.
// The function NewMetricAggregator should be used to create the objects. // The function NewMetricAggregator should be used to create the objects.
// //
......
...@@ -69,25 +69,33 @@ func (r *MetricReceiver) Receive(c net.PacketConn) error { ...@@ -69,25 +69,33 @@ func (r *MetricReceiver) Receive(c net.PacketConn) error {
func (srv *MetricReceiver) handleMessage(addr net.Addr, msg []byte) { func (srv *MetricReceiver) handleMessage(addr net.Addr, msg []byte) {
buf := bytes.NewBuffer(msg) buf := bytes.NewBuffer(msg)
for { for {
line, err := buf.ReadBytes('\n') line, readerr := buf.ReadBytes('\n')
if err == io.EOF {
break // protocol does not require line to end in \n, if EOF use received line if valid
} if readerr != nil && readerr != io.EOF {
if err != nil { log.Printf("error reading message from %s: %s", addr, readerr)
log.Printf("error reading message from %s: %s", addr, err)
return return
} else if readerr != io.EOF {
// remove newline, only if not EOF
if len(line) > 0 {
line = line[:len(line)-1]
}
} }
lineLength := len(line)
// Only process lines with more than one character // Only process lines with more than one character
if lineLength > 1 { if len(line) > 1 {
metric, err := parseLine(line[:lineLength-1]) metric, err := parseLine(line)
if err != nil { if err != nil {
log.Println("error parsing line %q from %s: %s", line, addr, err) log.Printf("error parsing line %q from %s: %s", line, addr, err)
continue continue
} }
go srv.Handler.HandleMetric(metric) go srv.Handler.HandleMetric(metric)
} }
if readerr == io.EOF {
// if was EOF, finished handling
return
}
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment