summary refs log tree commit diff
path: root/synapse/util/wheel_timer.py
blob: e108adc4604fc4a0ab1e81cfb950baf81fd3df5a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Copyright 2016 OpenMarket Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Generic, List, TypeVar

T = TypeVar("T")


class _Entry(Generic[T]):
    __slots__ = ["end_key", "queue"]

    def __init__(self, end_key: int) -> None:
        self.end_key: int = end_key
        self.queue: List[T] = []


class WheelTimer(Generic[T]):
    """Stores arbitrary objects that will be returned after their timers have
    expired.
    """

    def __init__(self, bucket_size: int = 5000) -> None:
        """
        Args:
            bucket_size: Size of buckets in ms. Corresponds roughly to the
                accuracy of the timer.
        """
        self.bucket_size: int = bucket_size
        self.entries: List[_Entry[T]] = []
        self.current_tick: int = 0

    def insert(self, now: int, obj: T, then: int) -> None:
        """Inserts object into timer.

        Args:
            now: Current time in msec
            obj: Object to be inserted
            then: When to return the object strictly after.
        """
        then_key = int(then / self.bucket_size) + 1

        if self.entries:
            min_key = self.entries[0].end_key
            max_key = self.entries[-1].end_key

            if then_key <= max_key:
                # The max here is to protect against inserts for times in the past
                self.entries[max(min_key, then_key) - min_key].queue.append(obj)
                return

        next_key = int(now / self.bucket_size) + 1
        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.
        self.entries.extend(_Entry(key) for key in range(last_key, then_key + 1))

        self.entries[-1].queue.append(obj)

    def fetch(self, now: int) -> List[T]:
        """Fetch any objects that have timed out

        Args:
            now (ms): Current time in msec

        Returns:
            list: List of objects that have timed out
        """
        now_key = int(now / self.bucket_size)

        ret = []
        while self.entries and self.entries[0].end_key <= now_key:
            ret.extend(self.entries.pop(0).queue)

        return ret

    def __len__(self) -> int:
        return sum(len(entry.queue) for entry in self.entries)