Last active
April 20, 2016 19:18
-
-
Save jvanasco/a5d013ecfaa4cd440501b4ec66dff914 to your computer and use it in GitHub Desktop.
statsd max connections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StatsClient(StatsClientBase): | |
"""A client for statsd.""" | |
def __init__(self, host='localhost', port=8125, prefix=None, | |
maxudpsize=512, ipv6=False): | |
"""Create a new client.""" | |
fam = socket.AF_INET6 if ipv6 else socket.AF_INET | |
family, _, _, _, addr = socket.getaddrinfo( | |
host, port, fam, socket.SOCK_DGRAM)[0] | |
self._addr = addr | |
self._prefix = prefix | |
self._maxudpsize = maxudpsize | |
self.family = family | |
self._connect() | |
def _connect(self): | |
self._sock = socket.socket(self.family, socket.SOCK_DGRAM) | |
self.transmissions = 0 | |
def _send(self, data): | |
"""Send data to statsd.""" | |
try: | |
if self.transmissions > 1000: | |
self._connect() | |
self.transmissions += 1 | |
self._sock.sendto(data.encode('ascii'), self._addr) | |
except (socket.error, RuntimeError): | |
# No time for love, Dr. Jones! | |
pass | |
def pipeline(self): | |
return Pipeline(self) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment