You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#182 was fixed, but I didn't implement rate limiting using burst windows. As per the comment in #184, I raised the question whether this project should provide a built-in rate limiter at all or if we should encourage the user to use an external project like leaky-bucket (full disclosure: my project).
The following is an example of how an external rate limiter is used for anyone who is curious. Unfortunately this hides a lot of utility functions of Sender (like send_cap_req) and it's not straight forward to make async functions like acquire below pluggable. async functions are not supported in traits, nor do we have existential types yet so the anonymous future can be named.
The benefit however is that the rate limiter is an async function that can be await:ed at any point. You have full control over its configuration, use, and implementation. Rate limiting happens before the send is buffered which makes sure that the rest of your application is back pressured when necessary.
use anyhow::Result;use irc::{Message,Sender};use leaky_bucket::LeakyBucket;structRateLimitedSender{sender: irc::Sender,rate_limiter:LeakyBucket,}implRateLimitedSender{pubfnnew(sender:Sender) -> Result<Self>{Ok(Self{
sender,leaky_bucket:LeakyBucket::builder().max(100).refill_interval(Duration::from_secs(10)).refill_amount(100).build()?,})}pubasyncfnsend(m:implInto<Message>) -> Result<()>{self.rate_limiter.acquire(1).await?;self.sender.send(m);Ok(())}}
The text was updated successfully, but these errors were encountered:
I'm fine with presenting the set up for rate limiting differently (including having it be more directly optional), but I'd definitely like to include an out-of-the-box solution within the IRC crate if possible.
Sleep 1s in between sending commands in sync_channel() will hopefully
be good enough while the `irc` crate doesn't actually have ratelimiting
(see aatxe/irc#190).
Calling the send_ function just queues the message to send, but
hopefully waiting a second between commands gives enough time for
limiting to be enough to avoid flooding.
Change-Id: Id81d77699bedb1bc1634b0ccc28423bf7a15e772
#182 was fixed, but I didn't implement rate limiting using burst windows. As per the comment in #184, I raised the question whether this project should provide a built-in rate limiter at all or if we should encourage the user to use an external project like
leaky-bucket
(full disclosure: my project).The following is an example of how an external rate limiter is used for anyone who is curious. Unfortunately this hides a lot of utility functions of
Sender
(likesend_cap_req
) and it's not straight forward to make async functions likeacquire
below pluggable. async functions are not supported in traits, nor do we have existential types yet so the anonymous future can be named.The benefit however is that the rate limiter is an
async
function that can beawait
:ed at any point. You have full control over its configuration, use, and implementation. Rate limiting happens before the send is buffered which makes sure that the rest of your application is back pressured when necessary.The text was updated successfully, but these errors were encountered: