File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -71,12 +71,19 @@ err := doWork()
7171limiter.Record (time.Since (start), err)
7272```
7373## Examples
74- - ** HTTP server example**
75- [ HTTP Example] ( https://github.com/bhatpriyanka8/adaptiveratelimit/tree/main/examples/http )
7674
77- - ** gRPC server example **
78- [ gRPC Example] ( https://github.com/bhatpriyanka8/adaptiveratelimit/tree/main/examples/grpc )
75+ For Runnable examples, refer
76+ [ HTTP Example] ( https://github.com/bhatpriyanka8/adaptiveratelimit/tree/main/examples/http )
7977
78+ [ gRPC Example] ( https://github.com/bhatpriyanka8/adaptiveratelimit/tree/main/examples/grpc )
79+
80+ Go to any of these folders and just run main.go
81+ ```
82+ cd examples/http
83+ go run main.go
84+ ```
85+
86+ go run main.go
8087## Disclaimer
8188
8289This project is developed and maintained in a personal capacity and
Original file line number Diff line number Diff line change 1+ package main
2+
3+ import (
4+ "log"
5+ "net"
6+ "time"
7+
8+ "google.golang.org/grpc"
9+
10+ "github.com/bhatpriyanka8/adaptiveratelimit"
11+ adaptgrpc "github.com/bhatpriyanka8/adaptiveratelimit/grpc"
12+ )
13+
14+ func main () {
15+ cfg := adaptiveratelimit.AdaptiveConfig {
16+ TargetLatency : 200 * time .Millisecond ,
17+ MaxErrorRate : 0.05 ,
18+ IncreaseStep : 1 ,
19+ DecreaseStep : 2 ,
20+ MinLimit : 1 ,
21+ MaxLimit : 100 ,
22+ Cooldown : 2 * time .Second ,
23+ }
24+
25+ limiter := adaptiveratelimit .NewAdaptivePerSecond (10 , cfg )
26+ defer limiter .Stop ()
27+
28+ server := grpc .NewServer (
29+ grpc .UnaryInterceptor (
30+ adaptgrpc .UnaryServerInterceptor (limiter ),
31+ ),
32+ )
33+
34+ listen , err := net .Listen ("tcp" , ":50051" )
35+ if err != nil {
36+ log .Fatalf ("failed to listen: %v" , err )
37+ }
38+
39+ log .Println ("gRPC server listening on :50051" )
40+ if err := server .Serve (listen ); err != nil {
41+ log .Fatalf ("failed to serve: %v" , err )
42+ }
43+ }
Original file line number Diff line number Diff line change 99 "google.golang.org/grpc/status"
1010)
1111
12+ // UnaryServerInterceptor applies adaptive rate limiting to all unary RPCs
1213func UnaryServerInterceptor (l * adaptiveratelimit.Limiter ) grpc.UnaryServerInterceptor {
1314 return func (
1415 ctx context.Context ,
You can’t perform that action at this time.
0 commit comments