ohnx / minervabot

Modular IRC bot. The reincarnation of athenabot!
Apache License 2.0
2 stars 1 forks source link

Flood rate limiting #11

Closed ohnx closed 5 years ago

ohnx commented 5 years ago

Add some sort of way to ratelimit sending. For free node, the recommendation is 1 line every 0.6 seconds. This should be configurable, though.

An easy way to do this is just to check when the last message was sent and sleep until at least 0.6 seconds have passed. I assume it would block until sent? Since we have the main loop always consuming the network, I think it should be fine to do it this way (as long as ping's are being handled with priority, I don't think there is anything else writing to network). The one concern would be that mutexes tend not to be FIFO and rather LIFO since the latest thread is likely in the context/in cache. Normally, this wouldn't be a problem, but if the stuff piles up, it will be since the bottom of the stack will wait forever for the mutex while the more recent things get to send all the stuff.

Consider this more. I don't want to implement an entire sendq since that makes the entire networking part of the bot much more complex, but I also don't want there to be issues if I try to send more than 1 line every 0.6 seconds.

ohnx commented 5 years ago

Ok, I tried this and it doesn't work well at all:

void net_preventflood() {
    uint64_t delta_us;

    clock_gettime(CLOCK_MONOTONIC_RAW, &curr_time);
    delta_us = (curr_time.tv_sec - last_time.tv_sec) * 1000000 + (curr_time.tv_nsec - last_time.tv_nsec) / 1000;

    /* check if it has been less than min_flood_time usec (1 sec) since we last sent a message */
    if (delta_us < min_flood_time) {
        /* it has not, so sleep until that time has been achieved */
        usleep(min_flood_time - delta_us);
    }

    last_time = curr_time;
}
ohnx commented 5 years ago

Stuff gets dropped and whatever, and this generally just doesn't work :(

I think, for now, i will put this on indefinite hold. It is not possible to do it with the current structure of the bot, unfortunately. Maybe in the next version of this bot, it will be possible.