Skip to content

Commit

Permalink
Merge pull request #2327 from sopel-irc/message-stack-deque
Browse files Browse the repository at this point in the history
irc: use deque for "stack" of recent messages
  • Loading branch information
dgw authored Aug 10, 2022
2 parents 6101e3a + 16f8ca3 commit 44171fa
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions sopel/irc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from __future__ import annotations

import abc
from collections import deque
from datetime import datetime
import logging
import os
Expand Down Expand Up @@ -798,7 +799,7 @@ def say(
with self.sending:
recipient_id = self.make_identifier(recipient)
recipient_stack = self.stack.setdefault(recipient_id, {
'messages': [],
'messages': deque(maxlen=10),
'flood_left': flood_burst_lines,
})

Expand Down Expand Up @@ -846,7 +847,7 @@ def say(

# Loop detection
if antiloop_threshold > 0 and elapsed < antiloop_window:
messages = [m[1] for m in recipient_stack['messages'][-10:]]
messages = [m[1] for m in recipient_stack['messages']]

# If what we're about to send repeated at least N times
# in the anti-looping window, replace it
Expand All @@ -858,11 +859,10 @@ def say(

self.backend.send_privmsg(recipient, text)

# update recipient meta-data
# update recipient metadata
flood_left = recipient_stack['flood_left'] - 1
recipient_stack['flood_left'] = max(0, flood_left)
recipient_stack['messages'].append((time.time(), safe(text)))
recipient_stack['messages'] = recipient_stack['messages'][-10:]

# Now that we've sent the first part, we need to send the rest if
# requested. Doing so recursively seems simpler than iteratively.
Expand Down

0 comments on commit 44171fa

Please sign in to comment.