sessionpool

package
v0.0.49 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: Apache-2.0, MIT Imports: 3 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package sessionpool keeps one live backend process per key, reused across jobs and closed once it has been idle.

A provider CLI's startup floor is seconds — measured at ~8.5s warm and up to ~128s cold on the slowest agent — and a batch queue that starts a fresh process per source pays it every time. Reusing one process across the whole queue pays it once. The pool is keyed (by provider name, in draft's use) so two request kinds routed to the same provider share a process, while two different providers each keep their own; and it closes a process that has gone unused for the idle interval so a stalled or finished queue does not hold a subprocess open forever.

The pool is generic over a Session — anything that can report whether it is still alive and can be closed — so it is exercised in tests with a fake. The engine's ACP agent connection satisfies it directly and is pooled through it by the ACP engine.

Lock order is entry before pool, never the reverse: Get holds an entry's lock across a slow start and then consults the pool map, so anything that held the pool lock while waiting for an entry would deadlock against it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*Pool)

Option configures a Pool.

func WithClock

func WithClock(now func() time.Time) Option

WithClock overrides the clock the pool reads for idle decisions. It exists for deterministic tests; production uses time.Now.

type Pool

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

Pool is a set of keyed, reused sessions with an idle close policy. The zero value is not usable; construct one with New. A Pool is safe for concurrent use.

func New

func New(idle time.Duration, opts ...Option) *Pool

New returns a pool that closes a session after it has been idle for at least idle. An idle of zero means a released session is eligible to be reaped immediately.

func (*Pool) CloseAll

func (p *Pool) CloseAll()

CloseAll closes every live session and empties the pool, regardless of idle time or in-use count. It is the end-of-queue shutdown: nothing should be borrowing when it runs, but it does not wait for borrowers.

func (*Pool) Get

func (p *Pool) Get(ctx context.Context, key string, start StartFunc) (Session, error)

Get returns a live session for key, starting one with start if the key has none or its session has died. The returned session is marked in use until a matching Release, so it cannot be reaped while a caller holds it. A start error is returned and leaves the key with no live session.

func (*Pool) Len

func (p *Pool) Len() int

Len reports how many keys currently hold a live session.

func (*Pool) Reap

func (p *Pool) Reap() int

Reap closes and drops every session that is not in use and has been idle for at least the pool's idle interval. It returns how many it closed. Call it periodically (a ticker) to bound how long an unused process lingers.

An entry whose lock is held is skipped rather than waited for: someone is borrowing it or starting its session, so it is not idle, and waiting would stall every Reap caller behind a start that can take minutes.

func (*Pool) Release

func (p *Pool) Release(key string)

Release returns a session borrowed with Get. After the last borrower releases it, the session becomes eligible for reaping once idle. Releasing a key with no live borrow is a no-op, so a defer is always safe.

type Session

type Session interface {
	// Alive reports whether the session is still usable. A process that has
	// exited returns false, and the pool starts a replacement.
	Alive() bool
	// Close shuts the session down. The pool calls it once per session, when
	// the session is reaped or the pool is closed.
	Close() error
}

Session is a reusable, closable backend process. draft's ACP agent connection satisfies it.

type StartFunc

type StartFunc func(ctx context.Context) (Session, error)

StartFunc opens a new session for a key. The pool calls it when a key has no live session; concurrent first borrowers of the same key wait on one call rather than each starting a process.

Jump to

Keyboard shortcuts

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