Documentation ¶
Overview ¶
Package limiters provides a set of wrappers intended to restrict the amount of resources consumed by the server.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var (
ErrClosed = errors.New("limiters: Rate bucket is closed")
)
Functions ¶
This section is empty.
Types ¶
type BucketSet ¶
type BucketSet struct { // New function is used to construct underlying L instances. // // It is safe to change it only when BucketSet is not used by any // goroutine. New func() L // Time after which bucket is considered stale and can be removed from the // set. For safe use with Rate limiter, it should be at least as twice as // big as Rate refill interval. ReapInterval time.Duration MaxBuckets int // contains filtered or unexported fields }
BucketSet combines a group of Ls into a single key-indexed structure. Basically, each unique key gets its own counter. The main use case for BucketSet is to apply per-resource rate limiting.
Amount of buckets is limited to a certain value. When the size of internal map is around or equal to that value, next Take call will attempt to remove any stale buckets from the group. If it is not possible to do so (all buckets are in active use), Take will return false. Alternatively, in some rare cases, some other (undefined) waiting Take can return false.
A BucksetSet without a New function assigned is no-op: Take and TakeContext always succeed and Release does nothing.
func NewBucketSet ¶
type L ¶
type L interface { Take() bool TakeContext(context.Context) error Release() // Close frees any resources used internally by Limiter for book-keeping. Close() }
The L interface represents a blocking limiter that has some upper bound of resource use and blocks when it is exceeded until enough resources are freed.
type MultiLimit ¶
type MultiLimit struct {
Wrapped []L
}
MultiLimit wraps multiple L implementations into a single one, locking them in the specified order.
It does not implement any deadlock detection or avoidance algorithms.
func (*MultiLimit) Close ¶
func (ml *MultiLimit) Close()
func (*MultiLimit) Release ¶
func (ml *MultiLimit) Release()
func (*MultiLimit) Take ¶
func (ml *MultiLimit) Take() bool
func (*MultiLimit) TakeContext ¶
func (ml *MultiLimit) TakeContext(ctx context.Context) error
type Rate ¶
type Rate struct {
// contains filtered or unexported fields
}
Rate structure implements a basic rate-limiter for requests using the token bucket approach.
Take() is expected to be called before each request. Excessive calls will block. Timeouts can be implemented using the TakeContext method.
Rate.Close causes all waiting Take to return false. TakeContext returns ErrClosed in this case.
If burstSize = 0, all methods are no-op and always succeed.
type Semaphore ¶
type Semaphore struct {
// contains filtered or unexported fields
}
Semaphore is a convenience wrapper for a channel that implements semaphore-kind synchronization.
If the argument given to the NewSemaphore is negative or zero, all methods are no-op.