strategy

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: May 27, 2025 License: Apache-2.0 Imports: 7 Imported by: 21

Documentation

Overview

Package strategy provides common strategy implementations.

Index

Constants

View Source
const PartitionTagName = "partition"

PartitionTagName represents the metric tag used for the partition identifier

Variables

This section is empty.

Functions

This section is empty.

Types

type LookupPartition

type LookupPartition struct {
	MetricSampleListener core.MetricSampleListener
	// contains filtered or unexported fields
}

LookupPartition defines a partition for the LookupPartitionStrategy Note: generally speaking you shouldn't use this directly, instead use the higher level LookupPartitionStrategy

func NewLookupPartitionWithMetricRegistry

func NewLookupPartitionWithMetricRegistry(
	name string,
	percent float64,
	limit int32,
	registry core.MetricRegistry,
) *LookupPartition

NewLookupPartitionWithMetricRegistry will create a new LookupPartition

func (*LookupPartition) Acquire

func (p *LookupPartition) Acquire()

Acquire from the worker pool note: not to be used directly, not thread safe.

func (*LookupPartition) BusyCount

func (p *LookupPartition) BusyCount() int

BusyCount will return the current limit

func (*LookupPartition) IsLimitExceeded

func (p *LookupPartition) IsLimitExceeded() bool

IsLimitExceeded will return true of the number of requests in flight >= limit note: not thread safe.

func (*LookupPartition) Limit

func (p *LookupPartition) Limit() int

Limit will return the current limit

func (*LookupPartition) Name

func (p *LookupPartition) Name() string

Name will return the partition name, these are immutable.

func (*LookupPartition) Percent

func (p *LookupPartition) Percent() float64

Percent returns the partition percent, these are immutable

func (*LookupPartition) Release

func (p *LookupPartition) Release()

Release from the worker pool note: not to be used directly, not thread safe.

func (*LookupPartition) String

func (p *LookupPartition) String() string

func (*LookupPartition) UpdateLimit

func (p *LookupPartition) UpdateLimit(totalLimit int32)

UpdateLimit will update the current limit Calculate this bin's limit while rounding up and ensuring the value is at least 1. With this technique the sum of bin limits may end up being higher than the concurrency limit.

type LookupPartitionStrategy

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

LookupPartitionStrategy defines the strategy for partitioning the limiter by named groups where the allocation of group to percentage is provided up front.

func NewLookupPartitionStrategyWithMetricRegistry

func NewLookupPartitionStrategyWithMetricRegistry(
	partitions map[string]*LookupPartition,
	lookupFunc func(ctx context.Context) string,
	limit int32,
	registry core.MetricRegistry,
) (*LookupPartitionStrategy, error)

NewLookupPartitionStrategyWithMetricRegistry will create a new LookupPartitionStrategy

func (*LookupPartitionStrategy) AddPartition added in v0.1.7

func (s *LookupPartitionStrategy) AddPartition(name string, partition *LookupPartition) bool

AddPartition will dynamically add a partition will return false if this partition is already defined, otherwise true if successfully added

func (*LookupPartitionStrategy) BinBusyCount

func (s *LookupPartitionStrategy) BinBusyCount(key string) (int, error)

BinBusyCount will return the current bin's busy count

func (*LookupPartitionStrategy) BinLimit

func (s *LookupPartitionStrategy) BinLimit(key string) (int, error)

BinLimit will return the current bin's limit

func (*LookupPartitionStrategy) BusyCount

func (s *LookupPartitionStrategy) BusyCount() int

BusyCount will return the current busy count.

func (*LookupPartitionStrategy) Limit

func (s *LookupPartitionStrategy) Limit() int

Limit will return the current limit.

func (*LookupPartitionStrategy) RemovePartition added in v0.1.7

func (s *LookupPartitionStrategy) RemovePartition(name string) (int, bool)

RemovePartition will remove a given partition dynamically will return the busy count from that partition, along with true if the partition was found, otherwise false.

func (*LookupPartitionStrategy) SetLimit

func (s *LookupPartitionStrategy) SetLimit(limit int)

