snapshot of https://github.com/ThreeDotsLabs/watermill.git (master, 2026-09-15; blobs > 131072B omitted)
Some checks failed
master / ci (push) Failing after 0s
Some checks failed
master / ci (push) Failing after 0s
This commit is contained in:
31
message/router/middleware/throttle.go
Normal file
31
message/router/middleware/throttle.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/ThreeDotsLabs/watermill/message"
|
||||
)
|
||||
|
||||
// Throttle provides a middleware that limits the amount of messages processed per unit of time.
|
||||
// This may be done e.g. to prevent excessive load caused by running a handler on a long queue of unprocessed messages.
|
||||
type Throttle struct {
|
||||
ticker *time.Ticker
|
||||
}
|
||||
|
||||
// NewThrottle creates a new Throttle middleware.
|
||||
// Example duration and count: NewThrottle(10, time.Second) for 10 messages per second
|
||||
func NewThrottle(count int64, duration time.Duration) *Throttle {
|
||||
return &Throttle{
|
||||
ticker: time.NewTicker(duration / time.Duration(count)),
|
||||
}
|
||||
}
|
||||
|
||||
// Middleware returns the Throttle middleware.
|
||||
func (t Throttle) Middleware(h message.HandlerFunc) message.HandlerFunc {
|
||||
return func(message *message.Message) ([]*message.Message, error) {
|
||||
// throttle is shared by multiple handlers, which will wait for their "tick"
|
||||
<-t.ticker.C
|
||||
|
||||
return h(message)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user