-
Notifications
You must be signed in to change notification settings - Fork 0
/
ratelimit.go
59 lines (50 loc) · 1.79 KB
/
ratelimit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package paths
import (
"context"
"golang.org/x/time/rate"
"github.com/TosinShada/monorepo/support/errors"
"github.com/TosinShada/monorepo/xdr"
)
var (
// ErrRateLimitExceeded indicates that the Finder is not able to fulfill the request due to rate limits.
ErrRateLimitExceeded = errors.New("Rate limit exceeded")
)
// RateLimitedFinder is a Finder implementation which limits the number of path finding requests.
type RateLimitedFinder struct {
finder Finder
limiter *rate.Limiter
}
// NewRateLimitedFinder constructs a new RateLimitedFinder which enforces a per
// second limit on path finding requests.
func NewRateLimitedFinder(finder Finder, limit uint) *RateLimitedFinder {
return &RateLimitedFinder{
finder: finder,
limiter: rate.NewLimiter(rate.Limit(limit), int(limit)),
}
}
// Limit returns the per second limit of path finding requests.
func (f *RateLimitedFinder) Limit() int {
return f.limiter.Burst()
}
// Find implements the Finder interface and returns ErrRateLimitExceeded if the
// RateLimitedFinder is unable to complete the request due to rate limits.
func (f *RateLimitedFinder) Find(ctx context.Context, q Query, maxLength uint) ([]Path, uint32, error) {
if !f.limiter.Allow() {
return nil, 0, ErrRateLimitExceeded
}
return f.finder.Find(ctx, q, maxLength)
}
// FindFixedPaths implements the Finder interface and returns ErrRateLimitExceeded if the
// RateLimitedFinder is unable to complete the request due to rate limits.
func (f *RateLimitedFinder) FindFixedPaths(
ctx context.Context,
sourceAsset xdr.Asset,
amountToSpend xdr.Int64,
destinationAssets []xdr.Asset,
maxLength uint,
) ([]Path, uint32, error) {
if !f.limiter.Allow() {
return nil, 0, ErrRateLimitExceeded
}
return f.finder.FindFixedPaths(ctx, sourceAsset, amountToSpend, destinationAssets, maxLength)
}