From 2f0b4036ed6eb6c2a9691c2a48703f2a16f1c21e Mon Sep 17 00:00:00 2001 From: James Alavosus Date: Tue, 12 Oct 2021 03:03:44 -0400 Subject: [PATCH 1/2] untested Wrap()/WrapContext() functions which wrap the wrappers. Woh. --- ratelimit.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/ratelimit.go b/ratelimit.go index 504d03d..4cd5172 100644 --- a/ratelimit.go +++ b/ratelimit.go @@ -33,6 +33,28 @@ type ( RateLimiterFn func() error ) +// Wrap wraps the passed function within another function, which no parameters +// and calls RateLimit using f(). Helpful for building functions which are "preloaded" with +// a rate limiter, rather than calling RateLimit around every function call. +func Wrap(f func() error, callsPerInterval int64, interval time.Duration) WrappedFn { + r := NewRateLimiter(callsPerInterval, interval) + + return func() error { + return r.RateLimit(f) + } +} + +// WrapContext wraps the passed function within another function, which takes a single context.Context parameter +// and calls RateLimitContext using f() and that context. Helpful for building functions which are "preloaded" with +// a rate limiter, rather than calling RateLimitContext around every function call. +func WrapContext(f func() error, callsPerInterval int64, interval time.Duration) WrappedFnContext { + r := NewRateLimiter(callsPerInterval, interval) + + return func(ctx context.Context) error { + return r.RateLimitContext(ctx, f) + } +} + // RateLimiter is a fancy little struct which can do everything you want it to and more // in the realm of rate limiting. type RateLimiter struct { From 3bec1a471bd090134b207abfb5eafa32560eea95 Mon Sep 17 00:00:00 2001 From: James Alavosus Date: Tue, 12 Oct 2021 03:26:52 -0400 Subject: [PATCH 2/2] Move Wrap() and WrapContext() to wrap.go for readability --- ratelimit.go | 22 ---------------------- wrap.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 22 deletions(-) create mode 100644 wrap.go diff --git a/ratelimit.go b/ratelimit.go index 4cd5172..504d03d 100644 --- a/ratelimit.go +++ b/ratelimit.go @@ -33,28 +33,6 @@ type ( RateLimiterFn func() error ) -// Wrap wraps the passed function within another function, which no parameters -// and calls RateLimit using f(). Helpful for building functions which are "preloaded" with -// a rate limiter, rather than calling RateLimit around every function call. -func Wrap(f func() error, callsPerInterval int64, interval time.Duration) WrappedFn { - r := NewRateLimiter(callsPerInterval, interval) - - return func() error { - return r.RateLimit(f) - } -} - -// WrapContext wraps the passed function within another function, which takes a single context.Context parameter -// and calls RateLimitContext using f() and that context. Helpful for building functions which are "preloaded" with -// a rate limiter, rather than calling RateLimitContext around every function call. -func WrapContext(f func() error, callsPerInterval int64, interval time.Duration) WrappedFnContext { - r := NewRateLimiter(callsPerInterval, interval) - - return func(ctx context.Context) error { - return r.RateLimitContext(ctx, f) - } -} - // RateLimiter is a fancy little struct which can do everything you want it to and more // in the realm of rate limiting. type RateLimiter struct { diff --git a/wrap.go b/wrap.go new file mode 100644 index 0000000..bdf2f50 --- /dev/null +++ b/wrap.go @@ -0,0 +1,28 @@ +package ratelimit + +import ( + "context" + "time" +) + +// Wrap wraps the passed function within another function, which no parameters +// and calls RateLimit using fn(). Helpful for building functions which are "preloaded" with +// a rate limiter, rather than calling RateLimit around every function call. +func Wrap(fn RateLimiterFn, callsPerInterval int64, interval time.Duration) WrappedFn { + r := NewRateLimiter(callsPerInterval, interval) + + return func() error { + return r.RateLimit(fn) + } +} + +// WrapContext wraps the passed function within another function, which takes a single context.Context parameter +// and calls RateLimitContext using fn() and that context. Helpful for building functions which are "preloaded" with +// a rate limiter, rather than calling RateLimitContext around every function call. +func WrapContext(fn RateLimiterFn, callsPerInterval int64, interval time.Duration) WrappedFnContext { + r := NewRateLimiter(callsPerInterval, interval) + + return func(ctx context.Context) error { + return r.RateLimitContext(ctx, fn) + } +}