inspect

package
v0.1.31 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultFillTimeout = 15 * time.Second

Variables

This section is empty.

Functions

func PolicyNames

func PolicyNames() []string

PolicyNames lists the cache classes a caller may flush by name, so a UI can offer them instead of asking someone to type one.

func RefreshRequested

func RefreshRequested(ctx context.Context) bool

RefreshRequested reports whether ctx asked for every lookup to rebuild.

func Register

func Register(cache Cache)

Register adds a cache to the process-wide list. NewMemo calls it, so a memo is reachable the moment it exists and no call site has to remember.

func WithObserver

func WithObserver(ctx context.Context, observe Observer) context.Context

WithObserver returns a context whose inspection lookups report to observe.

Installing a second observer replaces the first rather than chaining: the owner of a request context is the one party that knows what should hear about its lookups, and a hidden chain would let an inner scope leak observations to an outer one after it went out of scope.

func WithRefresh

func WithRefresh(ctx context.Context) context.Context

WithRefresh returns a context whose inspection lookups all rebuild.

Types

type Cache

type Cache interface {
	Policy() CachePolicy
	Stats() CacheStats
	// Clear drops every entry and returns how many there were. Entries being
	// filled right now are invalidated too, so the fill in flight cannot land
	// afterwards and resurrect what was just dropped.
	Clear() int
	Invalidate(key string)
	InvalidatePrefix(prefix string)
}

Cache is what a Memo looks like to something that does not know its value type — enough to describe it and to throw it away.

func Caches

func Caches() []Cache

Caches returns the registered caches, ordered by policy name so a console renders them in a stable order rather than in registration order, which depends on which packages happened to be linked in.

type CacheClass

type CacheClass string
const (
	CacheClassOpenSearchTargets         CacheClass = "opensearch-targets"
	CacheClassSQLCatalog                CacheClass = "sql-catalog"
	CacheClassOpenSearchFields          CacheClass = "opensearch-fields"
	CacheClassOpenSearchDynamicMapping  CacheClass = "opensearch-dynamic-mapping"
	CacheClassOpenSearchConcreteMapping CacheClass = "opensearch-concrete-mapping"
	CacheClassCardinality               CacheClass = "column-cardinality"
	CacheClassFilterValues              CacheClass = "filter-values"
)

type CacheMetadata

type CacheMetadata struct {
	Policy             string     `json:"policy"`
	State              CacheState `json:"state"`
	Cached             bool       `json:"cached"`
	Refreshing         bool       `json:"refreshing,omitempty"`
	LoadedAt           time.Time  `json:"loadedAt"`
	FreshUntil         time.Time  `json:"freshUntil"`
	LastChangedAt      time.Time  `json:"lastChangedAt"`
	LastRefreshAttempt *time.Time `json:"lastRefreshAttempt,omitempty"`
	LastRefreshError   string     `json:"lastRefreshError,omitempty"`
	AgeMS              int64      `json:"ageMs"`
	RetryAfterMS       int64      `json:"retryAfterMs,omitempty"`
	UnchangedRefreshes int        `json:"unchangedRefreshes,omitempty"`
}

type CachePolicy

type CachePolicy struct {
	Name            string
	InitialFreshFor time.Duration
	MaximumFreshFor time.Duration
	FillTimeout     time.Duration
	MaxEntries      int
	MaxWeight       int
}

func Policy

func Policy(class CacheClass) CachePolicy

type CacheState

type CacheState string
const (
	CacheStateFresh CacheState = "fresh"
	CacheStateStale CacheState = "stale"
)

type CacheStats

type CacheStats struct {
	Policy     string `json:"policy"`
	Entries    int    `json:"entries"`
	MaxEntries int    `json:"maxEntries"`
	Weight     int    `json:"weight"`
	MaxWeight  int    `json:"maxWeight"`
	// Filling counts loads in flight. A cache that is always filling is one
	// whose freshness window is shorter than the thing it caches takes to build.
	Filling int `json:"filling"`
	// Oldest is when the least recently loaded entry was filled, and is zero for
	// an empty cache.
	Oldest time.Time `json:"oldest,omitempty"`
	// FreshFor and MaxFreshFor are the policy's window, reported so a console can
	// explain a stale read without a second lookup.
	FreshForSeconds    int64 `json:"freshForSeconds"`
	MaxFreshForSeconds int64 `json:"maxFreshForSeconds"`
}

CacheStats describes one cache's occupancy against the ceilings its policy set — the two numbers that say whether it is doing anything and whether it is about to start evicting.

