throttle

package
v0.19.3 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2024 License: Apache-2.0 Imports: 35 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultAppThrottleDuration = time.Hour
	DefaultThrottleRatio       = 1.0
)

Variables

View Source
var (
	ErrThrottlerNotOpen = errors.New("throttler not open")
)

NoSuchMetricCheckResult is a result returns when a metric is unknown

View Source
var StandardCheckFlags = &CheckFlags{}

StandardCheckFlags have no special hints

Functions

This section is empty.

Types

type CheckFlags

type CheckFlags struct {
	ReadCheck             bool
	OverrideThreshold     float64
	LowPriority           bool
	OKIfNotExists         bool
	SkipRequestHeartbeats bool
}

CheckFlags provide hints for a check

type CheckResult

type CheckResult struct {
	StatusCode      int     `json:"StatusCode"`
	Value           float64 `json:"Value"`
	Threshold       float64 `json:"Threshold"`
	Error           error   `json:"-"`
	Message         string  `json:"Message"`
	RecentlyChecked bool    `json:"RecentlyChecked"`
}

CheckResult is the result for an app inquiring on a metric. It also exports as JSON via the API

func NewCheckResult

func NewCheckResult(statusCode int, value float64, threshold float64, err error) *CheckResult

NewCheckResult returns a CheckResult

func NewErrorCheckResult

func NewErrorCheckResult(statusCode int, err error) *CheckResult

NewErrorCheckResult returns a check result that indicates an error

type Client added in v0.10.0

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

Client construct is used by apps who wish to consult with a throttler. It encapsulates the check/throttling/backoff logic

func NewBackgroundClient added in v0.10.0

func NewBackgroundClient(throttler *Throttler, appName throttlerapp.Name, checkType ThrottleCheckType) *Client

NewBackgroundClient creates a client suitable for background jobs, which have low priority over production traffic, e.g. migration, table pruning, vreplication

func NewProductionClient added in v0.10.0

func NewProductionClient(throttler *Throttler, appName throttlerapp.Name, checkType ThrottleCheckType) *Client

NewProductionClient creates a client suitable for foreground/production jobs, which have normal priority.

func (*Client) Throttle added in v0.10.0

func (c *Client) Throttle(ctx context.Context)

Throttle throttles until the throttler is satisfied, or until context is cancelled. The function sleeps between throttle checks. The function is not thread safe.

func (*Client) ThrottleCheckOK added in v0.10.0

func (c *Client) ThrottleCheckOK(ctx context.Context, overrideAppName throttlerapp.Name) (throttleCheckOK bool)

ThrottleCheckOK checks the throttler, and returns 'true' when the throttler is satisfied. It does not sleep. The function caches results for a brief amount of time, hence it's safe and efficient to be called very frequently. The function is not thread safe.

func (*Client) ThrottleCheckOKOrWait added in v0.10.0

func (c *Client) ThrottleCheckOKOrWait(ctx context.Context) bool

ThrottleCheckOKOrWait checks the throttler; if throttler is satisfied, the function returns 'true' immediately, otherwise it briefly sleeps and returns 'false'. The function is not thread safe.

func (*Client) ThrottleCheckOKOrWaitAppName added in v0.14.0

func (c *Client) ThrottleCheckOKOrWaitAppName(ctx context.Context, appName throttlerapp.Name) bool

ThrottleCheckOKOrWait checks the throttler; if throttler is satisfied, the function returns 'true' immediately, otherwise it briefly sleeps and returns 'false'. Non-empty appName overrides the default appName. The function is not thread safe.

type ThrottleCheckType added in v0.10.0

type ThrottleCheckType int // nolint:revive

ThrottleCheckType allows a client to indicate what type of check it wants to issue. See available types below.

const (
	// ThrottleCheckPrimaryWrite indicates a check before making a write on a primary server
	ThrottleCheckPrimaryWrite ThrottleCheckType = iota
	// ThrottleCheckSelf indicates a check on a specific server health
	ThrottleCheckSelf
)

type Throttler

type Throttler struct {
	MetricsThreshold atomic.Uint64
	// contains filtered or unexported fields
}

Throttler is the main entity in the throttling mechanism. This service runs, probes, collects data, aggregates, reads inventory, provides information, etc.

func NewThrottler

func NewThrottler(env tabletenv.Env, srvTopoServer srvtopo.Server, ts *topo.Server, cell string, heartbeatWriter heartbeat.HeartbeatWriter, tabletTypeFunc func() topodatapb.TabletType) *Throttler

NewThrottler creates a Throttler

func (*Throttler) AppRequestMetricResult

func (throttler *Throttler) AppRequestMetricResult(ctx context.Context, appName string, metricResultFunc base.MetricResultFunc, denyApp bool) (metricResult base.MetricResult, threshold float64)

AppRequestMetricResult gets a metric result in the context of a specific app

func (*Throttler) CheckByType added in v0.10.0

func (throttler *Throttler) CheckByType(ctx context.Context, appName string, remoteAddr string, flags *CheckFlags, checkType ThrottleCheckType) (checkResult *CheckResult)

CheckByType runs a check by requested check type

func (*Throttler) CheckIsOpen added in v0.17.0

func (throttler *Throttler) CheckIsOpen() error

CheckIsOpen checks if this throttler is ready to serve. If not, it returns an error.

func (*Throttler) Close

func (throttler *Throttler) Close()

Close frees resources

func (*Throttler) Disable added in v0.16.0

func (throttler *Throttler) Disable() bool

