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

Make use of defer #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
9 changes: 4 additions & 5 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,12 @@ func newConn(conf connConfig, muted bool) (*conn, error) {
ticker := time.NewTicker(c.flushPeriod)
for _ = range ticker.C {
c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
ticker.Stop()
c.mu.Unlock()
return
}
c.flush(0)
c.mu.Unlock()
}
}()
}
Expand All @@ -83,18 +82,19 @@ func newConn(conf connConfig, muted bool) (*conn, error) {

func (c *conn) metric(prefix, bucket string, n interface{}, typ string, rate float32, tags string) {
c.mu.Lock()
defer c.mu.Unlock()
l := len(c.buf)
c.appendBucket(prefix, bucket, tags)
c.appendNumber(n)
c.appendType(typ)
c.appendRate(rate)
c.closeMetric(tags)
c.flushIfBufferFull(l)
c.mu.Unlock()
}

func (c *conn) gauge(prefix, bucket string, value interface{}, tags string) {
c.mu.Lock()
defer c.mu.Unlock()
l := len(c.buf)
// To set a gauge to a negative value we must first set it to 0.
// https://github.com/etsy/statsd/blob/master/docs/metric_types.md#gauges
Expand All @@ -105,7 +105,6 @@ func (c *conn) gauge(prefix, bucket string, value interface{}, tags string) {
c.appendBucket(prefix, bucket, tags)
c.appendGauge(value, tags)
c.flushIfBufferFull(l)
c.mu.Unlock()
}

func (c *conn) appendGauge(value interface{}, tags string) {
Expand All @@ -116,13 +115,13 @@ func (c *conn) appendGauge(value interface{}, tags string) {

func (c *conn) unique(prefix, bucket string, value string, tags string) {
c.mu.Lock()
defer c.mu.Unlock()
l := len(c.buf)
c.appendBucket(prefix, bucket, tags)
c.appendString(value)
c.appendType("s")
c.closeMetric(tags)
c.flushIfBufferFull(l)
c.mu.Unlock()
}

func (c *conn) appendByte(b byte) {
Expand Down