Skip to content

Commit 0a93a18

Browse files
committed
Use exponential backoff for connection retries
Calls to socket.connect() are non-blocking, hence all subsequent calls to socket.sendall() will fail if the target KDC service is temporarily or indefinitely unreachable. Since the kdcproxy task uses busy-looping, it results in the journal to be flooded with warning logs. This commit introduces a per-socket reactivation delay which increases exponentially as the number of reties is incremented, until timeout is reached (i.e. 100ms, 200ms, 400ms, 800ms, 1.6s, 3.2s, ...). Signed-off-by: Julien Rische <[email protected]>
1 parent f61979e commit 0a93a18

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

kdcproxy/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def __init__(self):
7474
def __await_reply(self, pr, rsocks, wsocks, timeout):
7575
extra = 0
7676
read_buffers = {}
77+
reactivations = {}
7778
while (timeout + extra) > time.time():
7879
if not wsocks and not rsocks:
7980
break
@@ -92,6 +93,9 @@ def __await_reply(self, pr, rsocks, wsocks, timeout):
9293
pass
9394

9495
for sock in w:
96+
(react_n, react_t) = reactivations.get(sock, (-1, 0.0))
97+
if react_t > time.time():
98+
continue
9599
try:
96100
if self.sock_type(sock) == socket.SOCK_DGRAM:
97101
# If we proxy over UDP, remove the 4-byte length
@@ -101,8 +105,12 @@ def __await_reply(self, pr, rsocks, wsocks, timeout):
101105
sock.sendall(pr.request)
102106
extra = 10 # New connections get 10 extra seconds
103107
except Exception as e:
104-
logging.warning("Conection broken while writing (%s)", e)
108+
logging.warning("Connection broken while writing (%s)", e)
109+
reactivations[sock] = (react_n + 1,
110+
time.time() + 2.0**(react_n + 1) / 10)
105111
continue
112+
if sock in reactivations:
113+
del reactivations[sock]
106114
rsocks.append(sock)
107115
wsocks.remove(sock)
108116

0 commit comments

Comments
 (0)