func Stats

func Stats() []CacheStats

Stats describes every registered cache.

type FlushOptions

type FlushOptions struct {
	Policy string
	Key    string
	Prefix string
}

FlushOptions narrows what a flush throws away. An empty Policy means every cache; an empty Key and Prefix mean every entry within the caches chosen.

type FlushResult

type FlushResult struct {
	Caches  []FlushedCache `json:"caches"`
	Entries int            `json:"entries"`
}

FlushResult reports what was actually dropped, per cache.

Reported rather than assumed: a flush aimed at a key that was never cached looks identical to one that worked, and an operator who cannot tell the two apart will conclude the flush is broken.

func Flush

func Flush(options FlushOptions) FlushResult

Flush drops matching entries and reports what went.

type FlushedCache

type FlushedCache struct {
	Policy  string `json:"policy"`
	Entries int    `json:"entries"`
}

type GetOptions

type GetOptions[T any] struct {
	Key     string
	Refresh bool
	Load    func(context.Context) (T, error)
}

type Memo

type Memo[T any] struct {
	// contains filtered or unexported fields
}

func NewMemo

func NewMemo[T any](options MemoOptions[T]) *Memo[T]

func (*Memo[T]) Clear

func (m *Memo[T]) Clear() int

Clear drops every entry and returns how many there were.

Generations are bumped for entries being filled right now, so a fill already in flight cannot land afterwards and resurrect what was just dropped — which is the difference between a flush and a suggestion.

func (*Memo[T]) Get

func (m *Memo[T]) Get(ctx context.Context, options GetOptions[T]) (Result[T], error)

func (*Memo[T]) Invalidate

func (m *Memo[T]) Invalidate(key string)

func (*Memo[T]) InvalidatePrefix

func (m *Memo[T]) InvalidatePrefix(prefix string)

func (*Memo[T]) Policy

func (m *Memo[T]) Policy() CachePolicy

Policy is the freshness and capacity contract this memo was built with.

func (*Memo[T]) Stats

func (m *Memo[T]) Stats() CacheStats

Stats describes what the memo currently holds.

type MemoOptions

type MemoOptions[T any] struct {
	Policy      CachePolicy
	Weight      func(T) int
	Fingerprint func(T) (string, error)
	Now         func() time.Time
}

type Observation

type Observation struct {
	// Policy is the cache class (see CacheClass), and Key the entry within it.
	Policy string
	Key    string

	// Elapsed is the wall time this caller waited. A cached read is ~0; a miss
	// pays the whole fill, which is the number worth looking at.
	Elapsed time.Duration

	Cache CacheMetadata

	// Err is the fill failure, if the caller got one. A stale entry served after
	// a failed refresh reports the failure on Cache.LastRefreshError instead and
	// leaves this empty — the caller was not failed, only served older facts.
	Err error
}

Observation is one inspection-cache lookup as the caller experienced it.

type Observer

type Observer func(Observation)

Observer receives every lookup made under a context it was installed on. It is called on the caller's goroutine, so it must not block.

func ObserverFrom

func ObserverFrom(ctx context.Context) Observer

ObserverFrom returns the observer installed on ctx, or nil.

type ObserverKey

type ObserverKey struct{}

ObserverKey is the context key an observer is stored under.

Exported because the request contexts that install one are not plain context.Context — commons-db's Context sets values through its own WithValue and would have to be rebuilt around a stdlib child otherwise, losing the database handle and namespace it carries.

type RefreshKey

type RefreshKey struct{}

RefreshKey marks a context whose inspection lookups must all rebuild rather than read what is cached.

Exported for the same reason ObserverKey is: the request contexts that set it are commons-db's Context, which carries a database handle and a namespace it would lose if it had to be rebuilt around a stdlib child.

It is deliberately per-request. GetOptions.Refresh already says "rebuild this one lookup" for a caller that knows which; this says "rebuild everything I read", which is what someone debugging a page wants and is still nobody else's problem — unlike flushing the cache, which is (see Flush).

type Result

type Result[T any] struct {
	Value T
	Cache CacheMetadata
}

Directories

Path Synopsis
Package opensearchinspect provides bounded, read-only OpenSearch metadata inspection.
Package opensearchinspect provides bounded, read-only OpenSearch metadata inspection.
Package sqlinspect provides reusable, read-only SQL catalog inspection.
Package sqlinspect provides reusable, read-only SQL catalog inspection.

Jump to

Keyboard shortcuts

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