Skip to content

rate limiter in logger, opt-in - #62

Open
c-cube wants to merge 9 commits into
masterfrom
sc/rate-limit-in-log
Open

rate limiter in logger, opt-in#62
c-cube wants to merge 9 commits into
masterfrom
sc/rate-limit-in-log

Conversation

@c-cube

@c-cube c-cube commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

No description provided.

@c-cube
c-cube requested a review from yasunariw August 25, 2026 12:50

@raphael-proust raphael-proust left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nothing important, just small potatoes feedback

Comment thread log.ml Outdated
Comment thread log.ml Outdated
Comment thread log.ml Outdated
@c-cube
c-cube force-pushed the sc/rate-limit-in-log branch from 2a93b89 to d6991e0 Compare August 25, 2026 18:39
Comment thread log.ml Outdated
Comment thread log.ml Outdated
Comment thread log.ml
Comment thread control.mli Outdated
Comment thread control.mli Outdated
Comment thread control.ml
type t =
| None
| RL of {
mutable tokens: float;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

tokens as float? i guess this helps avoid thinking about rounding but still feels strange

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't really know another way to deal with fractional refilling (what if we refill 3.4 tokens?). Well an alternative is to round up or down depending on Random.float (refill -. floor refill) (ie random(0.4) for refill=3.4) but that seems even weirder.

Comment thread control.ml Outdated
Comment thread control.ml Outdated
Comment thread control.ml
| RL rl ->
let now = Time.now() in

if now > rl.last_update then (

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this is pretty much guaranteed to be always true unless it is called in a tight loop with nothing else to do (but then why rate limit noop action), idk if it is important.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

well sure, although this is using Unix.gettimeofday() so the clock could in theory step backwards.

we could use mtime if that's better?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

i would use integer division and refill only full tokens (and update timestamp when refill actually happens)

@c-cube c-cube Aug 31, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I feel like it'd still lead to token loss.

Say we refill at 1.5 token/second, with an empty bucket, and emit one log every second. We should refill 1.5 token (time delta is 1.) but truncate to 1, so the log is accepted but the bucket remains empty. A burst of 2 log messages at once thus leads to a rejection of the second message, even though we emit logs at 1 log/second.

edit: looks like Go's stdlib uses float64 for the counter

@c-cube
c-cube requested a review from rr0gi August 28, 2026 19:40

@rr0gi rr0gi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

also lets enable some generous rate limit by default

Comment thread control.mli
val create : ?burst_capacity:int -> allowed_per_sec:float -> unit -> t
(** Create a token-bucket rate limiter. The bucket starts full.
@param burst_capacity limits the size of a burst when token bucket is full.
[burst_capacity = N] means a full bucket contains [N * allowed_per_sec] tokens.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

then this is not capacity but more like burst_factor?
i would make the comment less detailed overall : "create rate limiter with asymptotic maximum rate [allowed_per_sec] and handling up to [burst_factor] higher rate at peak", move implementation comments into .ml

Comment thread log.ml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants