Skip to content

Commit

Permalink
Add periodic reconnect to StatsdClient
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan McCall committed Mar 26, 2018
1 parent 39c3fe5 commit ea1eef8
Showing 1 changed file with 42 additions and 10 deletions.
52 changes: 42 additions & 10 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,37 +40,68 @@ func init() {

// StatsdClient is a client library to send events to StatsD
type StatsdClient struct {
conn net.Conn
addr string
prefix string
eventStringTpl string
Logger Logger
conn net.Conn
addr string
prefix string
eventStringTpl string
Logger Logger
conn_type string
reconnect_ticker *time.Ticker
}

// NewStatsdClient - Factory
func NewStatsdClient(addr string, prefix string) *StatsdClient {
// allow %HOST% in the prefix string
prefix = strings.Replace(prefix, "%HOST%", Hostname, 1)
return &StatsdClient{
addr: addr,
prefix: prefix,
Logger: log.New(os.Stdout, "[StatsdClient] ", log.Ldate|log.Ltime),
eventStringTpl: "%s%s:%s",
client := &StatsdClient{
addr: addr,
prefix: prefix,
Logger: log.New(os.Stdout, "[StatsdClient] ", log.Ldate|log.Ltime),
eventStringTpl: "%s%s:%s",
reconnect_ticker: time.NewTicker(30 * time.Second),
}

go func() {
for range client.reconnect_ticker.C {
err := client.Reconnect()
if err != nil {
fmt.Println(err)
}
}
}()
return client
}

// String returns the StatsD server address
func (c *StatsdClient) String() string {
return c.addr
}

func (c *StatsdClient) Reconnect() error {
var err error
if c.conn_type == "udp" {
fmt.Println("creating new udp")
err = c.CreateSocket()
} else if c.conn_type == "tcp" {
fmt.Println("creating new tcp")
err = c.CreateTCPSocket()
} else if c.conn_type == "" {
return fmt.Errorf("No socket created, cannot identify connection type")
}
if err != nil {
return err
}
return nil
}

// CreateSocket creates a UDP connection to a StatsD server
func (c *StatsdClient) CreateSocket() error {
conn, err := net.DialTimeout("udp", c.addr, 5*time.Second)
if err != nil {
return err
}
c.conn = conn
c.conn_type = "udp"
return nil
}

Expand All @@ -81,6 +112,7 @@ func (c *StatsdClient) CreateTCPSocket() error {
return err
}
c.conn = conn
c.conn_type = "tcp"
c.eventStringTpl = "%s%s:%s\n"
return nil
}
Expand Down

0 comments on commit ea1eef8

Please sign in to comment.