RATECHECK(9)

πŸ’– RATECHECK(9) πŸ’–

okay so bestie, we're in the Kernel Developer's Manual, section 9 β€” that's the big-kid section, kernel internals, no cap. this one's called RATECHECK and it's literally just about ✨*vibes per second control*✨ for events. sit down, grab a boba, we're doing rate limiting πŸ§‹


🌸 NAME 🌸

ratecheck, ppsratecheck β€” event rate limiting 🚦

like. it stops your events from being extra. that's the whole job. πŸ’…


✨ SYNOPSIS ✨

#include <sys/time.h>

int
ratecheck(struct timeval *lasttime, struct timeval *mininterval);

int
ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps);

two functions, both return int, both take pointers, both are giving "gatekeeper but make it polite" 🫢


πŸŽ€ DESCRIPTION πŸŽ€

so ratecheck and ppsratecheck are the two functions that handle rate-limiting of arbitrary events. and when we say arbitrary, we mean arbitrary β€” whatever event you got going on, these two will tell you if it's allowed to happen rn or not. iconic behaviour honestly.

the difference between them is lowkey the whole plot:

  • ratecheck enforces a minimum interval between events. think "bestie you literally just texted me, wait a sec" ⏳
  • ppsratecheck enforces a maximum number of events per second. think "okay you can text, but only this many times per second, we're not doing 400 messages again" πŸ“²

πŸ’œ ratecheck πŸ’œ

here's what it does, step by step, no skips:

  • it compares the current time to the value pointed to by lasttime.
  • if that difference is equal to or greater than mininterval β†’ it returns a non-zero value AND updates lasttime to the current time. (green flag behaviour: it actually updates the timestamp so you can't just spam it.)
  • otherwise β†’ it returns zero. that's a no from her. πŸ™…β™€οΈ

πŸ’œ ppsratecheck πŸ’œ

this one's got a lil more going on, she's the main character:

  • first, it compares the current time to lasttime.
  • if at least a full second has passed β†’ the value pointed to by the curpps argument gets reset to 1, and lasttime is updated to the current time. fresh second, fresh count, we move πŸŒ…
  • otherwise β†’ curpps gets incremented, and lasttime is left untouched. don't touch her, she's fine where she is.
  • and in either case β€” listen, this part matters β€” ppsratecheck returns a non-zero value if and only if the updated curpps is less than or equal to maxpps, or maxpps is negative. yep, negative maxpps means unlimited, it's giving "no rules just vibes" 🌈
ngl the negative-maxpps thing is a cute lil escape hatch and it's easy to miss, so like… noted. 🫑

πŸ¦‹ SEE ALSO πŸ¦‹

  • counter(9) β€” go read that one too, she's related, iykyk πŸ’«

⭐ HISTORY ⭐

the ratecheck and ppsratecheck functions first appeared in FreeBSD 5.1. so yeah, they've been slaying since way before you were born, respectfully πŸ‘‘


and that's the whole page, bestie!! πŸŽ€ this was the Debian build of the RATECHECK(9) man page, dated September 25, 2017 β€” so yeah, she's a 2017 girlie and she's still relevant. periodt. πŸ’…βœ¨πŸŒΈ

πŸ“¦ where this page comes from