Disable deactivates the probes and associated operations. When disabled, the throttler responds to check queries with "200 OK" irrespective of lag or any other metrics.

func (*Throttler) Enable added in v0.16.0

func (throttler *Throttler) Enable() *sync.WaitGroup

Enable activates the throttler probes; when enabled, the throttler responds to check queries based on the collected metrics. The function returns a WaitGroup that can be used to wait for the throttler to be fully disabled, ie when the Operate() goroutine function terminates and caches are invalidated.

func (*Throttler) GetMetricsQuery added in v0.16.0

func (throttler *Throttler) GetMetricsQuery() string

func (*Throttler) GetMetricsThreshold added in v0.16.2

func (throttler *Throttler) GetMetricsThreshold() float64

func (*Throttler) InitDBConfig

func (throttler *Throttler) InitDBConfig(keyspace, shard string)

InitDBConfig initializes keyspace and shard

func (*Throttler) IsAppExempted added in v0.18.0

func (throttler *Throttler) IsAppExempted(appName string) bool

IsAppExempt

func (*Throttler) IsAppThrottled

func (throttler *Throttler) IsAppThrottled(appName string) bool

IsAppThrottled tells whether some app should be throttled. Assuming an app is throttled to some extend, it will randomize the result based on the throttle ratio

func (*Throttler) IsEnabled added in v0.16.0

func (throttler *Throttler) IsEnabled() bool

func (*Throttler) IsOpen added in v0.14.0

func (throttler *Throttler) IsOpen() bool

func (*Throttler) IsRunning added in v0.17.0

func (throttler *Throttler) IsRunning() bool

func (*Throttler) Open

func (throttler *Throttler) Open() error

Open opens database pool and initializes the schema

func (*Throttler) Operate

func (throttler *Throttler) Operate(ctx context.Context, wg *sync.WaitGroup)

Operate is the main entry point for the throttler operation and logic. It will run the probes, collect metrics, refresh inventory, etc.

func (*Throttler) RecentAppsMap

func (throttler *Throttler) RecentAppsMap() (result map[string](*base.RecentApp))

RecentAppsMap returns a (copy) map of apps which checked for throttling recently

func (*Throttler) Status

func (throttler *Throttler) Status() *ThrottlerStatus

Status exports a status breakdown

func (*Throttler) StoreMetricsThreshold added in v0.17.0

func (throttler *Throttler) StoreMetricsThreshold(threshold float64)

func (*Throttler) ThrottleApp

func (throttler *Throttler) ThrottleApp(appName string, expireAt time.Time, ratio float64, exempt bool) (appThrottle *base.AppThrottle)

ThrottleApp instructs the throttler to begin throttling an app, to some period and with some ratio.

func (*Throttler) ThrottledApps added in v0.14.0

func (throttler *Throttler) ThrottledApps() (result []base.AppThrottle)

ThrottledAppsSnapshot returns a snapshot (a copy) of current throttled apps

func (*Throttler) ThrottledAppsMap

func (throttler *Throttler) ThrottledAppsMap() (result map[string](*base.AppThrottle))

ThrottledAppsMap returns a (copy) map of currently throttled apps

func (*Throttler) UnthrottleApp

func (throttler *Throttler) UnthrottleApp(appName string) (appThrottle *base.AppThrottle)

UnthrottleApp cancels any throttling, if any, for a given app

func (*Throttler) WatchSrvKeyspaceCallback added in v0.16.0

func (throttler *Throttler) WatchSrvKeyspaceCallback(srvks *topodatapb.SrvKeyspace, err error) bool

type ThrottlerCheck

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

ThrottlerCheck provides methods for an app checking on metrics

func NewThrottlerCheck

func NewThrottlerCheck(throttler *Throttler) *ThrottlerCheck

NewThrottlerCheck creates a ThrottlerCheck

func (*ThrottlerCheck) AggregatedMetrics

func (check *ThrottlerCheck) AggregatedMetrics(ctx context.Context) map[string]base.MetricResult

AggregatedMetrics is a convenience access method into throttler's `aggregatedMetricsSnapshot`

func (*ThrottlerCheck) Check

func (check *ThrottlerCheck) Check(ctx context.Context, appName string, storeType string, storeName string, remoteAddr string, flags *CheckFlags) (checkResult *CheckResult)

Check is the core function that runs when a user wants to check a metric

func (*ThrottlerCheck) MetricsHealth

func (check *ThrottlerCheck) MetricsHealth() map[string](*base.MetricHealth)

MetricsHealth is a convenience access method into throttler's `metricsHealthSnapshot`

func (*ThrottlerCheck) SelfChecks

func (check *ThrottlerCheck) SelfChecks(ctx context.Context)

SelfChecks runs checks on all known metrics as if we were an app. This runs asynchronously, continuously, and independently of any user interaction

type ThrottlerStatus

type ThrottlerStatus struct {
	Keyspace string
	Shard    string

	IsLeader  bool
	IsOpen    bool
	IsEnabled bool
	IsDormant bool

	Query     string
	Threshold float64

	AggregatedMetrics map[string]base.MetricResult
	MetricsHealth     base.MetricHealthMap
}

ThrottlerStatus published some status values from the throttler

Directories

Path Synopsis
This codebase originates from https://github.com/github/freno, See https://github.com/github/freno/blob/master/LICENSE
This codebase originates from https://github.com/github/freno, See https://github.com/github/freno/blob/master/LICENSE

Jump to

Keyboard shortcuts

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