Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support connecting using TCP #2

Merged
merged 1 commit into from
Oct 11, 2019
Merged
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
42 changes: 32 additions & 10 deletions statsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ func millisecond(d time.Duration) int {
return int(d.Seconds() * 1000)
}

// Dial connects to the given address on the
// given network using net.Dial and then returns
// a new Client for the connection.
// Dial connects to the given address on the given network using net.Dial over
// UDP and then returns a new Client for the connection.
func Dial(addr string) (*Client, error) {
conn, err := net.Dial("udp", addr)
if err != nil {
return nil, err
}
return newClient(conn, 0), nil
return DialSize(addr, 0)
}

// DialTCP connects to the given address on the given network using net.Dial
// over TCP and then returns a new Client for the connection.
func DialTCP(addr string) (*Client, error) {
return DialTCPSize(addr, 0)
}

// NewClient returns a new client with the given writer,
Expand All @@ -47,7 +48,7 @@ func NewClient(w io.Writer) *Client {
}
}

// DialTimeout acts like Dial but takes a timeout. The timeout
// DialTimeout acts like Dial but takes a timeout over UDP. The timeout
// includes name resolution, if required.
func DialTimeout(addr string, timeout time.Duration) (*Client, error) {
conn, err := net.DialTimeout("udp", addr, timeout)
Expand All @@ -57,7 +58,17 @@ func DialTimeout(addr string, timeout time.Duration) (*Client, error) {
return newClient(conn, 0), nil
}

// DialSize acts like Dial but takes a packet size.
// DialTCPTimeout acts like Dial but takes a timeout over TCP. The timeout
// includes name resolution, if required.
func DialTCPTimeout(addr string, timeout time.Duration) (*Client, error) {
conn, err := net.DialTimeout("tcp", addr, timeout)
if err != nil {
return nil, err
}
return newClient(conn, 0), nil
}

// DialSize acts like Dial but takes a packet size over UDP.
// By default, the packet size is 512,
// see https://github.com/etsy/statsd/blob/master/docs/metric_types.md#multi-metric-packets for guidelines.
func DialSize(addr string, size int) (*Client, error) {
Expand All @@ -68,6 +79,17 @@ func DialSize(addr string, size int) (*Client, error) {
return newClient(conn, size), nil
}

// DialTCPSize acts like Dial but takes a packet size over TCP.
// By default, the packet size is 512,
// see https://github.com/etsy/statsd/blob/master/docs/metric_types.md#multi-metric-packets for guidelines.
func DialTCPSize(addr string, size int) (*Client, error) {
conn, err := net.Dial("tcp", addr)
if err != nil {
return nil, err
}
return newClient(conn, size), nil
}

// new client helper.
func newClient(conn net.Conn, size int) *Client {
if size <= 0 {
Expand Down