Skip to content

Commit

Permalink
fix: subtract time spent leasing from max snooze value
Browse files Browse the repository at this point in the history
  • Loading branch information
acocuzzo committed Oct 28, 2022
1 parent c52aa05 commit 01f7ff4
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions google/cloud/pubsub_v1/subscriber/_protocol/leaser.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ def maintain_leases(self) -> None:
repeats.
"""
while not self._stop_event.is_set():
start_time = time.time()
# Determine the appropriate duration for the lease. This is
# based off of how long previous messages have taken to ack, with
# a sensible default and within the ranges allowed by Pub/Sub.
Expand Down Expand Up @@ -204,11 +205,14 @@ def maintain_leases(self) -> None:
# We determine the appropriate period of time based on a random
# period between:
# minimum: MAX_BATCH_LATENCY (to prevent duplicate modacks being created in one batch)
# maximum: 90% of the deadline
# maximum: 90% of the deadline,
# minus the time spent since the start of this while loop.
# This maximum time attempts to prevent ack expiration before new lease modacks arrive at the server.
# This use of jitter (http://bit.ly/2s2ekL7) helps decrease contention in cases
# where there are many clients.
snooze = random.uniform(_MAX_BATCH_LATENCY, deadline * 0.9)
snooze = random.uniform(
_MAX_BATCH_LATENCY, (deadline * 0.9) - (start_time - time.time())
)
_LOGGER.debug("Snoozing lease management for %f seconds.", snooze)
self._stop_event.wait(timeout=snooze)

Expand Down

0 comments on commit 01f7ff4

Please sign in to comment.