acceptcache

package
v1.28.6 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2026 License: Apache-2.0, MIT Imports: 2 Imported by: 0

Documentation

Overview

Package acceptcache stores the result of a CanAccept() call so the scheduler can reuse it on the next considerWork cycle without paying the (potentially expensive) cost of re-evaluating CanAccept. The entries expire after a configurable TTL to avoid acting on stale decisions.

The slice, timestamp, and mutex are unexported, so callers can only interact through Add / Consume / TakeMatching, which always acquire the lock correctly. Prefer TakeMatching in considerWork so unrelated cached IDs are not discarded and an empty intersection is treated as a miss.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache is a TTL-bounded bucket of accepted task IDs. Multiple producers (the background poller, the scheduler writing leftover remainders) may call Add concurrently with the scheduler calling Consume.

Add de-duplicates: the same task ID added more than once between Consume calls is stored once. Without this, a backlog that stays unowned across many poll cycles would re-Add the full set every cycle and grow the slice unboundedly (the TTL never expires while Add keeps refreshing it).

func New

func New(ttl time.Duration) *Cache

New constructs a Cache whose entries expire after ttl.

func (*Cache) Add

func (c *Cache) Add(ids []int64)

Add appends not-yet-cached ids to the cache and refreshes the TTL baseline. Duplicate ids (already present since the last Consume) are skipped. Callers may add from any goroutine.

func (*Cache) Consume

func (c *Cache) Consume() []int64

Consume returns all cached ids and clears the cache. If the TTL has elapsed since the last Add, the cache is discarded and nil is returned (the caller should fall back to calling CanAccept).

func (*Cache) TakeMatching

func (c *Cache) TakeMatching(candidates []int64) (matched []int64, hadFresh bool)

TakeMatching removes and returns cached ids that appear in candidates. Unmatched cached ids are left in place for later considerWork calls.

hadFresh is false when the cache is empty or TTL-expired (caller should call CanAccept for the full candidate set). hadFresh is true when the cache held live entries — even if matched is empty. An empty match with hadFresh true is a cache miss for this candidate set, not a CanAccept refusal; the caller must call CanAccept rather than treating it as deny.

Jump to

Keyboard shortcuts

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