[{"data":1,"prerenderedAt":682},["ShallowReactive",2],{"$fm1-oeDIaWELPIjAolFYgXkyCaafG73km85MoK9yYIGo":3},{"slug":4,"title":5,"description":6,"source":7,"document":8,"toc":667,"editUrl":675,"prev":676,"next":679},"techniques\u002Frate-limiting","Rate limiting","Protect endpoints with fixed-window throttling via ThrottlerGuard and ThrottlerInterceptor.","---\ntitle: Rate limiting\ndescription: Protect endpoints with fixed-window throttling via ThrottlerGuard and ThrottlerInterceptor.\n---\n\n# Rate limiting\n\n`ferrite-throttler` adds fixed-window rate limiting with an in-memory ledger, exposed both as a guard and as an interceptor.\n\n## Enable it\n\n```cargo\nfr add throttler\n```\n\n```rust\n#[module(imports = [ThrottlerModule])]\npub struct AppModule;\n```\n\n## `ThrottlerService`\n\nThe engine:\n\n```rust\nuse ferrite_throttler::ThrottlerService;\n\n#[injectable]\npub struct SignupController {\n    throttler: ThrottlerService,\n}\n\nimpl SignupController {\n    #[inject]\n    pub fn new(throttler: ThrottlerService) -> Self {\n        Self { throttler }\n    }\n\n    pub async fn hit(&self, key: String) -> Result\u003CHitOk, HitLimit> {\n        self.throttler.hit(key, self.throttler.default_limit(), self.throttler.default_window()).await\n    }\n}\n```\n\n| Method | Returns |\n| --- | --- |\n| `hit(key, limit, window_secs)` | `HitOk{used}` or `HitLimit{used}` |\n| `peek(key)` | Current count without increment |\n| `reset(key)` | Clear a key |\n| `purge_old()` | Drop expired windows |\n| `key_for_request(&RequestCtx)` | Derive a key from the request (e.g. IP) |\n| `default_limit()` \u002F `default_window()` | Config-backed defaults |\n\n`now_secs()` exposes the current time source (easy to stub in tests).\n\n## Guard usage\n\nDeny requests that exceed the limit:\n\n```rust\nuse ferrite_throttler::ThrottlerGuard;\n\n#[impl_controller]\nimpl SignupController {\n    #[use_guards(ThrottlerGuard)]\n    #[post(\"\u002F\")]\n    pub async fn signup(&self, Json(dto): Json\u003CSignupDto>) -> Result\u003CJson\u003C()>, HttpError> {\n        \u002F\u002F 429 when the client exceeds the window\n        Ok(Json(()))\n    }\n}\n```\n\n## Interceptor usage\n\nThe same policy as an interceptor over a whole block:\n\n```rust\n#[impl_controller]\n#[use_interceptors(ThrottlerInterceptor)]\nimpl SignupController { \u002F* ... *\u002F }\n```\n\n`ThrottlerInterceptor` implements `Interceptor` and rejects with 429 when the budget is spent.\n\n## Behavior\n\n- Fixed-window counting per key with a configurable window (`default_window`).\n- `purge_old` keeps the ledger tidy; the interceptor runs it opportunistically.\n- Keys are request-scoped (`key_for_request`) by default — override if you want per-user budgets.\n- Because it's in-memory, it's per-instance. For multi-instance throttling, pair it with the Redis counter helpers in `ferrite-cache-redis`.\n\n## Next steps\n\n- [Guards](\u002Fdocs\u002Foverview\u002Fguards).\n- [Background jobs](\u002Fdocs\u002Ftechniques\u002Fbackground-jobs).",{"frontmatter":9,"meta":10,"nodes":34},{"title":5,"description":6},{"toc":11},{"title":12,"searchDepth":13,"depth":14,"links":15},"On This Page",2,3,[16,19,22,25,28,31],{"id":17,"depth":13,"text":18},"enable-it","Enable it",{"id":20,"depth":13,"text":21},"throttlerservice","ThrottlerService",{"id":23,"depth":13,"text":24},"guard-usage","Guard usage",{"id":26,"depth":13,"text":27},"interceptor-usage","Interceptor usage",{"id":29,"depth":13,"text":30},"behavior","Behavior",{"id":32,"depth":13,"text":33},"next-steps","Next steps",[35,39,47,50,59,104,108,111,338,428,434,436,439,572,574,577,610,619,621,650,652],[36,37,5],"h1",{"id":38},"rate-limiting",[40,41,42,46],"p",{},[43,44,45],"code",{},"ferrite-throttler"," adds fixed-window rate limiting with an in-memory ledger, exposed both as a guard and as an interceptor.",[48,49,18],"h2",{"id":17},[51,52,55],"pre",{"language":53,"class":54},"cargo","shiki dark:github-dark",[43,56,58],{"class":57},"language-cargo","fr add throttler",[51,60,63],{"language":61,"class":62},"rust","shiki shiki-themes github-light github-dark dark:github-dark",[43,64,66,89,90],{"class":65},"language-rust",[67,68,71,75,79,82,86],"span",{"class":69,"style":70},"line","display: inline",[67,72,74],{"style":73},"color:#24292E;--shiki-dark:#E1E4E8","#[module(imports ",[67,76,78],{"style":77},"color:#D73A49;--shiki-dark:#F97583","=",[67,80,81],{"style":73}," [",[67,83,85],{"style":84},"color:#6F42C1;--shiki-dark:#B392F0","ThrottlerModule",[67,87,88],{"style":73},"])]","\n",[67,91,92,95,98,101],{"class":69,"style":70},[67,93,94],{"style":77},"pub",[67,96,97],{"style":77}," struct",[67,99,100],{"style":84}," AppModule",[67,102,103],{"style":73},";",[48,105,106],{"id":20},[43,107,21],{},[40,109,110],{},"The engine:",[51,112,113],{"language":61,"class":62},[43,114,115,89,130,89,132,89,137,89,149,89,163,89,168,89,170,89,179,89,184,89,214,89,222,89,227,89,229,89,280,89,330,89,334],{"class":65},[67,116,117,120,123,126,128],{"class":69,"style":70},[67,118,119],{"style":77},"use",[67,121,122],{"style":84}," ferrite_throttler",[67,124,125],{"style":77},"::",[67,127,21],{"style":84},[67,129,103],{"style":73},[67,131],{"class":69,"style":70},[67,133,134],{"class":69,"style":70},[67,135,136],{"style":73},"#[injectable]",[67,138,139,141,143,146],{"class":69,"style":70},[67,140,94],{"style":77},[67,142,97],{"style":77},[67,144,145],{"style":84}," SignupController",[67,147,148],{"style":73}," {",[67,150,151,154,157,160],{"class":69,"style":70},[67,152,153],{"style":73},"    throttler",[67,155,156],{"style":77},":",[67,158,159],{"style":84}," ThrottlerService",[67,161,162],{"style":73},",",[67,164,165],{"class":69,"style":70},[67,166,167],{"style":73},"}",[67,169],{"class":69,"style":70},[67,171,172,175,177],{"class":69,"style":70},[67,173,174],{"style":77},"impl",[67,176,145],{"style":84},[67,178,148],{"style":73},[67,180,181],{"class":69,"style":70},[67,182,183],{"style":73},"    #[inject]",[67,185,186,189,192,195,198,200,202,205,208,212],{"class":69,"style":70},[67,187,188],{"style":77},"    pub",[67,190,191],{"style":77}," fn",[67,193,194],{"style":84}," new",[67,196,197],{"style":73},"(throttler",[67,199,156],{"style":77},[67,201,159],{"style":84},[67,203,204],{"style":73},") ",[67,206,207],{"style":77},"->",[67,209,211],{"style":210},"color:#005CC5;--shiki-dark:#79B8FF"," Self",[67,213,148],{"style":73},[67,215,216,219],{"class":69,"style":70},[67,217,218],{"style":210},"        Self",[67,220,221],{"style":73}," { throttler }",[67,223,224],{"class":69,"style":70},[67,225,226],{"style":73},"    }",[67,228],{"class":69,"style":70},[67,230,231,233,236,238,241,244,247,250,253,255,258,260,262,265,268,271,274,277],{"class":69,"style":70},[67,232,188],{"style":77},[67,234,235],{"style":77}," async",[67,237,191],{"style":77},[67,239,240],{"style":84}," hit",[67,242,243],{"style":73},"(",[67,245,246],{"style":77},"&",[67,248,249],{"style":210},"self",[67,251,252],{"style":73},", key",[67,254,156],{"style":77},[67,256,257],{"style":84}," String",[67,259,204],{"style":73},[67,261,207],{"style":77},[67,263,264],{"style":84}," Result",[67,266,267],{"style":73},"\u003C",[67,269,270],{"style":84},"HitOk",[67,272,273],{"style":73},", ",[67,275,276],{"style":84},"HitLimit",[67,278,279],{"style":73},"> {",[67,281,282,285,288,291,293,296,299,301,303,305,307,310,313,315,317,319,321,324,327],{"class":69,"style":70},[67,283,284],{"style":210},"        self",[67,286,287],{"style":77},".",[67,289,290],{"style":73},"throttler",[67,292,287],{"style":77},[67,294,295],{"style":84},"hit",[67,297,298],{"style":73},"(key, ",[67,300,249],{"style":210},[67,302,287],{"style":77},[67,304,290],{"style":73},[67,306,287],{"style":77},[67,308,309],{"style":84},"default_limit",[67,311,312],{"style":73},"(), ",[67,314,249],{"style":210},[67,316,287],{"style":77},[67,318,290],{"style":73},[67,320,287],{"style":77},[67,322,323],{"style":84},"default_window",[67,325,326],{"style":73},"())",[67,328,329],{"style":77},".await",[67,331,332],{"class":69,"style":70},[67,333,226],{"style":73},[67,335,336],{"class":69,"style":70},[67,337,167],{"style":73},[339,340,341,354],"table",{},[342,343,344],"thead",{},[345,346,347,351],"tr",{},[348,349,350],"th",{},"Method",[348,352,353],{},"Returns",[355,356,357,374,384,394,404,414],"tbody",{},[345,358,359,365],{},[360,361,362],"td",{},[43,363,364],{},"hit(key, limit, window_secs)",[360,366,367,370,371],{},[43,368,369],{},"HitOk{used}"," or ",[43,372,373],{},"HitLimit{used}",[345,375,376,381],{},[360,377,378],{},[43,379,380],{},"peek(key)",[360,382,383],{},"Current count without increment",[345,385,386,391],{},[360,387,388],{},[43,389,390],{},"reset(key)",[360,392,393],{},"Clear a key",[345,395,396,401],{},[360,397,398],{},[43,399,400],{},"purge_old()",[360,402,403],{},"Drop expired windows",[345,405,406,411],{},[360,407,408],{},[43,409,410],{},"key_for_request(&RequestCtx)",[360,412,413],{},"Derive a key from the request (e.g. IP)",[345,415,416,425],{},[360,417,418,421,422],{},[43,419,420],{},"default_limit()"," \u002F ",[43,423,424],{},"default_window()",[360,426,427],{},"Config-backed defaults",[40,429,430,433],{},[43,431,432],{},"now_secs()"," exposes the current time source (easy to stub in tests).",[48,435,24],{"id":23},[40,437,438],{},"Deny requests that exceed the limit:",[51,440,441],{"language":61,"class":62},[43,442,443,89,456,89,458,89,463,89,471,89,481,89,492,89,546,89,552,89,564,89,568],{"class":65},[67,444,445,447,449,451,454],{"class":69,"style":70},[67,446,119],{"style":77},[67,448,122],{"style":84},[67,450,125],{"style":77},[67,452,453],{"style":84},"ThrottlerGuard",[67,455,103],{"style":73},[67,457],{"class":69,"style":70},[67,459,460],{"class":69,"style":70},[67,461,462],{"style":73},"#[impl_controller]",[67,464,465,467,469],{"class":69,"style":70},[67,466,174],{"style":77},[67,468,145],{"style":84},[67,470,148],{"style":73},[67,472,473,476,478],{"class":69,"style":70},[67,474,475],{"style":73},"    #[use_guards(",[67,477,453],{"style":84},[67,479,480],{"style":73},")]",[67,482,483,486,490],{"class":69,"style":70},[67,484,485],{"style":73},"    #[post(",[67,487,489],{"style":488},"color:#032F62;--shiki-dark:#9ECBFF","\"\u002F\"",[67,491,480],{"style":73},[67,493,494,496,498,500,503,505,507,509,511,514,517,519,522,524,527,530,532,534,536,538,541,544],{"class":69,"style":70},[67,495,188],{"style":77},[67,497,235],{"style":77},[67,499,191],{"style":77},[67,501,502],{"style":84}," signup",[67,504,243],{"style":73},[67,506,246],{"style":77},[67,508,249],{"style":210},[67,510,273],{"style":73},[67,512,513],{"style":84},"Json",[67,515,516],{"style":73},"(dto)",[67,518,156],{"style":77},[67,520,521],{"style":84}," Json",[67,523,267],{"style":73},[67,525,526],{"style":84},"SignupDto",[67,528,529],{"style":73},">) ",[67,531,207],{"style":77},[67,533,264],{"style":84},[67,535,267],{"style":73},[67,537,513],{"style":84},[67,539,540],{"style":73},"\u003C()>, ",[67,542,543],{"style":84},"HttpError",[67,545,279],{"style":73},[67,547,548],{"class":69,"style":70},[67,549,551],{"style":550},"color:#6A737D;--shiki-dark:#6A737D","        \u002F\u002F 429 when the client exceeds the window",[67,553,554,557,559,561],{"class":69,"style":70},[67,555,556],{"style":84},"        Ok",[67,558,243],{"style":73},[67,560,513],{"style":84},[67,562,563],{"style":73},"(()))",[67,565,566],{"class":69,"style":70},[67,567,226],{"style":73},[67,569,570],{"class":69,"style":70},[67,571,167],{"style":73},[48,573,27],{"id":26},[40,575,576],{},"The same policy as an interceptor over a whole block:",[51,578,579],{"language":61,"class":62},[43,580,581,89,585,89,595],{"class":65},[67,582,583],{"class":69,"style":70},[67,584,462],{"style":73},[67,586,587,590,593],{"class":69,"style":70},[67,588,589],{"style":73},"#[use_interceptors(",[67,591,592],{"style":84},"ThrottlerInterceptor",[67,594,480],{"style":73},[67,596,597,599,601,604,607],{"class":69,"style":70},[67,598,174],{"style":77},[67,600,145],{"style":84},[67,602,603],{"style":73}," { ",[67,605,606],{"style":550},"\u002F* ... *\u002F",[67,608,609],{"style":73}," }",[40,611,612,614,615,618],{},[43,613,592],{}," implements ",[43,616,617],{},"Interceptor"," and rejects with 429 when the budget is spent.",[48,620,30],{"id":29},[622,623,624,631,637,644],"ul",{},[625,626,627,628,630],"li",{},"Fixed-window counting per key with a configurable window (",[43,629,323],{},").",[625,632,633,636],{},[43,634,635],{},"purge_old"," keeps the ledger tidy; the interceptor runs it opportunistically.",[625,638,639,640,643],{},"Keys are request-scoped (",[43,641,642],{},"key_for_request",") by default — override if you want per-user budgets.",[625,645,646,647,287],{},"Because it's in-memory, it's per-instance. For multi-instance throttling, pair it with the Redis counter helpers in ",[43,648,649],{},"ferrite-cache-redis",[48,651,33],{"id":32},[622,653,654,661],{},[625,655,656,287],{},[657,658,660],"a",{"href":659},"\u002Fdocs\u002Foverview\u002Fguards","Guards",[625,662,663,287],{},[657,664,666],{"href":665},"\u002Fdocs\u002Ftechniques\u002Fbackground-jobs","Background jobs",{"title":12,"searchDepth":13,"depth":14,"links":668},[669,670,671,672,673,674],{"id":17,"depth":13,"text":18},{"id":20,"depth":13,"text":21},{"id":23,"depth":13,"text":24},{"id":26,"depth":13,"text":27},{"id":29,"depth":13,"text":30},{"id":32,"depth":13,"text":33},"https:\u002F\u002Fgithub.com\u002Fj4flmao\u002Fferrite_rs\u002Fedit\u002Fmain\u002Fdocs\u002Fcontent\u002Ftechniques\u002Frate-limiting.md",{"title":677,"slug":678},"Caching (Redis)","techniques\u002Fcaching",{"title":680,"slug":681},"WebSockets","techniques\u002Fwebsockets",1789565440184]