SetLimit will set a new limit for the LookupPartitionStrategy and it's partitions

func (*LookupPartitionStrategy) String

func (s *LookupPartitionStrategy) String() string

func (*LookupPartitionStrategy) TryAcquire

func (s *LookupPartitionStrategy) TryAcquire(ctx context.Context) (token core.StrategyToken, ok bool)

TryAcquire a token from a partition

type PreciseStrategy added in v0.4.0

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

PreciseStrategy strategy is much more strict than Simple strategy. It uses a sync.Mutex to keep track of of the number of inFlight requests instead of atomic accounting. The major difference is this adds additional contention for precise limiting, vs SimpleStrategy which trades off exact precision for speed.

func NewPreciseStrategy added in v0.4.0

func NewPreciseStrategy(limit int) *PreciseStrategy

NewPreciseStrategy will return a new PreciseStrategy.

func NewPreciseStrategyWithMetricRegistry added in v0.4.0

func NewPreciseStrategyWithMetricRegistry(limit int, registry core.MetricRegistry, tags ...string) *PreciseStrategy

NewPreciseStrategyWithMetricRegistry will create a new PreciseStrategy

func (*PreciseStrategy) GetBusyCount added in v0.4.0

func (s *PreciseStrategy) GetBusyCount() int

GetBusyCount will get the current busy count

func (*PreciseStrategy) GetLimit added in v0.4.0

func (s *PreciseStrategy) GetLimit() int

GetLimit will get the current limit

func (*PreciseStrategy) SetLimit added in v0.4.0

func (s *PreciseStrategy) SetLimit(limit int)

SetLimit will update the strategy with a new limit.

func (*PreciseStrategy) String added in v0.4.0

func (s *PreciseStrategy) String() string

func (*PreciseStrategy) TryAcquire added in v0.4.0

func (s *PreciseStrategy) TryAcquire(ctx context.Context) (token core.StrategyToken, ok bool)

TryAcquire will try to acquire a token from the limiter. context Context of the request for partitioned limits. returns not ok if limit is exceeded, or a StrategyToken that must be released when the operation completes.

type PredicatePartition

type PredicatePartition struct {
	MetricSampleListener core.MetricSampleListener
	// contains filtered or unexported fields
}

PredicatePartition defines a partition for the PredicatePartitionStrategy Note: generally speaking you shouldn't use this directly, instead use the higher level PredicatePartitionStrategy

func NewPredicatePartitionWithMetricRegistry

func NewPredicatePartitionWithMetricRegistry(
	name string,
	percent float64,
	predicateFunc func(ctx context.Context) bool,
	registry core.MetricRegistry,
) *PredicatePartition

NewPredicatePartitionWithMetricRegistry will create a new PredicatePartition

func (*PredicatePartition) Acquire

func (p *PredicatePartition) Acquire()

Acquire from the worker pool note: not to be used directly, not thread safe.

func (*PredicatePartition) BusyCount

func (p *PredicatePartition) BusyCount() int

BusyCount will return the current limit

func (*PredicatePartition) IsLimitExceeded

func (p *PredicatePartition) IsLimitExceeded() bool

IsLimitExceeded will return true of the number of requests in flight >= limit note: not thread safe.

func (*PredicatePartition) Limit

func (p *PredicatePartition) Limit() int

Limit will return the current limit

func (*PredicatePartition) Name

func (p *PredicatePartition) Name() string

Name will return the partition name, these are immutable.

func (*PredicatePartition) Percent

func (p *PredicatePartition) Percent() float64

Percent returns the partition percent, these are immutable

func (*PredicatePartition) Release

func (p *PredicatePartition) Release()

Release from the worker pool note: not to be used directly, not thread safe.

func (*PredicatePartition) String

func (p *PredicatePartition) String() string

func (*PredicatePartition) UpdateLimit

func (p *PredicatePartition) UpdateLimit(totalLimit int32)

UpdateLimit will update the current limit Calculate this bin's limit while rounding up and ensuring the value is at least 1. With this technique the sum of bin limits may end up being higher than the concurrency limit.

type PredicatePartitionStrategy

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

