2016-02-12 17:06:49 +01:00
|
|
|
#
|
2023-11-21 21:29:58 +01:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
2024-01-23 12:26:48 +01:00
|
|
|
# Copyright 2016 OpenMarket Ltd
|
2023-11-21 21:29:58 +01:00
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2016-02-12 17:06:49 +01:00
|
|
|
#
|
|
|
|
#
|
2022-05-06 18:41:57 +02:00
|
|
|
import logging
|
|
|
|
from typing import Generic, Hashable, List, Set, TypeVar
|
2016-02-12 17:06:49 +01:00
|
|
|
|
2022-05-06 18:41:57 +02:00
|
|
|
import attr
|
2016-02-12 17:06:49 +01:00
|
|
|
|
2022-05-06 18:41:57 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
T = TypeVar("T", bound=Hashable)
|
2021-09-10 18:03:18 +02:00
|
|
|
|
2016-02-12 17:06:49 +01:00
|
|
|
|
2022-05-06 18:41:57 +02:00
|
|
|
@attr.s(slots=True, frozen=True, auto_attribs=True)
|
|
|
|
class _Entry(Generic[T]):
|
|
|
|
end_key: int
|
|
|
|
elements: Set[T] = attr.Factory(set)
|
2016-02-12 17:06:49 +01:00
|
|
|
|
|
|
|
|
2021-09-10 18:03:18 +02:00
|
|
|
class WheelTimer(Generic[T]):
|
2016-02-12 17:06:49 +01:00
|
|
|
"""Stores arbitrary objects that will be returned after their timers have
|
|
|
|
expired.
|
|
|
|
"""
|
|
|
|
|
2021-09-10 18:03:18 +02:00
|
|
|
def __init__(self, bucket_size: int = 5000) -> None:
|
2016-02-12 17:06:49 +01:00
|
|
|
"""
|
|
|
|
Args:
|
2021-09-10 18:03:18 +02:00
|
|
|
bucket_size: Size of buckets in ms. Corresponds roughly to the
|
2016-02-12 17:06:49 +01:00
|
|
|
accuracy of the timer.
|
|
|
|
"""
|
2021-09-10 18:03:18 +02:00
|
|
|
self.bucket_size: int = bucket_size
|
|
|
|
self.entries: List[_Entry[T]] = []
|
|
|
|
self.current_tick: int = 0
|
2016-02-12 17:06:49 +01:00
|
|
|
|
2021-09-10 18:03:18 +02:00
|
|
|
def insert(self, now: int, obj: T, then: int) -> None:
|
2016-02-12 17:06:49 +01:00
|
|
|
"""Inserts object into timer.
|
|
|
|
|
|
|
|
Args:
|
2021-09-10 18:03:18 +02:00
|
|
|
now: Current time in msec
|
|
|
|
obj: Object to be inserted
|
|
|
|
then: When to return the object strictly after.
|
2016-02-12 17:06:49 +01:00
|
|
|
"""
|
|
|
|
then_key = int(then / self.bucket_size) + 1
|
2022-05-06 18:41:57 +02:00
|
|
|
now_key = int(now / self.bucket_size)
|
2016-02-18 17:33:07 +01:00
|
|
|
|
|
|
|
if self.entries:
|
|
|
|
min_key = self.entries[0].end_key
|
|
|
|
max_key = self.entries[-1].end_key
|
|
|
|
|
2022-05-06 18:41:57 +02:00
|
|
|
if min_key < now_key - 10:
|
|
|
|
# If we have ten buckets that are due and still nothing has
|
|
|
|
# called `fetch()` then we likely have a bug that is causing a
|
|
|
|
# memory leak.
|
|
|
|
logger.warning(
|
|
|
|
"Inserting into a wheel timer that hasn't been read from recently. Item: %s",
|
|
|
|
obj,
|
|
|
|
)
|
|
|
|
|
2016-02-18 17:33:07 +01:00
|
|
|
if then_key <= max_key:
|
|
|
|
# The max here is to protect against inserts for times in the past
|
2022-05-06 18:41:57 +02:00
|
|
|
self.entries[max(min_key, then_key) - min_key].elements.add(obj)
|
2016-02-12 17:06:49 +01:00
|
|
|
return
|
|
|
|
|
2022-05-06 18:41:57 +02:00
|
|
|
next_key = now_key + 1
|
2016-02-12 17:06:49 +01:00
|
|
|
if self.entries:
|
|
|
|
last_key = self.entries[-1].end_key
|
|
|
|
else:
|
|
|
|
last_key = next_key
|
|
|
|
|
|
|
|
# Handle the case when `then` is in the past and `entries` is empty.
|
|
|
|
then_key = max(last_key, then_key)
|
|
|
|
|
|
|
|
# Add empty entries between the end of the current list and when we want
|
|
|
|
# to insert. This ensures there are no gaps.
|
2018-04-28 13:57:00 +02:00
|
|
|
self.entries.extend(_Entry(key) for key in range(last_key, then_key + 1))
|
2016-02-12 17:06:49 +01:00
|
|
|
|
2022-05-06 18:41:57 +02:00
|
|
|
self.entries[-1].elements.add(obj)
|
2016-02-12 17:06:49 +01:00
|
|
|
|
2021-09-10 18:03:18 +02:00
|
|
|
def fetch(self, now: int) -> List[T]:
|
2016-02-12 17:06:49 +01:00
|
|
|
"""Fetch any objects that have timed out
|
|
|
|
|
|
|
|
Args:
|
2022-11-16 16:25:24 +01:00
|
|
|
now: Current time in msec
|
2016-02-12 17:06:49 +01:00
|
|
|
|
|
|
|
Returns:
|
2022-11-16 16:25:24 +01:00
|
|
|
List of objects that have timed out
|
2016-02-12 17:06:49 +01:00
|
|
|
"""
|
|
|
|
now_key = int(now / self.bucket_size)
|
|
|
|
|
2022-05-06 18:41:57 +02:00
|
|
|
ret: List[T] = []
|
2016-02-12 17:06:49 +01:00
|
|
|
while self.entries and self.entries[0].end_key <= now_key:
|
2022-05-06 18:41:57 +02:00
|
|
|
ret.extend(self.entries.pop(0).elements)
|
2016-02-12 17:06:49 +01:00
|
|
|
|
|
|
|
return ret
|
2016-02-19 10:50:54 +01:00
|
|
|
|
2021-09-10 18:03:18 +02:00
|
|
|
def __len__(self) -> int:
|
2022-05-06 18:41:57 +02:00
|
|
|
return sum(len(entry.elements) for entry in self.entries)
|