Documentation
¶
Index ¶
- Variables
- func WithKey(ctx context.Context, key string) context.Context
- func WithMarshaler(ctx context.Context, mrsh Marshaler) context.Context
- func WithMessage(ctx context.Context, message interface{}) context.Context
- func WithParams(ctx context.Context, ts time.Time, priority uint8, weight int64, key string, ...) context.Context
- func WithPriority(ctx context.Context, priority uint8) context.Context
- func WithThrottler(ctx context.Context, thr Throttler, freq time.Duration) context.Context
- func WithTimestamp(ctx context.Context, ts time.Time) context.Context
- func WithWeight(ctx context.Context, weight int64) context.Context
- type Enqueuer
- type ErrorInternal
- type ErrorThreshold
- type Generator
- type Logger
- type Marshaler
- type Metric
- type Monitor
- type Pattern
- type Runnable
- type Runner
- type Stats
- type Throttler
- func NewThrottlerAdaptive(threshold uint64, interval time.Duration, quantum time.Duration, step uint64, ...) Throttler
- func NewThrottlerAfter(threshold uint64) Throttler
- func NewThrottlerAll(thrs ...Throttler) Throttler
- func NewThrottlerAny(thrs ...Throttler) Throttler
- func NewThrottlerBefore(threshold uint64) Throttler
- func NewThrottlerBucket(threshold uint64, interval time.Duration, monotone bool) Throttler
- func NewThrottlerBuffered(threshold uint64) Throttler
- func NewThrottlerCache(thr Throttler, cache time.Duration) Throttler
- func NewThrottlerCellRate(threshold uint64, interval time.Duration, monotone bool) Throttler
- func NewThrottlerChance(threshold float64) Throttler
- func NewThrottlerContext() Throttler
- func NewThrottlerEach(threshold uint64) Throttler
- func NewThrottlerEcho(err error) Throttler
- func NewThrottlerEnqueue(enq Enqueuer) Throttler
- func NewThrottlerFuture(threshold time.Time) Throttler
- func NewThrottlerGenerator(gen Generator, capacity uint64, eviction float64) Throttler
- func NewThrottlerJitter(initial time.Duration, limit time.Duration, reset bool, jitter float64) Throttler
- func NewThrottlerLatency(threshold time.Duration, retention time.Duration) Throttler
- func NewThrottlerMetric(mtc Metric) Throttler
- func NewThrottlerMonitor(mnt Monitor, threshold Stats) Throttler
- func NewThrottlerNot(thr Throttler) Throttler
- func NewThrottlerPanic() Throttler
- func NewThrottlerPast(threshold time.Time) Throttler
- func NewThrottlerPattern(patterns ...Pattern) Throttler
- func NewThrottlerPercentile(threshold time.Duration, capacity uint8, percentile float64, ...) Throttler
- func NewThrottlerPriority(threshold uint64, levels uint8) Throttler
- func NewThrottlerRetry(thr Throttler, retries uint64, onthreshold bool) Throttler
- func NewThrottlerRing(thrs ...Throttler) Throttler
- func NewThrottlerRunning(threshold uint64) Throttler
- func NewThrottlerSemaphore(weight int64) Throttler
- func NewThrottlerSquare(initial time.Duration, limit time.Duration, reset bool) Throttler
- func NewThrottlerSuppress(thr Throttler) Throttler
- func NewThrottlerTimed(threshold uint64, interval time.Duration, quantum time.Duration) Throttler
- func NewThrottlerWait(duration time.Duration) Throttler
Constants ¶
This section is empty.
Variables ¶
var DefaultRetriedDuration = 100 * time.Millisecond
DefaultRetriedDuration defines default initial duration for `square` throttler used by default. By default DefaultRetriedDuration is set to use `100 * time.Millisecond`.
Functions ¶
func WithKey ¶
WithKey adds the provided key to the provided context to add additional call identifier to context. Resulted context is used by: `pattern` and `generator` throtttlers.
func WithMarshaler ¶
WithMarshaler adds the provided marshaler to the provided context to add additional message marshaler that need to be used to context. Resulted context is used by: `enqueue` throtttler. Used in pair with `WithMessage`.
func WithMessage ¶
WithMessage adds the provided message to the provided context to add additional message that need to be used to context. Resulted context is used by: `enqueue` throtttler. Used in pair with `WithMarshaler`.
func WithParams ¶
func WithParams( ctx context.Context, ts time.Time, priority uint8, weight int64, key string, message interface{}, marshaler Marshaler, ) context.Context
WithParams facade call that respectively calls: - `WithTimestamp` - `WithPriority` - `WithWeight` - `WithKey` - `WithMessage` - `WithMarshaler`
func WithPriority ¶
WithPriority adds the provided priority to the provided context to differ `Acquire` priority levels. Resulted context is used by: `priority` throtttler.
func WithThrottler ¶
WithThrottler adds the provided thr to the provided context and defines context implementation that uses parrent context plus throttler internally that closes context done chanel if internal throttler throttles.
func WithTimestamp ¶
WithTimestamp adds the provided timestamp to the provided context to determine latency between `Acquire` and `Release`. Resulted context is used by: `latency` and `percentile` throtttlers.
func WithWeight ¶ added in v0.7.0
WithWeight adds the provided weight to the provided context to differ `Acquire` weight levels. Resulted context is used by: `before`, `after`, `timed`, `adaptive`, `semaphore`, `cellrate` and `bucket` throtttlers.
Types ¶
type Enqueuer ¶
type Enqueuer interface { // Enqueue enqueues provided message or returns internal error if any happened. Enqueue(context.Context, []byte) error }
Enqueuer defines abstract message enqueuing interface.
func NewEnqueuerKafka ¶
NewEnqueuerKafka creates Kafka enqueuer instance with cached connection and failure retries which enqueues provided message to the specified topic. New unique message key `gohalt_enqueue_{{uuid}}` is created for each new message. Only successful connections are cached.
func NewEnqueuerRabbit ¶
NewEnqueuerRabbit creates RabbitMQ enqueuer instance with cached connection and failure retries which enqueues provided message to the specified queue. New unique exchange `gohalt_exchange_{{uuid}}` is created for each new enqueuer, new unique message id `gohalt_enqueue_{{uuid}}` is created for each new message. Only successful connections are cached.
type ErrorInternal ¶ added in v0.4.0
ErrorInternal defines error type that occurs if throttler internal error happens.
func (ErrorInternal) Error ¶ added in v0.4.0
func (err ErrorInternal) Error() string
type ErrorThreshold ¶ added in v0.4.0
ErrorThreshold defines error type that occurs if throttler reaches specified threshold.
func (ErrorThreshold) Error ¶ added in v0.4.0
func (err ErrorThreshold) Error() string
type Generator ¶ added in v0.6.0
Generator defines func signature that is able to generate new throttlers by provided key.
type Logger ¶
type Logger func(string, ...interface{})
Logger defined by typical logger func signature.
type Metric ¶
type Metric interface { // Query returns the result of the query or internal error if any happened. Query(context.Context) (bool, error) }
Metric defines single metric querier interface that returns the metric query result.
func NewMetricPrometheus ¶
NewMetricPrometheus creates prometheus metric querier instance with cache interval defined by the provided duration which executes provided prometheus boolean metric query and cache it. Only successful metric results are cached.
type Monitor ¶
type Monitor interface { // Stats returns system stats or internal error if any happened. Stats(context.Context) (Stats, error) }
Monitor defines system monitor interface that returns the system stats.
type Runnable ¶
Runnable defined by typical abstract async func signature. Runnable is used by `Runner` as a subject for execution.
type Runner ¶
type Runner interface { // Run executes single prodived `Runnable` instance. Run(Runnable) // Result returns possible execution error back. Result() error }
Runner defines abstraction to execute a set of `Runnable` and return possible execution error back. Runner is designed to simplify work with throttlers by managing `Acquire`/`Release` loop.
func NewRunnerAsync ¶
NewRunnerAsync creates asynchronous runner instance that runs a set of `Runnable` simultaneously with regard to the provided context and throttler. First occurred error is returned from result.
type Stats ¶
Stats defines typical set of metrics returned by system monitor: - MEMAlloc shows how many bytes are allocated by heap objects. - MEMSystem shows how many bytes are obtained from the OS. - CPUPause shows average GC stop-the-world pause in nanoseconds. - CPUUsage shows average CPU utilization in percents.
type Throttler ¶
type Throttler interface { // Acquire takes a part of throttling quota or returns error if throttling quota is drained // it needs to be called right before shared resource acquire. Acquire(context.Context) error // Release puts a part of throttling quota back or returns error if this is not possible // it needs to be called just after shared resource release. Release(context.Context) error }
Throttler defines core gohalt throttler abstraction and exposes pair of counterpart methods: `Acquire` and `Release`.
func NewThrottlerAdaptive ¶
func NewThrottlerAdaptive( threshold uint64, interval time.Duration, quantum time.Duration, step uint64, thr Throttler, ) Throttler
NewThrottlerAdaptive creates new throttler instance that throttles each call which exeeds the running quota acquired - release q defined by the specified threshold in the specified interval. Periodically each specified interval the running quota number is reseted. If quantum is set then quantum will be used instead of interval to provide the running quota delta updates. Provided adapted throttler adjusts the running quota of adapter throttler by changing the value by d defined by the specified step, it subtracts *d^2* from the running quota if adapted throttler throttles or adds *d* to the running quota if it doesn't. Use `WithWeight` to override context call qunatity, 1 by default. - could return `ErrorThreshold`;
func NewThrottlerAfter ¶
NewThrottlerAfter creates new throttler instance that throttles each call after the i-th call defined by the specified threshold. Use `WithWeight` to override context call qunatity, 1 by default. - could return `ErrorThreshold`;
func NewThrottlerAll ¶
NewThrottlerAll creates new throttler instance that throttles call if all provided throttlers throttle. - could return `ErrorInternal`;
func NewThrottlerAny ¶
NewThrottlerAny creates new throttler instance that throttles call if any of provided throttlers throttle. - could return `ErrorInternal`;
func NewThrottlerBefore ¶
NewThrottlerBefore creates new throttler instance that throttles each call below the i-th call defined by the specified threshold. Use `WithWeight` to override context call qunatity, 1 by default. - could return `ErrorThreshold`;
func NewThrottlerBucket ¶ added in v0.9.0
NewThrottlerBucket creates new throttler instance that uses leaky bucket algorithm to throttles call within provided interval and threshold. If provided monotone flag is set class to release will have no effect on throttler. Use `WithWeight` to override context call qunatity, 1 by default. - could return `ErrorThreshold`;
func NewThrottlerBuffered ¶
NewThrottlerBuffered creates new throttler instance that waits on call which exeeds the running quota acquired - release q defined by the specified threshold until the running quota is available again.
func NewThrottlerCache ¶ added in v0.2.0
NewThrottlerCache creates new throttler instance that caches provided throttler calls for the provided cache duration, throttler release resulting resets cache. Only non throttling calls are cached for the provided cache duration. - could return any underlying throttler error;
func NewThrottlerCellRate ¶ added in v0.8.0
NewThrottlerCellRate creates new throttler instance that uses generic cell rate algorithm to throttles call within provided interval and threshold. If provided monotone flag is set class to release will have no effect on throttler. Use `WithWeight` to override context call qunatity, 1 by default. - could return `ErrorThreshold`;
func NewThrottlerChance ¶
NewThrottlerChance creates new throttler instance that throttles each call with the chance p defined by the specified threshold. Chance value is normalized to [0.0, 1.0] range. Implementation uses secure `crypto/rand` as PRNG function. - could return `ErrorThreshold`;
func NewThrottlerContext ¶
func NewThrottlerContext() Throttler
NewThrottlerContext creates new throttler instance that always throttless on done context. - could return `ErrorInternal`;
func NewThrottlerEach ¶
NewThrottlerEach creates new throttler instance that throttles each periodic i-th call defined by the specified threshold. - could return `ErrorThreshold`;
func NewThrottlerEcho ¶
NewThrottlerEcho creates new throttler instance that always throttles with the specified error back. - could return any specified error;
func NewThrottlerEnqueue ¶
NewThrottlerEnqueue creates new throttler instance that always enqueues message to the specified queue throttles only if any internal error occurred. Use `WithMessage` to specify context message for enqueued message and `WithMarshaler` to specify context message marshaler. Builtin `Enqueuer` implementations come with connection reuse and retries by default. Use builtin `NewEnqueuerRabbit` to create RabbitMQ enqueuer instance or `NewEnqueuerKafka` to create Kafka enqueuer instance. - could return `ErrorInternal`;
func NewThrottlerFuture ¶ added in v0.5.0
NewThrottlerFuture creates new throttler instance that throttles each call after timestamp defined by the specified UTC time threshold. - could return `ErrorThreshold`;
func NewThrottlerGenerator ¶ added in v0.6.0
NewThrottlerGenerator creates new throttler instance that throttles if found key matching throttler throttles. If no key matching throttler has been found generator see `Generator` used insted to provide new throttler that will be added to existing throttlers map. Generated throttlers are kept in bounded map with capacity c defined by the specified capacity and eviction rate e defined by specified eviction value is normalized to [0.0, 1.0], where eviction rate affects number of throttlers that will be removed from the map after bounds overflow. Use `WithKey` to specify key for throttler matching and generation. - could return `ErrorInternal`; - could return any underlying throttler error;
func NewThrottlerJitter ¶ added in v0.2.0
func NewThrottlerJitter(initial time.Duration, limit time.Duration, reset bool, jitter float64) Throttler
NewThrottlerJitter creates new throttler instance that waits accordingly to undelying square throttler but also adds the provided jitter delta distribution on top. Jitter value is normalized to [0.0, 1.0] range and defines which part of square delay could be randomized in percents. Implementation uses secure `crypto/rand` as PRNG function.
func NewThrottlerLatency ¶
NewThrottlerLatency creates new throttler instance that throttles each call after the call latency l defined by the specified threshold was exeeded once. If retention is set then throttler state will be reseted after retention duration. Use `WithTimestamp` to specify running duration between throttler acquire and release. - could return `ErrorThreshold`;
func NewThrottlerMetric ¶
NewThrottlerMetric creates new throttler instance that throttles call ifboolean metric defined by the specified boolean metric is reached or if any internal error occurred. Builtin `Metric` implementations come with boolean metric caching by default. Use builtin `NewMetricPrometheus` to create Prometheus metric instance. - could return `ErrorInternal`; - could return `ErrorThreshold`;
func NewThrottlerMonitor ¶
NewThrottlerMonitor creates new throttler instance that throttles call if any of the stats returned by provided monitor exceeds any of the stats defined by the specified threshold or if any internal error occurred. Builtin `Monitor` implementations come with stats caching by default. Use builtin `NewMonitorSystem` to create go system monitor instance. - could return `ErrorInternal`; - could return `ErrorThreshold`;
func NewThrottlerNot ¶
NewThrottlerNot creates new throttler instance that throttles call if provided throttler doesn't throttle. - could return `ErrorInternal`;
func NewThrottlerPanic ¶
func NewThrottlerPanic() Throttler
NewThrottlerPanic creates new throttler instance that always panics with `ErrorInternal`.
func NewThrottlerPast ¶ added in v0.5.0
NewThrottlerPast creates new throttler instance that throttles each call before timestamp defined by the specified UTC time threshold. - could return `ErrorThreshold`;
func NewThrottlerPattern ¶
NewThrottlerPattern creates new throttler instance that throttles if matching throttler from provided patterns throttles. Use `WithKey` to specify key for regexp pattern throttler matching. See `Pattern` which defines a pair of regexp and related throttler. - could return `ErrorInternal`; - could return any underlying throttler error;
func NewThrottlerPercentile ¶
func NewThrottlerPercentile( threshold time.Duration, capacity uint8, percentile float64, retention time.Duration, ) Throttler
NewThrottlerPercentile creates new throttler instance that throttles each call after the call latency l defined by the specified threshold was exeeded once considering the specified percentile. Percentile values are kept in bounded buffer with capacity c defined by the specified capacity. If retention is set then throttler state will be reseted after retention duration. Use `WithTimestamp` to specify running duration between throttler acquire and release. - could return `ErrorThreshold`;
func NewThrottlerPriority ¶
NewThrottlerPriority creates new throttler instance that waits on call which exeeds the running quota acquired - release q defined by the specified threshold until the running quota is available again. Running quota is not equally distributed between n levels of priority defined by the specified levels. Use `WithPriority` to override context call priority, 1 by default.
func NewThrottlerRetry ¶ added in v0.2.0
NewThrottlerRetry creates new throttler instance that retries provided throttler error up until the provided retries threshold. If provided onthreshold flag is set even `ErrorThreshold` errors will be retried. Internally retry uses square throttler with `DefaultRetriedDuration` initial duration. - could return any underlying throttler error;
func NewThrottlerRing ¶
NewThrottlerRing creates new throttler instance that throttles if the i-th call throttler from provided list throttle. - could return `ErrorInternal`; - could return any underlying throttler error;
func NewThrottlerRunning ¶
NewThrottlerRunning creates new throttler instance that throttles each call which exeeds the running quota acquired - release q defined by the specified threshold. - could return `ErrorThreshold`;
func NewThrottlerSemaphore ¶ added in v0.7.0
NewThrottlerSemaphore creates new throttler instance that throttles call if underlying semaphore throttles. Use `WithWeight` to override context call weight, 1 by default. - could return `ErrorThreshold`;
func NewThrottlerSquare ¶
NewThrottlerSquare creates new throttler instance that always waits for square growing [1, 4, 9, 16, ...] multiplier on the specified initial duration, up until the specified duration limit is reached. If reset is set then after throttler riches the specified duration limit next multiplier value will be reseted.
func NewThrottlerSuppress ¶
NewThrottlerSuppress creates new throttler instance that suppresses provided throttler to never throttle.
func NewThrottlerTimed ¶
NewThrottlerTimed creates new throttler instance that throttles each call which exeeds the running quota acquired - release q defined by the specified threshold in the specified interval. Periodically each specified interval the running quota number is reseted. If quantum is set then quantum will be used instead of interval to provide the running quota delta updates. Use `WithWeight` to override context call qunatity, 1 by default. - could return `ErrorThreshold`;
func NewThrottlerWait ¶
NewThrottlerWait creates new throttler instance that always waits for the specified duration.