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

config, bot: Configurable message rate #1518

Merged
merged 3 commits into from
Jun 7, 2019
Merged

Conversation

kwaaak
Copy link
Contributor

@kwaaak kwaaak commented Mar 25, 2019

Code originally by @larsks in #908

I just made it compatible with the current state.

@dgw dgw changed the base branch from 6.6.x to master March 25, 2019 06:18
@dgw dgw added this to the 7.0.0 milestone Mar 25, 2019
@dgw dgw added the Feature label Mar 25, 2019
@dgw
Copy link
Member

dgw commented Mar 25, 2019

@kwaaak This needs to go into master, not 6.6.x, so it'll need rebasing. (Unfortunately, I discovered an issue with the changelog after tagging the release, so you have an extra commit past what was merged.)

It would also be great to include the original commit message (feel free to correct the spelling—there are a few typos in there), and a Co-authored-by: trailer at the end to give @larsks credit as well. (I won't put his name & email here, to lessen the risk of scraper bots grabbing it from the conversation, but it's easily retrieved. I can send it to you on IRC if you need.)

@kwaaak kwaaak force-pushed the patch-4 branch 4 times, most recently from 8ec333c to 64e71c8 Compare March 25, 2019 07:21
@kwaaak kwaaak closed this Mar 25, 2019
@kwaaak kwaaak reopened this Mar 25, 2019
Copy link
Member

@dgw dgw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Between the two of us, we got the branch rebased and the commit message updated.

Time for a real look at the code.

