Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make message rate configurable, including burst mode #908

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions sopel/config/core_section.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,16 @@ def homedir(self):

verify_ssl = ValidatedAttribute('verify_ssl', bool, default=True)
"""Whether to require a trusted SSL certificate for SSL connections."""

bucket_burst_tokens = ValidatedAttribute('bucket_burst_tokens', int,
default=4)
"""How many messages can be sent in burst mode."""

bucket_refill_rate = ValidatedAttribute('bucket_refill_rate', int,
default=1)
"""How many tokens/second to add to the token bucket."""

bucket_empty_wait = ValidatedAttribute('bucket_empty_wait', float,
default=0.7)
"""How long to wait before sending a messaging when not in burst
mode."""
35 changes: 23 additions & 12 deletions sopel/irc.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,18 +418,28 @@ def say(self, text, recipient, max_messages=1):

recipient_id = Identifier(recipient)

if recipient_id not in self.stack:
self.stack[recipient_id] = []
elif self.stack[recipient_id]:
elapsed = time.time() - self.stack[recipient_id][-1][0]
if elapsed < 3:
penalty = float(max(0, len(text) - 50)) / 70
wait = 0.7 + penalty
if elapsed < wait:
time.sleep(wait - elapsed)
reciprec = self.stack.get(recipient_id)
if not reciprec:
reciprec = self.stack[recipient_id] = {
'messages': [],
'burst': self.config.core.bucket_burst_tokens,
}

if not reciprec['burst']:
elapsed = time.time() - reciprec['messages'][-1][0]
reciprec['burst'] = min(
self.config.core.bucket_burst_tokens,
int(elapsed) * self.config.core.bucket_refill_rate)

if not reciprec['burst']:
elapsed = time.time() - reciprec['messages'][-1][0]
penalty = float(max(0, len(text) - 50)) / 70
wait = self.config.core.bucket_empty_wait + penalty
if elapsed < wait:
time.sleep(wait - elapsed)

# Loop detection
messages = [m[1] for m in self.stack[recipient_id][-8:]]
messages = [m[1] for m in reciprec['messages'][-8:]]

# If what we about to send repeated at least 5 times in the
# last 2 minutes, replace with '...'
Expand All @@ -440,8 +450,9 @@ def say(self, text, recipient, max_messages=1):
return

self.write(('PRIVMSG', recipient), text)
self.stack[recipient_id].append((time.time(), self.safe(text)))
self.stack[recipient_id] = self.stack[recipient_id][-10:]
reciprec['burst'] = max(0, reciprec['burst']-1)
reciprec['messages'].append((time.time(), self.safe(text)))
reciprec['messages'] = reciprec['messages'][-10:]
finally:
self.sending.release()
# Now that we've sent the first part, we need to send the rest. Doing
Expand Down