PredicatePartitionStrategy is a concurrency limiter that guarantees a certain percentage of the limit to specific callers while allowing callers to borrow from underutilized callers.

Callers are identified by their index into an array of percentages passed in during initialization. A percentage of 0.0 means that a caller may only use excess capacity and can be completely starved when the limit is reached. A percentage of 1.0 means that the caller is guaranteed to get the entire limit.

grpc.server.call.inflight (group=[a, b, c]) grpc.server.call.limit (group=[a,b,c])

func NewPredicatePartitionStrategyWithMetricRegistry

func NewPredicatePartitionStrategyWithMetricRegistry(
	partitions []*PredicatePartition,
	limit int32,
	registry core.MetricRegistry,
) (*PredicatePartitionStrategy, error)

NewPredicatePartitionStrategyWithMetricRegistry will create a new PredicatePartitionStrategy

func (*PredicatePartitionStrategy) AddPartition added in v0.1.7

func (s *PredicatePartitionStrategy) AddPartition(partition *PredicatePartition) bool

AddPartition will dynamically add a partition will return false if this partition is already defined, otherwise true if successfully added

func (*PredicatePartitionStrategy) BinBusyCount

func (s *PredicatePartitionStrategy) BinBusyCount(idx int) (int, error)

BinBusyCount will return the current bin's busy count

func (*PredicatePartitionStrategy) BinLimit

func (s *PredicatePartitionStrategy) BinLimit(idx int) (int, error)

BinLimit will return the current bin's limit

func (*PredicatePartitionStrategy) BusyCount

func (s *PredicatePartitionStrategy) BusyCount() int

BusyCount will return the current busy count.

func (*PredicatePartitionStrategy) Limit

func (s *PredicatePartitionStrategy) Limit() int

Limit will return the current limit.

func (*PredicatePartitionStrategy) RemovePartitionsMatching added in v0.1.7

func (s *PredicatePartitionStrategy) RemovePartitionsMatching(matcher context.Context) ([]*PredicatePartition, bool)

RemovePartitionsMatching will remove partitions dynamically will return the removed matching partitions, and true if there are at least 1 removed partition

func (*PredicatePartitionStrategy) SetLimit

func (s *PredicatePartitionStrategy) SetLimit(limit int)

SetLimit will set a new limit for the PredicatePartitionStrategy and it's partitions

func (*PredicatePartitionStrategy) String

func (s *PredicatePartitionStrategy) String() string

func (*PredicatePartitionStrategy) TryAcquire

TryAcquire a token from a partition

type SimpleStrategy

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

SimpleStrategy is the simplest strategy for enforcing a concurrency limit that has a single counter for tracking total usage.

func NewSimpleStrategy

func NewSimpleStrategy(limit int) *SimpleStrategy

NewSimpleStrategy will create a new SimpleStrategy

func NewSimpleStrategyWithMetricRegistry

func NewSimpleStrategyWithMetricRegistry(limit int, registry core.MetricRegistry, tags ...string) *SimpleStrategy

NewSimpleStrategyWithMetricRegistry will create a new SimpleStrategy

func (*SimpleStrategy) GetBusyCount

func (s *SimpleStrategy) GetBusyCount() int

GetBusyCount will get the current busy count

func (*SimpleStrategy) GetLimit

func (s *SimpleStrategy) GetLimit() int

GetLimit will get the current limit

func (*SimpleStrategy) SetLimit

func (s *SimpleStrategy) SetLimit(limit int)

SetLimit will update the strategy with a new limit.

func (*SimpleStrategy) String

func (s *SimpleStrategy) String() string

func (*SimpleStrategy) TryAcquire

func (s *SimpleStrategy) TryAcquire(ctx context.Context) (token core.StrategyToken, ok bool)

TryAcquire will try to acquire a token from the limiter. context Context of the request for partitioned limits. returns not ok if limit is exceeded, or a StrategyToken that must be released when the operation completes.

Directories

Path Synopsis
Package matchers provides basic matchers for partitioned type strategies.
Package matchers provides basic matchers for partitioned type strategies.

Jump to

Keyboard shortcuts

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