@kwaaak If you want, you can address these yourself, in a new commit (don't amend/rebase). You can also wait for @Exirel to weigh in, since I'll ask his opinion too. One of us can also make the name changes if you don't want to. 😸

sopel/config/core_section.py Outdated Show resolved Hide resolved
sopel/bot.py Outdated Show resolved Hide resolved
@dgw dgw requested a review from Exirel March 25, 2019 08:14
Copy link
Contributor

@Exirel Exirel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good so far, the main logic is here. We can nitpick at naming things, but I feel like it's a valuable improvement.

sopel/bot.py Outdated Show resolved Hide resolved
@deathbybandaid
Copy link
Contributor

deathbybandaid commented Apr 2, 2019

With the work I'm doing on multi-recipients and reaching the 512byte limit of a message, I'm not sure exactly how this is going to affect that. #1525

@dgw
Copy link
Member

dgw commented Apr 2, 2019

@deathbybandaid Affect that in what way? Or do you just mean that they both step on the same part of the code? (In which case, by convention, this will get merged first because it's older.)

dgw
dgw previously approved these changes Apr 2, 2019
Copy link
Member

@dgw dgw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy after the renaming of things. Now to see if @Exirel wants more renames or not. 😛

@dgw dgw requested a review from Exirel April 2, 2019 21:31
Exirel
Exirel previously approved these changes Apr 2, 2019
@Exirel
Copy link
Contributor

Exirel commented Apr 2, 2019

Code-style and naming-thing wise, I'm happy about your changes! LGTM.

@deathbybandaid
Copy link
Contributor

The configfile part of this PR is going to be super helpful for what I'm working on.

The actual implementation of how the flood protection works with multiple recipients and my new bytecounting logic is likely to be a bit different than this.

Not a conflict, just pointing out that I'm not fully sure how I'm working out my stuff yet, and this add a little extra layer to that. :)

@dgw
Copy link
Member

dgw commented Apr 2, 2019

Plan to build on top of this, @deathbybandaid, and see where it takes you. Remember that git reset can be your friend if you break a pull request and need to go back to a previous good state. 😉

I think it's been said before, but adding "official" support for multiple recipients isn't extremely high on the priority list compared to this since it's already possible to do "manually" (with commas).

@deathbybandaid
Copy link
Contributor

I decided to give your branch a test, and have come across an issue:

Signature: AttributeError: 'dict' object has no attribute 'replace' (file "/usr/local/lib/python3.6/dist-packages/sopel/irc.py", line 121, in safe)
from deathbybandaid at 2019-04-03 12:37:43.234871. Message was: .dbbtest
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/sopel/bot.py", line 473, in call
    exit_code = func(sopel, trigger)
  File "/home/sopel/.sopel/modules/dbbtest.py", line 25, in mainfunction
    bot.say(str(lorem_ipsum), trigger.sender)
  File "/usr/local/lib/python3.6/dist-packages/sopel/bot.py", line 410, in say
    self._bot.say(message, destination, max_messages)
  File "/usr/local/lib/python3.6/dist-packages/sopel/bot.py", line 337, in say
    self.write(('PRIVMSG', recipient), text)
  File "/usr/local/lib/python3.6/dist-packages/sopel/bot.py", line 166, in write
    irc.Bot.write(self, args, text=text)
  File "/usr/local/lib/python3.6/dist-packages/sopel/irc.py", line 126, in write
    args = [self.safe(arg) for arg in args]
  File "/usr/local/lib/python3.6/dist-packages/sopel/irc.py", line 126, in <listcomp>
    args = [self.safe(arg) for arg in args]
  File "/usr/local/lib/python3.6/dist-packages/sopel/irc.py", line 121, in safe
    string = string.replace('\n', '')
AttributeError: 'dict' object has no attribute 'replace'

It looks like you may be overwriting the variable recipient?

Copy link
Contributor

@deathbybandaid deathbybandaid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using recipient for the dict overrides the actual recipient that gets sent to self.write causing:

AttributeError: 'dict' object has no attribute 'replace' (file "/usr/local/lib/python3.6/dist-packages/sopel/irc.py", line 121, in safe)

sopel/bot.py Outdated Show resolved Hide resolved
sopel/bot.py Outdated Show resolved Hide resolved
sopel/bot.py Outdated Show resolved Hide resolved
sopel/bot.py Outdated Show resolved Hide resolved
sopel/bot.py Outdated Show resolved Hide resolved
sopel/bot.py Outdated Show resolved Hide resolved
sopel/bot.py Outdated Show resolved Hide resolved
sopel/bot.py Outdated Show resolved Hide resolved
sopel/bot.py Outdated Show resolved Hide resolved
sopel/bot.py Outdated Show resolved Hide resolved
@dgw
Copy link
Member

dgw commented May 12, 2019

@kwaaak Seeing as we keep getting issues asking how to turn off the flood protection delay, I think it's time this was rebased and squashed a bit so we can merge it.

I'll leave sorting out the merge conflict to you. As far as squashing, you can meld the last two or even three commits into the first commit, merging the co-author trailers together. With co-author tags and detailed PR history, we don't have to worry too much about losing attribution in squashes. 😸

@dgw
Copy link
Member

dgw commented May 12, 2019

@kwaaak Alternatively, you can squash just the last commit ("Update bot.py") into the first, and tweak the third commit's message: "Fix variable name" -> "bot: fix variable name in flood prevention logic"

That last commit makes a change to the penalty logic that was already made in #1552 between when this PR was opened and now, so it makes sense to include it in the first commit after rebasing.

@kwaaak kwaaak force-pushed the patch-4 branch 3 times, most recently from b1d8664 to 7dc09fc Compare May 13, 2019 21:01
Original change description by @larsks (from sopel-irc#908, slightly edited):

This adds several knobs that control how fast the bot sends multiple
messages to the same recipient:

- bucket_burst_tokens -- this controls how many messages may be sent in
  "burst mode", i.e., without any inter-message delay.
- bucket_refill_rate -- once exhausted, burst tokens are refilled at a
  rate of bucket_refill_rate tokens/second.
- bucket_empty_wait -- how long to wait between sending messages when
  not in burst mode.

This permits the bot to return a few lines of information quickly and
not trigger flood protection.

How it works:

When sending to a new recipient, we initialize a token counter to
bucket_burst_tokens. Every time we send a message, we decrement this by
1. If the token counter reaches 0, we engage the rate limiting behavior
(which is identical to the existing code, except that the minimum wait
of 0.7 seconds is now configurable).

When sending a new message to an existing recipient, we check if the
token counter is exhausted. If it is, we refill it based on the elapsed
time since the last message and the value of bucket_refill_rate, and
then proceed as described above.

----

Adapted to current Sopel code and rebased by these users, respectively:

Co-authored-by: kwaaak <kwaaak@users.noreply.github.com>
Co-authored-by: dgw <dgw@technobabbl.es>
Co-authored-by: deathbybandaid <sam@deathbybandaid.net>
@dgw
Copy link
Member

dgw commented May 13, 2019

I think this is successfully rescued from the brink of being thrown out and started over. Always make a backup branch when you rebase, people—and don't delete your local repo if it breaks, because you'll want to keep that reflog! (@kwaaak learned a valuable lesson today.)

@Exirel do you stand by your approval of this now that the dust has settled post-squash?

@Exirel
Copy link
Contributor

Exirel commented May 14, 2019

@Exirel do you stand by your approval of this now that the dust has settled post-squash?

@dgw yeah, LGTM. My main concern was naming stuff, and @kwaaak did a great job here. The logic looks good too, but I haven't tested it myself. If you feel confident, it's OK by me!

@deathbybandaid
Copy link
Contributor

couldn't figure out how to make github let me review these lines:

            # No messages within the last 3 seconds? Go ahead!
            # Otherwise, wait so it's been at least 0.8 seconds + penalty

May need to update these comments with the new configurable logic. Especially since the default for 'flood_empty_wait' = 0.7

@deathbybandaid
Copy link
Contributor

I have taken the time to test different combinations of configured numbers, and I'm very happy with this.

My primary bots sit behind ZNC, and this allows me to essentially disable the flood protection from the bot and rely on ZNCs flood protection, if I want.

@Exirel
Copy link
Contributor

Exirel commented May 15, 2019

I have taken the time to test different combinations of configured numbers, and I'm very happy with this.

Thank you very much for your field testing. This is not a feature that is easy to test, so this type of feedback is very valuable. It's good to know it works as intended!

So I guess 🚢 ? 😄

Loop detection was only running if the bot was out of flood lines. It
should ALWAYS try to detect message loops.

Also removed an outdated comment and added updated ones for the new
flood-protection logic.
@dgw
Copy link
Member

dgw commented Jun 6, 2019

I was looking over this one last time, partly because of @deathbybandaid's comment, and I was having the damnedest time figuring out how to fix the code comments. Then I realized part of the issue was the loop-detection code being indented too far (so it became part of an if block it shouldn't be).

Re-requesting review so someone else can double-check me 😁

@dgw dgw requested a review from a team June 6, 2019 15:22
@dgw dgw dismissed stale reviews from Exirel and themself June 6, 2019 15:24

I touched it, so I can't approve my own work.

Copy link
Contributor

@Exirel Exirel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dgw
Copy link
Member

dgw commented Jun 7, 2019

OK, I'm finally ready to ship this. Anything I missed can get fixed up when @Exirel inevitably moves a big chunk of the new logic over into bot.write(), because I know he wants to! 😁

@dgw dgw merged commit 61a2699 into sopel-irc:master Jun 7, 2019
@kwaaak kwaaak deleted the patch-4 branch December 9, 2019 00:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants