planner

package
v1.10.0 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 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 {
	InitialGuess      time.Duration // The starting performance assumption for a new resolver.
	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) SelectResolver

func (kp *KeyPlan) SelectResolver(resolvers []string) string

SelectResolver implements the Thompson Sampling decision rule.

func (*KeyPlan) UpdateStats

func (kp *KeyPlan) UpdateStats(resolver string, duration time.Duration)

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

func (*KeyPlan) UpdateStatsOverGuess

func (kp *KeyPlan) UpdateStatsOverGuess(resolver string, duration time.Duration)

UpdateStatsOverGuess is like UpdateStats but only updates if the duration is worse than the initial guess. This can help reduce noise from very fast responses that might not reflect true performance (or cancelled ones)..

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