-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Wrap() and WrapContext() to wrap.go for readability
- Loading branch information
Showing
2 changed files
with
28 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |