|
13 | 13 | * [Documentation](#documentation) |
14 | 14 | * [Workers](#workers) |
15 | 15 | * [WorkerPool](#workerpool) |
| 16 | + * [Middlewares](#middlewares) |
16 | 17 | * [Logging](#logging) |
17 | 18 | * [Tracing](#tracing) |
18 | 19 | * [Metrics](#metrics) |
@@ -156,6 +157,115 @@ func main() { |
156 | 157 | } |
157 | 158 | ``` |
158 | 159 |
|
| 160 | +### Middlewares |
| 161 | + |
| 162 | +This module provides middleware support for workers, allowing you to add behaviors without modifying the worker's core implementation. |
| 163 | + |
| 164 | +Middlewares wrap a worker's `Run` method and can perform actions before and after the worker execution, or even modify the execution flow. |
| 165 | + |
| 166 | +#### Using Middlewares |
| 167 | + |
| 168 | +Here's an example of a middleware that returns an error if a worker takes too long to execute: |
| 169 | + |
| 170 | +```go |
| 171 | +package main |
| 172 | + |
| 173 | +import ( |
| 174 | + "context" |
| 175 | + "errors" |
| 176 | + "time" |
| 177 | + |
| 178 | + "github.com/ankorstore/yokai/worker" |
| 179 | +) |
| 180 | + |
| 181 | +// SimpleWorker is a basic worker that sleeps for a specified duration |
| 182 | +type SimpleWorker struct { |
| 183 | + sleepDuration time.Duration |
| 184 | +} |
| 185 | + |
| 186 | +// NewSimpleWorker creates a new SimpleWorker |
| 187 | +func NewSimpleWorker(sleepDuration time.Duration) *SimpleWorker { |
| 188 | + return &SimpleWorker{ |
| 189 | + sleepDuration: sleepDuration, |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +// Name returns the worker name |
| 194 | +func (w *SimpleWorker) Name() string { |
| 195 | + return "simple-worker" |
| 196 | +} |
| 197 | + |
| 198 | +// Run executes the worker |
| 199 | +func (w *SimpleWorker) Run(ctx context.Context) error { |
| 200 | + // Simulate work by sleeping |
| 201 | + time.Sleep(w.sleepDuration) |
| 202 | + |
| 203 | + return nil |
| 204 | +} |
| 205 | + |
| 206 | +// TimeoutMiddleware implements the worker.Middleware interface |
| 207 | +type TimeoutMiddleware struct { |
| 208 | + timeout time.Duration |
| 209 | +} |
| 210 | + |
| 211 | +// NewTimeoutMiddleware creates a new TimeoutMiddleware with the specified timeout |
| 212 | +func NewTimeoutMiddleware(timeout time.Duration) *TimeoutMiddleware { |
| 213 | + return &TimeoutMiddleware{ |
| 214 | + timeout: timeout, |
| 215 | + } |
| 216 | +} |
| 217 | + |
| 218 | +// Name returns the middleware name |
| 219 | +func (m *TimeoutMiddleware) Name() string { |
| 220 | + return "timeout-middleware" |
| 221 | +} |
| 222 | + |
| 223 | +// Handle returns the middleware function |
| 224 | +func (m *TimeoutMiddleware) Handle() worker.MiddlewareFunc { |
| 225 | + return func(next worker.HandlerFunc) worker.HandlerFunc { |
| 226 | + return func(ctx context.Context) error { |
| 227 | + // Create a timeout context |
| 228 | + timeoutCtx, cancel := context.WithTimeout(ctx, m.timeout) |
| 229 | + defer cancel() |
| 230 | + |
| 231 | + // Create a channel to receive the result of the worker execution |
| 232 | + done := make(chan error) |
| 233 | + |
| 234 | + // Execute the worker in a goroutine |
| 235 | + go func() { |
| 236 | + done <- next(timeoutCtx) |
| 237 | + }() |
| 238 | + |
| 239 | + // Wait for either the worker to complete or the timeout to occur |
| 240 | + select { |
| 241 | + case err := <-done: |
| 242 | + return err |
| 243 | + case <-timeoutCtx.Done(): |
| 244 | + if errors.Is(timeoutCtx.Err(), context.DeadlineExceeded) { |
| 245 | + return errors.New("worker execution timed out") |
| 246 | + } |
| 247 | + return timeoutCtx.Err() |
| 248 | + } |
| 249 | + } |
| 250 | + } |
| 251 | +} |
| 252 | + |
| 253 | +func main() { |
| 254 | + // Create a worker pool with a worker and the timeout middleware |
| 255 | + pool, _ := worker.NewDefaultWorkerPoolFactory().Create( |
| 256 | + worker.WithWorker( |
| 257 | + NewSimpleWorker(10 * time.Second), // Worker that takes 10 seconds to complete |
| 258 | + worker.WithMiddlewares(NewTimeoutMiddleware(5 * time.Second)), // Middleware that times out after 5 seconds |
| 259 | + ), |
| 260 | + ) |
| 261 | + |
| 262 | + // Start the pool |
| 263 | + pool.Start(context.Background()) |
| 264 | + |
| 265 | + // The worker will be interrupted after 5 seconds with a timeout error |
| 266 | +} |
| 267 | +``` |
| 268 | + |
159 | 269 | ### Logging |
160 | 270 |
|
161 | 271 | You can use the [CtxLogger()](context.go) function to retrieve the |
|
0 commit comments