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/36291963-efaa-4957-bb69-387dd1721487%40www.fastmail.com.