Skip to content

statsd: thread safe Flush #5

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# statsd

Statsd client which adheres to [statsd/client-interface](https://github.com/statsd/client-interface). A fork of github.com/cyberdelia/statsd.
Statsd client which adheres to
[statsd/client-interface](https://github.com/statsd/client-interface). A fork of
github.com/cyberdelia/statsd.
14 changes: 11 additions & 3 deletions statsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package statsd
import (
"bufio"
"fmt"
. "github.com/visionmedia/go-debug"
"io"
"math/rand"
"net"
"sync"
"time"

. "github.com/visionmedia/go-debug"
)

var debug = Debug("statsd")
Expand Down Expand Up @@ -163,8 +164,15 @@ func (c *Client) Unique(name string, value int, rate float64) error {
return c.send(name, rate, "%d|s", value)
}

// Flush flushes writes any buffered data to the network.
// Flush flushes any buffered data to the network.
func (c *Client) Flush() error {
c.m.Lock()
defer c.m.Unlock()
return c.flush()
}

// flush() flushes data to the network. The caller is expected to hold c.m.
func (c *Client) flush() error {
return c.buf.Flush()
}

Expand Down Expand Up @@ -199,7 +207,7 @@ func (c *Client) send(stat string, rate float64, format string, args ...interfac

// Flush data if we have reach the buffer limit
if c.buf.Available() < len(format) {
if err := c.Flush(); err != nil {
if err := c.flush(); err != nil {
return nil
}
}
Expand Down