Skip to content

Commit 3a5b70c

Browse files
committed
global timeout
1 parent 50cbda9 commit 3a5b70c

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

sentry_sdk/_span_batcher.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import random
22
import threading
3+
import time
34
from collections import defaultdict
45
from datetime import datetime, timezone
56
from typing import TYPE_CHECKING
@@ -24,6 +25,7 @@ class SpanBatcher(Batcher["StreamedSpan"]):
2425
MAX_BEFORE_FLUSH = 1000
2526
MAX_BEFORE_DROP = 2000
2627
MAX_BYTES_BEFORE_FLUSH = 5 * 1024 * 1024 # 5 MB
28+
2729
FLUSH_WAIT_TIME = 5.0
2830

2931
TYPE = "span"
@@ -47,6 +49,7 @@ def __init__(
4749
self._lock = threading.Lock()
4850
self._active: "threading.local" = threading.local()
4951

52+
self._last_full_flush: float = time.monotonic()
5053
self._flush_queue: Queue = Queue()
5154

5255
self._flusher: "Optional[threading.Thread]" = None
@@ -55,13 +58,19 @@ def __init__(
5558
def _flush_loop(self) -> None:
5659
self._active.flag = True
5760
while self._running:
61+
jitter = random.random()
5862
try:
59-
trace_id = self._flush_queue.get(
60-
timeout=self.FLUSH_WAIT_TIME + random.random()
61-
)
63+
trace_id = self._flush_queue.get(timeout=self.FLUSH_WAIT_TIME + jitter)
6264
self._flush(trace_id=trace_id)
6365
except EmptyError:
66+
pass
67+
68+
if (
69+
time.monotonic() - self._last_full_flush
70+
>= self.FLUSH_WAIT_TIME + jitter
71+
):
6472
self._flush()
73+
self._last_full_flush = time.monotonic()
6574

6675
def add(self, span: "StreamedSpan") -> None:
6776
# Bail out if the current thread is already executing batcher code.

0 commit comments

Comments
 (0)