simple rate limiter
This commit is contained in:
parent
d2af3b1183
commit
8b1bc57cd0
3 changed files with 9 additions and 2 deletions
|
|
@ -3,6 +3,7 @@ package rabbit
|
|||
import (
|
||||
"context"
|
||||
amqp "github.com/rabbitmq/amqp091-go"
|
||||
"golang.org/x/time/rate"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
|
@ -11,6 +12,8 @@ func runConsumer(ctx context.Context, queue *Client, msgCh chan []byte) {
|
|||
runCtx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
|
||||
limiter := rate.NewLimiter(rate.Every(time.Millisecond*500), 100)
|
||||
|
||||
deliveries, err := queue.Consume()
|
||||
if err != nil {
|
||||
log.Printf("Could not start consuming: %s\n", err)
|
||||
|
|
@ -44,11 +47,11 @@ func runConsumer(ctx context.Context, queue *Client, msgCh chan []byte) {
|
|||
case delivery := <-deliveries:
|
||||
msgCh <- delivery.Body
|
||||
log.Printf("Received message: %s\n", delivery.Body)
|
||||
|
||||
|
||||
if err = delivery.Ack(false); err != nil {
|
||||
log.Printf("Error acknowledging message: %s\n", err)
|
||||
}
|
||||
<-time.After(time.Second * 2)
|
||||
limiter.Wait(runCtx)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue