Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Warn if inserting to a wheel timer that hasn't been read recently
Browse files Browse the repository at this point in the history
  • Loading branch information
erikjohnston committed May 6, 2022
1 parent bae9665 commit 769e141
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion synapse/util/wheel_timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
# 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.
import logging
from typing import Generic, Hashable, List, Set, TypeVar

logger = logging.getLogger(__name__)

T = TypeVar("T", bound=Hashable)


Expand Down Expand Up @@ -51,17 +54,27 @@ def insert(self, now: int, obj: T, then: int) -> None:
then: When to return the object strictly after.
"""
then_key = int(then / self.bucket_size) + 1
now_key = int(now / self.bucket_size)

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

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,
)

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.add(obj)
return

next_key = int(now / self.bucket_size) + 1
next_key = now_key + 1
if self.entries:
last_key = self.entries[-1].end_key
else:
Expand Down

0 comments on commit 769e141

Please sign in to comment.