Skip to content

Commit

Permalink
Add random component to track query interval
Browse files Browse the repository at this point in the history
Add per-query jitter to the track query interval, to avoid repeatedly
sending queries to all clients connected at the same time, which in
turn is to avoid load spikes.

Signed-off-by: Mattias Rönnblom <[email protected]>
  • Loading branch information
m-ronnblom committed Apr 1, 2024
1 parent 56c284f commit 33c83e9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
11 changes: 10 additions & 1 deletion paf/sd.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import contextlib
import enum
import random
import time


Expand Down Expand Up @@ -289,6 +290,7 @@ def assure_not_connected(fun):


WARNING_THRESHOLD = 0.5
WARNING_JITTER = 0.1
MIN_IDLE_TIME = 4


Expand All @@ -298,6 +300,11 @@ class IdleState(enum.Enum):
TIMED_OUT = enum.auto()


def jitter(base, max_jitter):
k = 1.0 + (random.random() - 0.5) * 2 * max_jitter
return k * base


class Connection:
def __init__(self, client, timer_manager, max_idle_time, idle_cb):
self.client = client
Expand Down Expand Up @@ -393,7 +400,9 @@ def idle_time(self):
return t

def install_idle_warning_timer(self):
self.install_idle_timer(WARNING_THRESHOLD * self.idle_time())
warning_time = jitter(WARNING_THRESHOLD * self.idle_time(),
WARNING_JITTER)
self.install_idle_timer(warning_time)

def install_idle_timeout_timer(self):
self.install_idle_timer((1 - WARNING_THRESHOLD) * self.idle_time())
Expand Down
2 changes: 1 addition & 1 deletion test/test_paf.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ def test_server_tracking_client(impatient_server):
wait(conn, criteria=lambda: track_recorder.count_notifications() > 0)

latency = time.time() - start
assert latency > query_time
assert latency > (query_time - 0.5)
assert latency < (query_time + 0.5)

notifications = track_recorder.get_notifications()
Expand Down

0 comments on commit 33c83e9

Please sign in to comment.