@@ -13,6 +13,7 @@ This example demonstrates how to use middleware in [go-pkgz/pool](https://github
1313 - Input validation before processing
1414 - Automatic retries for failed tasks
1515 - Panic recovery for robustness
16+ - Rate limiting for flow control
1617 - Structured logging for observability
1718
18193 . Real-world patterns:
@@ -26,6 +27,7 @@ This example demonstrates how to use middleware in [go-pkgz/pool](https://github
2627- Task validation before processing
2728- Automatic retries with exponential backoff
2829- Panic recovery with custom handler
30+ - Rate limiting with token bucket algorithm
2931- Structured JSON logging
3032- Performance metrics collection
3133- Configurable worker count and retry attempts
@@ -72,10 +74,11 @@ The implementation demonstrates several key concepts:
72742 . Middleware composition:
7375 ``` go
7476 pool.New [Task](workers, makeWorker ()).Use (
75- middleware.Validate (validator), // validate first
76- middleware.Retry [Task](retries), // then retry on failure
77+ middleware.Validator (validator), // validate first
78+ middleware.Retry [Task](retries), // then retry on failure
7779 middleware.Recovery [Task](handler), // recover from panics
78- customLogger, // log everything
80+ middleware.RateLimiter [Task](5 , 3 ), // rate limit to 5/sec
81+ customLogger, // log everything
7982 )
8083 ```
8184
@@ -114,6 +117,20 @@ The implementation demonstrates several key concepts:
114117 "duration_ms" : 100 ,
115118 "error" : " failed to process task 2"
116119}
120+ {
121+ "time" : " 2025-02-12T10:00:00Z" ,
122+ "level" : " INFO" ,
123+ "msg" : " submitting rate-limited tasks"
124+ }
125+ {
126+ "time" : " 2025-02-12T10:00:00Z" ,
127+ "level" : " INFO" ,
128+ "msg" : " pool finished" ,
129+ "processed" : 14 ,
130+ "errors" : 2 ,
131+ "total_time" : " 3.2s" ,
132+ "duration" : " 2.1s"
133+ }
117134```
118135
119136## Architecture
@@ -137,4 +154,5 @@ Each component is isolated and has a single responsibility, making the code easy
137154- The first middleware wraps the outermost layer
138155- Built-in middleware handles common patterns
139156- Custom middleware can add any functionality
157+ - Rate limiting is shared across all workers in the pool
140158- Structured logging as an example of cross-cutting concern
0 commit comments