In my tests 2000 events per second appear to be fine, but there is an issue 
a magnitude larger with 20 000 events per second.

On Tuesday, February 1, 2022 at 10:50:29 AM UTC+1 Ian Davis wrote:

> On Mon, 31 Jan 2022, at 8:33 PM, Uli Kunitz wrote:
>
> Please have a look at golang.org/x/time/rate 
> <https://pkg.go.dev/golang.org/x/time/rate>. This package should solve 
> your problem.
>
> Here is an example with 10 events per second.
>
> func main() {
>         log.SetFlags(log.Lmicroseconds)
>
>         l := rate.NewLimiter(10, 1)
>         ctx := context.Background()
>         for {
>                 if err := l.Wait(ctx); err != nil {
>                         log.Fatal(err)
>                 }
>                 log.Print("event!")
>         }
> }
>
> The package is intended to rate external events, but the simple loop above 
> works as well. You can generate/handle events also in parallel threads if  
> one CPU is not sufficient. Note that for high frequencies it doesn't ensure 
> that the time intervals are exact, but it ensures that the rate limit is 
> maintained.
>
>
> I agree that this is the right solution for the OP task, but note that the 
> rate package is also affected by the same timer issues: 
> https://github.com/golang/go/issues/47084
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/a3c398f9-14b0-4834-9c56-c1f5a90845b5n%40googlegroups.com.

Reply via email to