cachebatch

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package cachebatch provides batch cache loading with bounded concurrency. It warms the HTTP cache for gallery, info box, and lightbox routes, skips already-cached entries, and blocks when discovery is active.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrDiscoveryActive is returned when batch load is blocked because discovery is running.
	ErrDiscoveryActive = errors.New("cache batch load blocked: discovery is active")
	// ErrAlreadyRunning is returned when a batch load run is already in progress.
	ErrAlreadyRunning = errors.New("cache batch load already running")
)

Functions

This section is empty.

Types

type BatchLoadQueries

type BatchLoadQueries interface {
	GetBatchLoadTargets(ctx context.Context) ([]gallerydb.BatchLoadTarget, error)
	HttpCacheExistsByKey(ctx context.Context, key string) (bool, error)
}

BatchLoadQueries is the minimal interface needed for batch load.

type Config

type Config struct {
	// GetQueries returns HandlerQueries-like access; must provide GetBatchLoadTargets
	// and HttpCacheExistsByKey. Typically from dbRoPool.Get().Queries (CustomQueries).
	GetQueries func() (BatchLoadQueries, func())

	// GetHandler returns the HTTP handler wrapped with full middleware chain
	// (cache middleware, compression middleware, etc.)
	GetHandler func() http.Handler

	// GetETagVersion returns the current ETag version for cache keys.
	GetETagVersion func() string

	// ModuleStateService for discovery active check; nil skips guard.
	ModuleStateService moduleStateService
}

Config holds dependencies for BatchLoadManager.

type Manager

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

Manager runs batch cache load with bounded concurrency.

func NewManager

func NewManager(cfg Config) *Manager

NewManager creates a new BatchLoadManager.

func (*Manager) GetBatchLoadSnapshot added in v0.8.0

func (m *Manager) GetBatchLoadSnapshot() Metrics

GetBatchLoadSnapshot returns a snapshot of the current metrics for the metrics collector.

func (*Manager) Metrics

func (m *Manager) Metrics() *Metrics

Metrics returns the metrics instance for snapshot/recording.

func (*Manager) Run

func (m *Manager) Run(ctx context.Context) error

Run executes a batch load run. Returns ErrDiscoveryActive if discovery is running, ErrAlreadyRunning if another run is in progress, or nil on success.

type Metrics

type Metrics struct {
	TargetsTotal        int64 // total targets from GetBatchLoadTargets
	TargetsScheduled    int64 // enqueued for processing
	TargetsCompleted    int64 // completed successfully
	TargetsFailed       int64 // request failed
	TargetsSkipped      int64 // already cached (HttpCacheExistsByKey)
	ThrottlesSkipped    int64 // number of times scheduling was delayed (queue >80% full)
	BackpressureSkipped int64 // targets skipped due to backpressure (queue >95% full)
	InFlight            int64 // currently in progress
	IsRunning           int32 // 1 if a run is active, 0 otherwise
	LastStartedAt       int64 // Unix time of last Run start
	LastFinishedAt      int64 // Unix time of last Run finish
}

Metrics holds batch load run statistics. All int64 fields are updated with sync/atomic; use Snapshot for a consistent view.

func (*Metrics) RecordBackpressureSkipped

func (m *Metrics) RecordBackpressureSkipped()

RecordBackpressureSkipped increments backpressure skipped.

func (*Metrics) RecordCompleted

func (m *Metrics) RecordCompleted()

RecordCompleted increments completed and decrements in-flight.

func (*Metrics) RecordFailed

func (m *Metrics) RecordFailed()

RecordFailed increments failed and decrements in-flight.

func (*Metrics) RecordSkipped

func (m *Metrics) RecordSkipped()

RecordSkipped increments skipped.

func (*Metrics) RecordThrottled

func (m *Metrics) RecordThrottled()

RecordThrottled increments the throttle counter (scheduling was delayed due to high queue utilization).

func (*Metrics) Snapshot

func (m *Metrics) Snapshot() Metrics

Snapshot returns a copy of current metrics.

Jump to

Keyboard shortcuts

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