noop limiter & interface for limiter
This commit is contained in:
commit
e42fb9cf0d
@ -36,7 +36,7 @@ func (c *MgClient) WithLogger(logger BasicLogger) *MgClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// WithLimiter sets the provided limiter instance into the Client.
|
// WithLimiter sets the provided limiter instance into the Client.
|
||||||
func (c *MgClient) WithLimiter(limiter *TokensBucket) *MgClient {
|
func (c *MgClient) WithLimiter(limiter Limiter) *MgClient {
|
||||||
c.limiter = limiter
|
c.limiter = limiter
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
@ -7,11 +7,21 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// NoopLimiter implements Limiter but doesn't limit anything.
|
||||||
|
var NoopLimiter Limiter = &noopLimiter{}
|
||||||
|
|
||||||
type token struct {
|
type token struct {
|
||||||
rps atomic.Uint32
|
rps atomic.Uint32
|
||||||
lastUse atomic.Value
|
lastUse atomic.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Limiter implements some form of rate limiting.
|
||||||
|
type Limiter interface {
|
||||||
|
// Obtain the right to send a request. Should lock the execution if current goroutine needs to wait.
|
||||||
|
Obtain(string)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TokensBucket implements basic Limiter with fixed window and fixed amount of tokens per window.
|
||||||
type TokensBucket struct {
|
type TokensBucket struct {
|
||||||
maxRPS uint32
|
maxRPS uint32
|
||||||
tokens sync.Map
|
tokens sync.Map
|
||||||
@ -21,7 +31,8 @@ type TokensBucket struct {
|
|||||||
sleep sleeper
|
sleep sleeper
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTokensBucket(maxRPS uint32, unusedTokenTime, checkTokenTime time.Duration) *TokensBucket {
|
// NewTokensBucket constructs TokensBucket with provided parameters.
|
||||||
|
func NewTokensBucket(maxRPS uint32, unusedTokenTime, checkTokenTime time.Duration) Limiter {
|
||||||
bucket := &TokensBucket{
|
bucket := &TokensBucket{
|
||||||
maxRPS: maxRPS,
|
maxRPS: maxRPS,
|
||||||
unusedTokenTime: unusedTokenTime,
|
unusedTokenTime: unusedTokenTime,
|
||||||
@ -79,6 +90,11 @@ func (m *TokensBucket) deleteUnusedToken() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type noopLimiter struct{}
|
||||||
|
|
||||||
|
func (l *noopLimiter) Obtain(string) {}
|
||||||
|
|
||||||
|
// sleeper sleeps. This thing is necessary for tests.
|
||||||
type sleeper interface {
|
type sleeper interface {
|
||||||
Sleep(time.Duration)
|
Sleep(time.Duration)
|
||||||
}
|
}
|
||||||
|
12
v1/types.go
12
v1/types.go
@ -78,12 +78,12 @@ const (
|
|||||||
|
|
||||||
// MgClient type.
|
// MgClient type.
|
||||||
type MgClient struct {
|
type MgClient struct {
|
||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
Debug bool `json:"debug"`
|
Debug bool `json:"debug"`
|
||||||
httpClient *http.Client `json:"-"`
|
httpClient *http.Client `json:"-"`
|
||||||
logger BasicLogger `json:"-"`
|
logger BasicLogger `json:"-"`
|
||||||
limiter *TokensBucket `json:"-"`
|
limiter Limiter `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Channel type.
|
// Channel type.
|
||||||
|
Loading…
Reference in New Issue
Block a user