Skip to content

Commit 4db7403

Browse files
committed
Update README to clarify adaptive configuration and add gRPC example
1 parent 29c3137 commit 4db7403

3 files changed

Lines changed: 55 additions & 4 deletions

File tree

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,19 @@ err := doWork()
7171
limiter.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

8289
This project is developed and maintained in a personal capacity and

examples/grpc/main.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
}

grpc/interceptor.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"google.golang.org/grpc/status"
1010
)
1111

12+
// UnaryServerInterceptor applies adaptive rate limiting to all unary RPCs
1213
func UnaryServerInterceptor(l *adaptiveratelimit.Limiter) grpc.UnaryServerInterceptor {
1314
return func(
1415
ctx context.Context,

0 commit comments

Comments
 (0)