class LogShipper(threading.Thread): """ This is a thread that exists solely so that we can batch logs and ship them to the SenderThread every FLUSH_INTERVAL seconds. """ def __init__( self, ship_queue: queue.Queue, master_url: str, ) -> None: self.ship_queue = ship_queue self.logs = [] self.master_url = master_url super().__init__() def run(self) -> None: while True: deadline = time.time() + SHIPPER_FLUSH_INTERVAL for m in pop_until_deadline(self.ship_queue, deadline): if isinstance(m, ShutdownMessage): self.ship() return self.logs.append(m) if len(self.logs) >= LOG_BATCH_MAX_SIZE: self.ship() # Timeout met. self.ship() @backoff.on_exception( # type: ignore lambda: backoff.full_jitter(SHIPPER_FAILURE_BACKOFF_SECONDS), errors.APIException, max_tries=3, ) def ship(self) -> None: if len(self.logs) <= 0: return api.post(self.master_url, "task-logs", self.logs) self.logs = []
def pct_jitter(value, multiplier): jitter = backoff.full_jitter(value * multiplier) return value + jitter
def test_full_jitter(): for input in range(100): for i in range(100): jitter = backoff.full_jitter(input) assert jitter >= 0 assert jitter <= input