Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions statsd/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,27 @@ import (

// udpWriter is an internal class wrapping around management of UDP connection
type udpWriter struct {
conn net.Conn
conn net.PacketConn
addr string
}

// New returns a pointer to a new udpWriter given an addr in the format "hostname:port".
func newUDPWriter(addr string, _ time.Duration) (*udpWriter, error) {
udpAddr, err := net.ResolveUDPAddr("udp", addr)
conn, err := net.ListenPacket("udp", ":0")
if err != nil {
return nil, err
}
conn, err := net.DialUDP("udp", nil, udpAddr)
if err != nil {
return nil, err
}
writer := &udpWriter{conn: conn}
writer := &udpWriter{conn: conn, addr: addr}
return writer, nil
}

// Write data to the UDP connection with no error handling
func (w *udpWriter) Write(data []byte) (int, error) {
return w.conn.Write(data)
dst, err := net.ResolveUDPAddr("udp", w.addr)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an expensive operation that you do not want to do before every packet write. If you want to do such a change, it needs to be done infrequently to avoid substantially increasing the overhead of the datadog statsd client.

if err != nil {
return 0, err
}
return w.conn.WriteTo(data, dst)
}

func (w *udpWriter) Close() error {
Expand Down