throttle

package
v0.21.0-rc2 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2024 License: Apache-2.0 Imports: 30 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

Functions

func ResponseCodeFromStatus

ResponseCodeFromStatus returns a ResponseCode based on either given response code or HTTP status code. It is used to handle the transition period from v20 to v21 where v20 only returns HTTP status code. In v22 and beyond, the HTTP status code will be removed, and so will this function.

Types

type CheckFlags

type CheckFlags struct {
	Scope                 base.Scope
	ReadCheck             bool
	OverrideThreshold     float64
	OKIfNotExists         bool
	SkipRequestHeartbeats bool
	MultiMetricsEnabled   bool
}

CheckFlags provide hints for a check

type CheckResult

type CheckResult struct {
	ResponseCode    tabletmanagerdatapb.CheckThrottlerResponseCode `json:"ResponseCode"`
	StatusCode      int                                            `json:"StatusCode"`
	Value           float64                                        `json:"Value"`
	Threshold       float64                                        `json:"Threshold"`
	Error           error                                          `json:"-"`
	Message         string                                         `json:"Message"`
	RecentlyChecked bool                                           `json:"RecentlyChecked"`
	AppName         string                                         `json:"AppName"`
	MetricName      string                                         `json:"MetricName"`
	Scope           string                                         `json:"Scope"`
	Metrics         map[string]*MetricResult                       `json:"Metrics"` // New in multi-metrics support. Will eventually replace the above fields.
}

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

func NewCheckResult

func NewCheckResult(responseCode tabletmanagerdatapb.CheckThrottlerResponseCode, statusCode int, value float64, threshold float64, appName string, err error) *CheckResult

NewCheckResult returns a CheckResult

func NewErrorCheckResult

func NewErrorCheckResult(responseCode tabletmanagerdatapb.CheckThrottlerResponseCode, statusCode int, err error) *CheckResult

NewErrorCheckResult returns a check result that indicates an error

func (*CheckResult) IsOK

func (c *CheckResult) IsOK() bool

func (*CheckResult) Summary

func (c *CheckResult) Summary() string

Summary returns a human-readable summary of the check result

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, scope base.Scope) *Client

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

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) (checkResult *CheckResult, 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) (checkResult *CheckResult, throttleCheckOK 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) (checkResult *CheckResult, throttleCheckOK 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 MetricResult

type MetricResult struct {
	ResponseCode tabletmanagerdatapb.CheckThrottlerResponseCode `json:"ResponseCode"`
	StatusCode   int                                            `json:"StatusCode"`
	Scope        string                                         `json:"Scope"`
	Value        float64                                        `json:"Value"`
	Threshold    float64                                        `json:"Threshold"`
	Error        error                                          `json:"-"`
	Message      string                                         `json:"Message"`
	AppName      string                                         `json:"AppName"`
}

func (*MetricResult) IsOK

func (m *MetricResult) IsOK() bool

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, tabletAlias *topodatapb.TabletAlias, 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, matchedApp string)

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

func (*Throttler) Check

func (throttler *Throttler) Check(ctx context.Context, appName string, metricNames base.MetricNames, flags *CheckFlags) (checkResult *CheckResult)

Check 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) GetCustomMetricsQuery

func (throttler *Throttler) GetCustomMetricsQuery() string

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, string)

IsAppExempt

func (*Throttler) IsAppThrottled

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

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) MetricName

func (throttler *Throttler) MetricName(s string) base.MetricName

func (*Throttler) MetricNames

func (throttler *Throttler) MetricNames(s []string) base.MetricNames

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) 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, scope base.Scope, metricNames base.MetricNames, flags *CheckFlags) (checkResult *CheckResult)

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

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
	RecentlyChecked bool

	Query                   string
	CustomQuery             string
	Threshold               float64
	MetricNameUsedAsDefault string

	AggregatedMetrics map[string]base.MetricResult
	MetricsThresholds map[string]float64
	MetricsHealth     base.MetricHealthMap
	ThrottledApps     []base.AppThrottle
	AppCheckedMetrics map[string]string
	RecentApps        map[string](*base.RecentApp)
}

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