Skip to content

Commit 3bbf998

Browse files
committed
fix: improve service throttling mechanism to reject clients instead of blocking
The theory is that the connections are still backing up even while the service is sleeping. If we instead close connections agressively during the throttle period hopefully we'll be able to recover from overload periods more effectively
1 parent 0cfeeeb commit 3bbf998

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/DIRAC/Core/DISET/ServiceReactor.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ def __acceptIncomingConnection(self, svcName=False):
200200
services at the same time
201201
"""
202202
sel = self.__getListeningSelector(svcName)
203+
throttleExpires = None
203204
while self.__alive:
204205
clientTransport = None
205206
try:
@@ -223,12 +224,19 @@ def __acceptIncomingConnection(self, svcName=False):
223224
gLogger.warn(f"Client connected from banned ip {clientIP}")
224225
clientTransport.close()
225226
continue
227+
# Handle throttling
228+
if self.__services[svcName].wantsThrottle and throttleExpires is None:
229+
throttleExpires = time.time() + THROTTLE_SERVICE_SLEEP_SECONDS
230+
if throttleExpires:
231+
if time.time() > throttleExpires:
232+
throttleExpires = None
233+
else:
234+
gLogger.warn("Rejecting client due to throttling", str(clientTransport.getRemoteAddress()))
235+
clientTransport.close()
236+
continue
226237
# Handle connection
227238
self.__stats.connectionStablished()
228239
self.__services[svcName].handleConnection(clientTransport)
229-
while self.__services[svcName].wantsThrottle:
230-
gLogger.warn("Sleeping as service requested throttling", svcName)
231-
time.sleep(THROTTLE_SERVICE_SLEEP_SECONDS)
232240
# Renew context?
233241
now = time.time()
234242
renewed = False

0 commit comments

Comments
 (0)