diff options
author | James Socol <me@jamessocol.com> | 2013-03-06 18:39:58 -0500 |
---|---|---|
committer | James Socol <me@jamessocol.com> | 2013-03-06 18:40:40 -0500 |
commit | 4d3a1964e166ba625842b5caece972d60ae27849 (patch) | |
tree | 79fd3f11e4bc9aa5d145433b0f094b7e6d0317d0 | |
parent | b479d61e0c4561fe941c9b9b5cef60355ed9e75b (diff) | |
download | pystatsd-4d3a1964e166ba625842b5caece972d60ae27849.tar.gz |
Limit packet payload to 512 bytes.
- Limits the size of pipeline packets to 512 bytes. Fixes #16.
- Adds a test to verify that pipelines are cleaned after send().
-rw-r--r-- | statsd/client.py | 14 | ||||
-rw-r--r-- | statsd/tests.py | 22 |
2 files changed, 33 insertions, 3 deletions
diff --git a/statsd/client.py b/statsd/client.py index b69ede8..cef1669 100644 --- a/statsd/client.py +++ b/statsd/client.py @@ -112,6 +112,14 @@ class Pipeline(StatsClient): self.send() def send(self): - data = '\n'.join(self._stats) - self._client._send(data) - self._stats = [] + # Use pop(0) to preserve the order of the stats. + data = self._stats.pop(0) + while self._stats: + stat = self._stats.pop(0) + if len(stat) + len(data) + 1 >= 512: + self._client._send(data) + data = stat + else: + data += '\n' + stat + if data: + self._client._send(data) diff --git a/statsd/tests.py b/statsd/tests.py index 32950e5..eeaf575 100644 --- a/statsd/tests.py +++ b/statsd/tests.py @@ -290,3 +290,25 @@ def test_pipeline_timer_decorator(): pass foo() _timer_check(sc, 1, 'foo', 'ms') + + +def test_pipeline_empty(): + """Pipelines should be empty after a send() call.""" + sc = _client() + with sc.pipeline() as pipe: + pipe.incr('foo') + eq_(1, len(pipe._stats)) + eq_(0, len(pipe._stats)) + + +def test_pipeline_packet_size(): + """Pipelines shouldn't send packets larger than 512 bytes.""" + sc = _client() + pipe = sc.pipeline() + for x in xrange(32): + # 32 * 16 = 512, so this will need 2 packets. + pipe.incr('sixteen_char_str') + pipe.send() + eq_(2, sc._sock.sendto.call_count) + assert len(sc._sock.sendto.call_args_list[0][0][0]) <= 512 + assert len(sc._sock.sendto.call_args_list[1][0][0]) <= 512 |