pool

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package pool manages a dynamic fleet of tgbox clients with LRU capacity bounds and idle garbage collection.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ClientBuilder

type ClientBuilder func(token string, keepAlive func()) (*tgbox.Client, error)

ClientBuilder is the factory used to construct a new isolated bot client. The builder must wire keepAlive into the client's traffic observation (tgbox.WithTrafficObserver) so active clients are not reaped by the idle GC.

type EvictCallback

type EvictCallback[K comparable, V any] func(key K, value V)

EvictCallback is invoked when an entry is removed from the LRU cache, either by capacity pressure or an explicit Remove. It runs while the cache mutex is held, so it must not call back into the cache.

type LRU

type LRU[K comparable, V any] struct {
	// contains filtered or unexported fields
}

LRU is a thread-safe least-recently-used cache.

func NewLRU

func NewLRU[K comparable, V any](capacity int, onEvict EvictCallback[K, V]) *LRU[K, V]

NewLRU constructs an LRU cache with the given capacity. Capacity must be positive; the caller (NewManager) validates it.

func (*LRU[K, V]) Get

func (c *LRU[K, V]) Get(key K) (V, bool)

Get retrieves a key's value, promoting it as recently used. It achieves an O(1) retrieval leveraging the internal map.

func (*LRU[K, V]) Len

func (c *LRU[K, V]) Len() int

Len returns the current number of cached entries.

func (*LRU[K, V]) Put

func (c *LRU[K, V]) Put(key K, value V) bool

Put inserts or updates a value in the cache, promoting it to the front. Returns true if the capacity was exceeded and an eviction occurred.

func (*LRU[K, V]) Remove

func (c *LRU[K, V]) Remove(key K) bool

Remove deletes the key from the cache, invoking the eviction callback.

type Manager

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

Manager orchestrates a fleet of tgbox clients. Capacity is bounded by an LRU cache, concurrent Get calls for the same token are deduplicated with singleflight, and a background loop reaps idle connections.

func NewManager

func NewManager(capacity int, idleTimeout time.Duration, builder ClientBuilder) (*Manager, error)

NewManager creates a pool manager. capacity must be positive. If idleTimeout is not positive, idle garbage collection is disabled and clients are only evicted by LRU capacity pressure.

func (*Manager) Close

func (m *Manager) Close()

Close cleanly shuts down the manager and all active clients.

func (*Manager) Get

func (m *Manager) Get(token string) (*tgbox.Client, error)

Get returns the client for the token, creating and starting it if needed.

Jump to

Keyboard shortcuts

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