summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeremy Lainé <jeremy.laine@m4x.org>2020-04-13 16:50:56 +0200
committerJames Socol <me@jamessocol.com>2020-10-08 07:34:21 -0400
commite7a36f9d68c44476184261371276c76bf7f22ff8 (patch)
treee671566762b1bdce60a71c7db53d029d6b6f1cc1
parent1c90b9fdf322680e2625da659abc2aa5d79b5bff (diff)
downloadpystatsd-e7a36f9d68c44476184261371276c76bf7f22ff8.tar.gz
Add `close()` method to UDP client
Unlike its stream counterpart, the UDP client does not have a "close()" method. This means there is no public API to clean up the socket, resulting in a `ResourceWarning`.
-rw-r--r--docs/reference.rst4
-rw-r--r--statsd/client/base.py4
-rw-r--r--statsd/client/udp.py5
3 files changed, 13 insertions, 0 deletions
diff --git a/docs/reference.rst b/docs/reference.rst
index de9b054..df691d8 100644
--- a/docs/reference.rst
+++ b/docs/reference.rst
@@ -28,6 +28,10 @@ server supports.
generally considered safe for the public internet, but private networks
may support larger packet sizes.
+.. py:method:: StatsClient.close()
+
+ Close the underlying UDP socket.
+
.. py:method:: StatsClient.incr(stat, count=1, rate=1)
Increment a :ref:`counter <counter-type>`.
diff --git a/statsd/client/base.py b/statsd/client/base.py
index 08474c6..e273b41 100644
--- a/statsd/client/base.py
+++ b/statsd/client/base.py
@@ -10,6 +10,10 @@ from .timer import Timer
class StatsClientBase(object):
"""A Base class for various statsd clients."""
+ def close(self):
+ """Used to close and clean up any underlying resources."""
+ raise NotImplementedError()
+
def _send(self):
raise NotImplementedError()
diff --git a/statsd/client/udp.py b/statsd/client/udp.py
index 790c5e8..ddb9d22 100644
--- a/statsd/client/udp.py
+++ b/statsd/client/udp.py
@@ -46,5 +46,10 @@ class StatsClient(StatsClientBase):
# 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)