worker

package
v0.24.0 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2026 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package worker provides an asynchronous worker pool and utils for persisting conversation turns using the provided storage.Driver.

The pool decouples storage operations from the proxy's HTTP hot path so that the client-proxy-upstream interaction is fully transparent.

Embedding writes deliberately do NOT happen here: the derive worker family is the single writer of embeddings (pkg/spanembed), keyed by deterministic span identity, so the ingest hot path stays pure capture.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Driver is the storage backend for persisting nodes.
	Driver storage.Driver

	// Publisher is an optional event publisher for newly inserted nodes.
	// If nil, publishing is disabled.
	Publisher publisher.Publisher

	// NumWorkers is the number of background workers in the pool.
	NumWorkers uint

	// QueueSize is the capacity of the buffered job channel (defaults to 256).
	QueueSize uint

	// Project is the git repository or project name to tag on stored nodes.
	Project string

	// Logger is the provided logger
	Logger *slog.Logger
}

Config is the configuration options for the worker pool.

type Job

type Job struct {
	Provider  string
	AgentName string
	// ThreadID is the harness sub-thread that fired this call ("" for
	// the main thread), captured at the wire and stamped onto the
	// turn's nodes as non-hashed metadata.
	ThreadID string
	Req      *llm.ChatRequest
	Resp     *llm.ChatResponse

	// Session is the optional session-tracking envelope attached to
	// the turn at the ingest HTTP boundary. When non-nil and the
	// driver supports session-aware ingest (Postgres), the worker
	// pool runs the transactional path: a `sessions` row is UPSERTed,
	// nodes are inserted with a non-NULL session_id FK, and counters
	// are rolled up — all in one Tx. When nil OR when the driver does
	// not implement that capability (e.g. inmemory), the legacy
	// per-node Put loop runs and session metadata is dropped on the
	// floor — this keeps the local CLI proxy and unit tests working
	// without a Postgres backend.
	Session *sessions.IngestEnvelope
}

Job is a unit of work for the worker pool to execute against.

type Pool

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

Pool processes storage jobs asynchronously via a worker pool.

func NewPool

func NewPool(c *Config) (*Pool, error)

NewPool creates a new Storer and starts its worker goroutines.

func (*Pool) Close

func (p *Pool) Close()

Close signals workers to stop and waits for in-flight jobs to drain. Call this during graceful shutdown after the proxy HTTP server has stopped.

func (*Pool) Enqueue

func (p *Pool) Enqueue(job Job) bool

Enqueue submits a job for processing by the worker pool. Returns true if enqueued, false if the queue is full, resulting in the job being dropped

func (*Pool) Len added in v0.7.0

func (p *Pool) Len() int

Len returns the current number of jobs buffered in the queue. It is a best-effort snapshot — workers may pick up items between the read and any downstream observation — and is intended for metric instrumentation rather than for routing decisions.

Jump to

Keyboard shortcuts

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