Documentation
¶
Index ¶
- func BoolPtr(v bool) *bool
- func DecisionTypePtr(t s.DecisionType) *s.DecisionType
- func EventTypePtr(t s.EventType) *s.EventType
- func Int32Ptr(v int32) *int32
- func Int64Ptr(v int64) *int64
- func StringPtr(v string) *string
- func TaskListPtr(v s.TaskList) *s.TaskList
- type TimeSource
- type TokenBucket
- type TokenBucketFactory
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DecisionTypePtr ¶
func DecisionTypePtr(t s.DecisionType) *s.DecisionType
DecisionTypePtr makes a copy and returns the pointer to a DecisionType.
func EventTypePtr ¶
EventTypePtr makes a copy and returns the pointer to a EventType.
Types ¶
type TimeSource ¶
TimeSource is an interface for any entity that provides the current time. Its primarily used to mock out timesources in unit test
func NewRealTimeSource ¶
func NewRealTimeSource() TimeSource
NewRealTimeSource returns a time source that servers real wall clock time using CLOCK_REALTIME
type TokenBucket ¶
type TokenBucket interface {
// TryConsume attempts to take count tokens from the
// bucket. Returns true on success, false
// otherwise along with the duration for the next refill
TryConsume(count int) (bool, time.Duration)
// Consume waits up to timeout duration to take count
// tokens from the bucket. Returns true if count
// tokens were acquired before timeout, false
// otherwise
Consume(count int, timeout time.Duration) bool
}
TokenBucket is the interface for any implememtation of a token bucket rate limiter
func NewTokenBucket ¶
func NewTokenBucket(rps int, timeSource TimeSource) TokenBucket
NewTokenBucket creates and returns a new token bucket rate limiter that repelenishes the bucket every 100 milliseconds. Thread safe.
@param rps
Desired rate per second
Golang.org has an alternative implementation of the rate limiter. On benchmarking, golang's implementation was order of magnitude slower. In addition, it does a lot more than what we need. These are the benchmarks under different scenarios
BenchmarkTokenBucketParallel 50000000 40.7 ns/op BenchmarkGolangRateParallel 10000000 150 ns/op BenchmarkTokenBucketParallel-8 20000000 124 ns/op BenchmarkGolangRateParallel-8 10000000 208 ns/op BenchmarkTokenBucketParallel 50000000 37.8 ns/op BenchmarkGolangRateParallel 10000000 153 ns/op BenchmarkTokenBucketParallel-8 10000000 129 ns/op BenchmarkGolangRateParallel-8 10000000 208 ns/op
type TokenBucketFactory ¶
type TokenBucketFactory interface {
CreateTokenBucket(rps int, timeSource TimeSource) TokenBucket
}
TokenBucketFactory is an interface mainly used for injecting mock implementation of TokenBucket for unit testing
func NewTokenBucketFactory ¶
func NewTokenBucketFactory() TokenBucketFactory
NewTokenBucketFactory creates an instance of factory used for creating TokenBucket instances