Documentation ¶
Index ¶
- Constants
- type ConcurrencyLimiter
- type GlobalTokenCounter
- func (gtc *GlobalTokenCounter) Close() error
- func (gtc *GlobalTokenCounter) GetCapacity() float64
- func (gtc *GlobalTokenCounter) GetPassThrough() bool
- func (gtc *GlobalTokenCounter) Name() string
- func (gtc *GlobalTokenCounter) Return(ctx context.Context, label string, count float64, reqID string) (ok bool, err error)
- func (gtc *GlobalTokenCounter) SetCapacity(capacity float64)
- func (gtc *GlobalTokenCounter) SetPassThrough(passthrough bool)
- func (gtc *GlobalTokenCounter) Take(ctx context.Context, label string, count float64) (ok bool, _ time.Duration, remaining float64, current float64, reqID string)
- func (gtc *GlobalTokenCounter) TakeIfAvailable(ctx context.Context, label string, n float64) (ok bool, waitTime time.Duration, remaining float64, current float64, ...)
Constants ¶
const ( // TakeNFunction is the name of the function used to take N tokens from the global counter. TakeNFunction = "TakeN" // ReturnFunction is the name of the function used to return N tokens to the global counter. ReturnFunction = "Return" // CancelQueuedFunction is the name of the function used to cancel a queued request. CancelQueuedFunction = "CancelQueued" // CancelInflightFunction is the name of the function used to cancel an inflight request. CancelInflightFunction = "CancelInflight" // MinimumWaitTime is the minimum wait time for a request in the queue. This is used if the token rate is 0. MinimumWaitTime = 10 * time.Millisecond // MaximumWaitTime is the maximum wait time for a request in the queue. MaximumWaitTime = 5 * time.Second // WaitExpiryMargin is the margin for expiry in addition to wait time for a request in the queue. WaitExpiryMargin = 100 * time.Millisecond // TokenRateWindowSize is the number of requests after which token rate is updated. TokenRateWindowSize = 10 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ConcurrencyLimiter ¶
type ConcurrencyLimiter interface { Name() string TakeIfAvailable(ctx context.Context, label string, count float64) (ok bool, waitTime time.Duration, remaining float64, current float64, reqID string) Take(ctx context.Context, label string, count float64) (ok bool, waitTime time.Duration, remaining float64, current float64, reqID string) Return(ctx context.Context, label string, count float64, reqID string) (ok bool, err error) SetPassThrough(passthrough bool) SetCapacity(capacity float64) GetPassThrough() bool Close() error }
ConcurrencyLimiter is a generic limiter interface.
type GlobalTokenCounter ¶
type GlobalTokenCounter struct {
// contains filtered or unexported fields
}
GlobalTokenCounter is a global token counter that uses a distributed map to store the state.
func NewGlobalTokenCounter ¶
func NewGlobalTokenCounter( dc *distcache.DistCache, name string, maxIdleDuration time.Duration, maxInflightDuration time.Duration, ) (*GlobalTokenCounter, error)
NewGlobalTokenCounter creates a new GlobalTokenCounter.
func (*GlobalTokenCounter) Close ¶
func (gtc *GlobalTokenCounter) Close() error
Close closes the global token counter.
func (*GlobalTokenCounter) GetCapacity ¶
func (gtc *GlobalTokenCounter) GetCapacity() float64
GetCapacity returns the capacity of the global token counter.
func (*GlobalTokenCounter) GetPassThrough ¶
func (gtc *GlobalTokenCounter) GetPassThrough() bool
GetPassThrough returns the current value of passThrough.
func (*GlobalTokenCounter) Name ¶
func (gtc *GlobalTokenCounter) Name() string
Name returns the name of the global token counter.
func (*GlobalTokenCounter) Return ¶
func (gtc *GlobalTokenCounter) Return(ctx context.Context, label string, count float64, reqID string) (ok bool, err error)
Return returns N tokens to the global token counter.
func (*GlobalTokenCounter) SetCapacity ¶
func (gtc *GlobalTokenCounter) SetCapacity(capacity float64)
SetCapacity sets the capacity of the global token counter.
func (*GlobalTokenCounter) SetPassThrough ¶
func (gtc *GlobalTokenCounter) SetPassThrough(passthrough bool)
SetPassThrough sets the value of passThrough.
func (*GlobalTokenCounter) Take ¶
func (gtc *GlobalTokenCounter) Take(ctx context.Context, label string, count float64) (ok bool, _ time.Duration, remaining float64, current float64, reqID string)
Take takes N tokens from the global token counter.
func (*GlobalTokenCounter) TakeIfAvailable ¶
func (gtc *GlobalTokenCounter) TakeIfAvailable(ctx context.Context, label string, n float64) (ok bool, waitTime time.Duration, remaining float64, current float64, reqID string)
TakeIfAvailable takes N tokens from the global token counter if they are available.