MultipleWorkers creates n workers that are rate limited by limit. Use case is to have multiple workers per simulation node. ProcessFunc must be thread safe.
(processFunc ProcessFunc, n int, limit rate.Limit, burst int)
| 10 | // Use case is to have multiple workers per simulation node. |
| 11 | // ProcessFunc must be thread safe. |
| 12 | func MultipleWorkers(processFunc ProcessFunc, n int, limit rate.Limit, burst int) []ProcessFunc { |
| 13 | rateLimiter := rate.NewLimiter(limit, burst) |
| 14 | |
| 15 | process := make([]ProcessFunc, n) |
| 16 | for i := 0; i < n; i++ { |
| 17 | process[i] = func(ctx context.Context, data []byte, info QueueItemInfo) error { |
| 18 | err := rateLimiter.Wait(ctx) |
| 19 | if err != nil { |
| 20 | return err |
| 21 | } |
| 22 | return processFunc(ctx, data, info) |
| 23 | } |
| 24 | } |
| 25 | return process |
| 26 | } |
no outgoing calls