tokenbucket

package
v0.0.0-...-e48efe8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 25, 2020 License: Apache-2.0 Imports: 6 Imported by: 4

Documentation

Overview

Package tokenbucket provides an interface of token bucket and its implementation.

Index

Constants

View Source
const (
	// DefaultTimeSlice is default interval to fill bucket.
	DefaultTimeSlice = 10 * time.Millisecond
	// DefaultGCInterval is default interval to collect garbege token.
	DefaultGCInterval = 1 * time.Second
	// DefaultRate is filled tokens per seccond.
	DefaultRate int32 = 100
	// DefaultBucketSize is default size of bucket.
	DefaultBucketSize = DefaultRate / 5
)

Variables

View Source
var (
	// DefaultConfig is the default configuration.
	DefaultConfig = defaultConfig{}
)

Functions

This section is empty.

Types

type BucketOption

type BucketOption struct {
	// Banned means whether the bucket is banned.
	// When the value is true,
	// size will be 0 and any requets for the bucket will be rejected.
	// Otherwise, the default size is used.
	Banned bool
	// Size is size of the bucket.
	// Default value is uesed when this value is 0
	// and this bucket is not banned.
	Size int32
	// Rate is rate of the bucket per second.
	// Default value is uesed when this value is 0.
	Rate int32
}

BucketOption is an option of bucket

type ChunkOption

type ChunkOption struct {
	Default BucketOption
	Buckets map[string]*BucketOption
}

ChunkOption is an option of chunk

type Config

type Config interface {
	// Rate returns a number of filled tokens.
	Rate(chunkKey, bucketKey string) int32
	// Overflow returns true when .
	Overflow(chunkKey, bucketKey string, tokens int32) bool
}

Config is a configuration of TokenBucket.

func NewFixedConfig

func NewFixedConfig(o *Option) Config

NewFixedConfig constructs a new configuration. The fixed config is read only after creation.

type MockConfig

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

MockConfig is a mock of Config interface

func NewMockConfig

func NewMockConfig(ctrl *gomock.Controller) *MockConfig

NewMockConfig creates a new mock instance

func (*MockConfig) EXPECT

func (m *MockConfig) EXPECT() *MockConfigMockRecorder

EXPECT returns an object that allows the caller to indicate expected use

func (*MockConfig) Overflow

func (m *MockConfig) Overflow(chunkKey, bucketKey string, tokens int32) bool

Overflow mocks base method

func (*MockConfig) Rate

func (m *MockConfig) Rate(chunkKey, bucketKey string) int32

Rate mocks base method

type MockConfigMockRecorder

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

MockConfigMockRecorder is the mock recorder for MockConfig

func (*MockConfigMockRecorder) Overflow

func (mr *MockConfigMockRecorder) Overflow(chunkKey, bucketKey, tokens interface{}) *gomock.Call

Overflow indicates an expected call of Overflow

func (*MockConfigMockRecorder) Rate

func (mr *MockConfigMockRecorder) Rate(chunkKey, bucketKey interface{}) *gomock.Call

Rate indicates an expected call of Rate

type MockTokenBucket

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

MockTokenBucket is a mock of TokenBucket interface

func NewMockTokenBucket

func NewMockTokenBucket(ctrl *gomock.Controller) *MockTokenBucket

NewMockTokenBucket creates a new mock instance

func (*MockTokenBucket) EXPECT

EXPECT returns an object that allows the caller to indicate expected use

func (*MockTokenBucket) Fill

func (m *MockTokenBucket) Fill()

Fill mocks base method

func (*MockTokenBucket) Take

func (m *MockTokenBucket) Take(chunkKey string, bucketKeys []string) (bool, error)

Take mocks base method

type MockTokenBucketMockRecorder

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

MockTokenBucketMockRecorder is the mock recorder for MockTokenBucket

func (*MockTokenBucketMockRecorder) Fill

Fill indicates an expected call of Fill

func (*MockTokenBucketMockRecorder) Take

func (mr *MockTokenBucketMockRecorder) Take(chunkKey, bucketKeys interface{}) *gomock.Call

Take indicates an expected call of Take

type Option

type Option struct {
	Interval time.Duration

	Default BucketOption
	Chunks  map[string]*ChunkOption
}

Option is an option of tokenbucket

type TimeSliceTokenBucket

type TimeSliceTokenBucket interface {
	TokenBucket
	Fill()
}

TimeSliceTokenBucket is an TokenBucket based on time slices.

func NewTimeSliceTokenBucket

func NewTimeSliceTokenBucket(config Config) TimeSliceTokenBucket

NewTimeSliceTokenBucket constructs a in-memory TokenBucket

type TimestampTokenBucket

type TimestampTokenBucket interface {
	TokenBucket
	GC()
}

TimestampTokenBucket is an TokenBucket based on timestamp.

func NewTimestampTokenBucket

func NewTimestampTokenBucket(config Config) TimestampTokenBucket

NewTimestampTokenBucket constructs a in-memory TokenBucket

type TokenBucket

type TokenBucket interface {
	Take(chunkKey string, bucketKeys []string) (bool, error)
}

TokenBucket is an algorithm used to control network traffic. This interface provices goroutine-safe methods.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL