servertable

package
v0.22.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: AGPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package servertable owns the lifecycle of the agent process a direct External Agent driver shares between the conversations of one Bot Agent.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Resource

type Resource interface {
	Close() error
	Done() <-chan struct{}
}

Resource is the shape the table manages: a process-backed handle that can be torn down and reports its own exit.

type Table

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

Table owns one shared resource per key. It exists because an agent server is used concurrently (turns, model catalog, login) and recycled asynchronously (configuration updates, wedged-turn recovery), and ad-hoc checks over a bare map kept racing each other: kill-by-bot aborted sibling turns, an is-anyone-else-active check raced registration, and a recycle could miss a server still starting outside the lock.

The model is reference counting plus displacement:

  • Acquire takes the current entry under the lock and holds a reference for the whole use, so a user is visible from the moment it commits to the server — there is no window between "decided to use it" and "registered".
  • Recycle displaces the entry from the table. New acquires build a fresh server (a new generation); existing references keep the displaced server alive until the last release, which tears it down. A wedged turn is thereby confined to a dying process instead of forcing a choice between killing siblings and sharing the process forever.
  • a server still starting when displaced discovers it on completion and destroys itself; the acquire retries and rebuilds from current state.

func New

func New(start func(ctx context.Context, key string) (Resource, error), logger *slog.Logger) *Table

func (*Table) Acquire

func (t *Table) Acquire(ctx context.Context, key string) (Resource, func(), error)

Acquire returns the key's live server and a release the caller must invoke when done with it (idempotent). It waits for a concurrent startup, retries once when a startup loses to a recycle, and never returns a displaced or dead server.

func (*Table) CloseAll

func (t *Table) CloseAll()

CloseAll hard-tears every entry down for process shutdown. Servers still starting discover their displacement on completion and self-destroy.

func (*Table) Peek

func (t *Table) Peek(key string) Resource

Peek returns the key's current live server without creating one and without taking a reference; callers may only touch in-memory state.

func (*Table) Recycle

func (t *Table) Recycle(key string)

Recycle displaces the key's entry: new acquires build a fresh server, and the displaced one dies when idle — immediately if nothing holds it, at the last release otherwise, or on startup completion if it was still starting.

func (*Table) RecycleResource

func (t *Table) RecycleResource(key string, expected Resource)

RecycleResource only displaces the observed generation. Discovery runs outside the table lock; another caller may already have replaced it by the time the discovery result arrives.

func (*Table) RecycleWhere

func (t *Table) RecycleWhere(match func(string) bool)

Jump to

Keyboard shortcuts

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