time: create ticker with instant first tick - rebooted #41885
Labels
Milestone
Comments
in this specific case package main
import (
"context"
"fmt"
"time"
"golang.org/x/time/rate"
)
var interval = time.Second
type limited struct {
l *rate.Limiter
f func() string
}
func (l *limited) do() string {
l.l.Wait(context.Background())
return l.f()
}
func main() {
r3 := limited{
l: rate.NewLimiter(rate.Every(interval), 1),
f: func() string { return "result" },
}
fmt.Println(r3.do())
fmt.Println(r3.do())
fmt.Println(r3.do())
} |
It seems to me that the |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have the same issue as #17601, but the issue was closed and locked with this reason:
However, "call the function once at the start" does not work when you are not in control of when the function is called.
Consider this clumsy implementation of a rate limiting in
rateLimited
, vs. the much more concise and elegant one inrateLimitedImmediate
:Just that the nice implementation in
rateLimited
cannot start immediately.I am not in control of calling the function myself, the library client does that.
So I second the need for a stdlib solution like proposed in the original issue, and hope the issue can be re-opened (or discussed here).
The text was updated successfully, but these errors were encountered: