planner

package
v1.11.4 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	EvictionThreshold time.Duration // How long a key can be unused before being evicted. (e.g., 30 * time.Minute)
	CleanupInterval   time.Duration // How often the planner checks for stale keys. (e.g., 5 * time.Minute)
}

Config holds configuration for the planner.

type Manager added in v1.11.1

type Manager interface {
	GetPlanSelector(key string) Selector
	Stop()
}

Manager defines the interface for creating and managing plans for different keys.

type PlanConfig added in v1.11.1

type PlanConfig struct {
	Name string
	/*
		In Thompson Sampling, the algorithm models its belief about each strategy's performance as a probability distribution (a "belief curve").
		The InitialGuess is the value that the algorithm assumes is the most likely average latency for a strategy it knows nothing about.
		To choose a strategy, the algorithm doesn't just use the average; it takes a random sample (a "draw") from each strategy's full belief curve.
		Before any data is collected, these draws will be statistically centered around the InitialGuess.
		A strategy with a low InitialGuess will have its draws centered around a low number, making it more likely to be chosen initially.
		Once a strategy is chosen and its actual performance is measured, that new data point is used to update the belief curve via a Bayesian update.
		If a strategy with an InitialGuess of 50ms consistently performs at 10ms, the curve's center will quickly "move" from 50ms down to 10ms. This is how the algorithm learns and adapts.
	*/
	InitialGuess time.Duration
	/*
		Lambda represents our confidence in the InitialGuess. How many good runs do we believe we've effectively seen already?
		A low Lambda (e.g., 1) means we have very little confidence. The model is "open-minded" and will quickly change its beliefs based on the first few real results. This encourages exploration.
		A medium lambda: 5, means a modest trust.
		A high Lambda (e.g., 10) means we are very confident. The model is "stubborn" and will require a lot of contradictory data to move away from its initial guess. This encourages exploitation.
	*/
	Lambda float64

	Alpha float64
	Beta  float64
}

type Planner

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

Planner is the top-level entry point for creating and managing plans for different keys. It is safe for concurrent use and includes a background routine to evict old keys.

func New

func New(config *Config) *Planner

New creates a new Planner with the specified configuration and starts its cleanup routine.

func NewNoopPlanner

func NewNoopPlanner() *Planner

func (*Planner) GetPlanSelector added in v1.11.1

func (p *Planner) GetPlanSelector(key string) Selector

GetPlanSelector retrieves the plan for a specific key, creating it if it doesn't exist.

func (*Planner) Stop added in v1.11.1

func (p *Planner) Stop()

Stop gracefully terminates the background cleanup goroutine.

type Selector added in v1.11.1

type Selector interface {
	Select(resolvers map[string]*PlanConfig) *PlanConfig
	UpdateStats(plan *PlanConfig, duration time.Duration)
}

Selector defines the interface for managing strategy selection and stats for a single key.

type ThompsonStats

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

ThompsonStats holds the parameters for the Normal-gamma distribution, which models our belief about the performance (execution time) of a strategy.

func NewThompsonStats

func NewThompsonStats(initialGuess time.Duration, lambda, alpha, beta float64) *ThompsonStats

NewThompsonStats creates a new stats object with a diffuse prior, representing our initial uncertainty about a strategy's performance.

func (*ThompsonStats) Sample

func (ts *ThompsonStats) Sample(r *rand.Rand) float64

Sample draws a random execution time from the learned distribution. This is the core of Thompson Sampling: we sample from our belief and act greedily on that sample.

func (*ThompsonStats) Update

func (ts *ThompsonStats) Update(duration time.Duration)

Update performs a Bayesian update on the distribution's parameters using the new data point (the observed execution duration). It is the responsibility of the caller to enforce synchronization if multiple goroutines may call Update concurrently.

Jump to

Keyboard shortcuts

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