##// END OF EJS Templates
deps: bumped idna==3.10
deps: bumped idna==3.10

File last commit:

r1130:d68a72e0 python3
r1336:b4c1c007 default
Show More
udp.py
53 lines | 1.5 KiB | text/x-python | PythonLexer
application: added statsd client for sending usage statistics.
r920 import socket
from .base import StatsClientBase, PipelineBase
class Pipeline(PipelineBase):
def __init__(self, client):
vcsserver: modernize code for python3
r1130 super().__init__(client)
application: added statsd client for sending usage statistics.
r920 self._maxudpsize = client._maxudpsize
def _send(self):
data = self._stats.popleft()
while self._stats:
# Use popleft to preserve the order of the stats.
stat = self._stats.popleft()
if len(stat) + len(data) + 1 >= self._maxudpsize:
self._client._after(data)
data = stat
else:
data += '\n' + stat
self._client._after(data)
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._sock = socket.socket(family, socket.SOCK_DGRAM)
self._prefix = prefix
self._maxudpsize = maxudpsize
def _send(self, data):
"""Send data to statsd."""
try:
self._sock.sendto(data.encode('ascii'), self._addr)
except (socket.error, RuntimeError):
# No time for love, Dr. Jones!
pass
def close(self):
if self._sock and hasattr(self._sock, 'close'):
self._sock.close()
self._sock = None
def pipeline(self):
return Pipeline(self)