Skip to content
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

Reconnect to socket on exception #186

Open
wants to merge 1 commit into
base: main
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
6 changes: 5 additions & 1 deletion statsd/client/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ def _send(self, data):
"""Send data to statsd."""
if not self._sock:
self.connect()
self._do_send(data)
try:
self._do_send(data)
except (OSError, RuntimeError):
self.reconnect()
self._do_send(data)

def _do_send(self, data):
self._sock.sendall(data.encode('ascii') + b'\n')
Expand Down
7 changes: 4 additions & 3 deletions statsd/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1037,15 +1037,16 @@ def test_pipeline_packet_size():


@mock.patch.object(socket, 'socket')
def test_tcp_raises_exception_to_user(mock_socket):
def test_tcp_reconnect_on_exception(mock_socket):
"""Socket errors in TCPStatsClient should be raised to user."""
addr = ('127.0.0.1', 1234)
cl = _tcp_client(addr=addr[0], port=addr[1])
cl.reconnect = mock.Mock(wraps=cl.reconnect)
cl.incr('foo')
eq_(1, cl._sock.sendall.call_count)
cl._sock.sendall.side_effect = socket.error
with assert_raises(socket.error):
cl.incr('foo')
cl.incr('foo')
assert 1 == cl.reconnect.call_count


@mock.patch.object(socket, 'socket')
Expand Down