Skip to content

Instantly share code, notes, and snippets.

@dipankardas011
Created July 14, 2024 09:36
Show Gist options
  • Save dipankardas011/2a8049aa618528938c04df4c5323822e to your computer and use it in GitHub Desktop.
Save dipankardas011/2a8049aa618528938c04df4c5323822e to your computer and use it in GitHub Desktop.
control the rate of messages
package main
import (
"fmt"
"sync/atomic"
"time"
)
func smoothOutListening(rc <-chan string) {
var ops atomic.Uint64
var secondaryRateLimit atomic.Uint64
for _rc := range rc {
ops.Add(1)
if ops.Load() > rateLimit {
secondaryRateLimit.Add(1)
fmt.Printf("Rate limit exceeded, waiting for %d seconds\n", secondaryRateLimit.Load())
t := time.NewTicker(time.Duration(secondaryRateLimit.Load()) * time.Second)
<-t.C
ops.Store(0)
}
fmt.Println("Do some job" + _rc)
}
}
const rateLimit = 10
func Listen(c <-chan string, ratelimitedCh chan<- string) {
go func() {
for _c := range c {
ratelimitedCh <- _c
}
}()
}
func main() {
listerCh := make(chan string)
rateLimitCh := make(chan string, rateLimit)
go smoothOutListening(rateLimitCh)
Listen(listerCh, rateLimitCh)
for i := 0; i < 1e4; i++ {
listerCh <- fmt.Sprintf("[%d]: Hello\n", i)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment