planner

package
v1.11.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2025 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 KeyPlan

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

KeyPlan manages the statistics for a single key and makes decisions about its resolvers. This struct is now entirely lock-free, using a sync.Map to manage its stats.

func (*KeyPlan) SelectStrategy added in v1.10.2

func (kp *KeyPlan) SelectStrategy(resolvers map[string]*KeyPlanStrategy) *KeyPlanStrategy

SelectStrategy implements the Thompson Sampling decision rule.

func (*KeyPlan) UpdateStats

func (kp *KeyPlan) UpdateStats(plan *KeyPlanStrategy, duration time.Duration)

UpdateStats performs the Bayesian update for the given resolver's statistics.

type KeyPlanStrategy added in v1.10.2

type KeyPlanStrategy struct {
	Type 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) GetKeyPlan

func (p *Planner) GetKeyPlan(key string) *KeyPlan

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

func (*Planner) StopCleanup

func (p *Planner) StopCleanup()

StopCleanup gracefully terminates the background cleanup goroutine.

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