@@ -9,39 +9,39 @@ Apply rate limiting to prevent system overload. Choose appropriate algorithms ba
99
1010**Fixed window rate limiting:**
1111```scala
12- val rateLimiter = RateLimiter.fixedWindow(
13- permits = 100, // 100 requests
14- window = 1.minute // per minute
15- )
16-
17- rateLimiter.runBlocking {
18- callExternalAPI()
19- }
12+ val rateLimiter = RateLimiter.fixedWindow(
13+ permits = 100, // 100 requests
14+ window = 1.minute // per minute
15+ )
16+
17+ rateLimiter.runBlocking {
18+ callExternalAPI()
19+ }
2020```
2121
2222**Token bucket for burst handling:**
2323```scala
24- val rateLimiter = RateLimiter.tokenBucket(
25- capacity = 50, // 50 tokens max
26- refillRate = 10 // 10 tokens per second
27- )
24+ val rateLimiter = RateLimiter.tokenBucket(
25+ capacity = 50, // 50 tokens max
26+ refillRate = 10 // 10 tokens per second
27+ )
2828
29- // Allows bursts up to 50, then 10/second sustained
30- rateLimiter.runBlocking {
31- processRequest()
32- }
29+ // Allows bursts up to 50, then 10/second sustained
30+ rateLimiter.runBlocking {
31+ processRequest()
32+ }
3333```
3434
3535**Sliding window for smooth rate limiting:**
3636```scala
37- val rateLimiter = RateLimiter.slidingWindow(
38- permits = 1000, // 1000 requests
39- window = 1.hour // per hour
40- )
37+ val rateLimiter = RateLimiter.slidingWindow(
38+ permits = 1000, // 1000 requests
39+ window = 1.hour // per hour
40+ )
4141
42- rateLimiter.runBlocking {
43- heavyComputation()
44- }
42+ rateLimiter.runBlocking {
43+ heavyComputation()
44+ }
4545```
4646
4747**Algorithm selection:**
0 commit comments