godropbox: github.com/dropbox/godropbox/rate_limiter Index | Files

package rate_limiter

import "github.com/dropbox/godropbox/rate_limiter"

Index

Package Files

rate_limiter.go

Constants

const TickInterval = 50 * time.Millisecond
const TicksPerSec = 20
const Unlimited = math.MaxFloat64

type MockRateLimiter Uses

type MockRateLimiter struct {
    // contains filtered or unexported fields
}

A mock rate limiter for external unittesting. The bucket is fill via the Tick call.

func NewMockRateLimiter Uses

func NewMockRateLimiter() *MockRateLimiter

func (MockRateLimiter) HasStopped Uses

func (l MockRateLimiter) HasStopped() bool

func (MockRateLimiter) MaxQuota Uses

func (l MockRateLimiter) MaxQuota() float64

This returns the leaky bucket's maximum capacity.

func (MockRateLimiter) Quota Uses

func (l MockRateLimiter) Quota() float64

This returns the current available quota.

func (MockRateLimiter) QuotaPerSec Uses

func (l MockRateLimiter) QuotaPerSec() float64

This returns the leaky bucket's fill rate.

func (MockRateLimiter) SetMaxQuota Uses

func (l MockRateLimiter) SetMaxQuota(q float64) error

This sets the leaky bucket's maximum capacity. The value must be non-negative.

func (*MockRateLimiter) SetQuota Uses

func (l *MockRateLimiter) SetQuota(q float64)

func (MockRateLimiter) SetQuotaPerSec Uses

func (l MockRateLimiter) SetQuotaPerSec(r float64) error

This sets the leaky bucket's fill rate. The value must be non-negative.

func (MockRateLimiter) Stop Uses

func (l MockRateLimiter) Stop()

Stop the rate limiter.

func (MockRateLimiter) Throttle Uses

func (l MockRateLimiter) Throttle(request float64) bool

This blocks until the request amount of resources is acquired. This returns false if the request can be satisfied immediately. Otherwise, this returns true.

NOTE: When maxQuota is zero, or when the rate limiter is stopped, this returns immediately.

func (*MockRateLimiter) Tick Uses

func (l *MockRateLimiter) Tick()

func (MockRateLimiter) TryThrottle Uses

func (l MockRateLimiter) TryThrottle(request float64) bool

type RateLimiter Uses

type RateLimiter interface {
    // This returns the leaky bucket's maximum capacity.
    MaxQuota() float64

    // This sets the leaky bucket's maximum capacity.  The value must be non-negative.
    // Zero means "throttle everything". Unlimited means "throttle nothing".
    SetMaxQuota(q float64) error

    // This returns the leaky bucket's fill rate.
    QuotaPerSec() float64

    // This sets the leaky bucket's fill rate.  The value must be non-negative.
    SetQuotaPerSec(r float64) error

    // This returns the current available quota.
    Quota() float64

    // This blocks until the request amount of resources is acquired.  This
    // returns false if the request can be satisfied immediately.  Otherwise, this
    // returns true.
    //
    // NOTE: When maxQuota is zero, or when the rate limiter is stopped,
    // this returns immediately.
    Throttle(request float64) bool

    // Like Throttle(), but does not block: returns false when NOT throttled,
    // and true when the request would have blocked under Throttle().
    TryThrottle(request float64) bool

    // Stop the rate limiter.
    Stop()

    HasStopped() bool
}

Interface for a thread-safe leaky bucket rate limiter.

func NewRateLimiter Uses

func NewRateLimiter(
    maxQuota float64,
    quotaPerSec float64) (
    RateLimiter,
    error)

func NewUnthrottledRateLimiter Uses

func NewUnthrottledRateLimiter() (RateLimiter, error)

Package rate_limiter imports 4 packages (graph). Updated 2017-09-23. Refresh now. Tools for package owners. This is an inactive package (no imports and no commits in at least two years).