Documentation
¶
Overview ¶
Package postgres holds a complete Storage over Postgres (§3.2): the production storage adapter, the Go stand-in for the TypeScript package's PrismaStorage.
It owns its tables, prefixed agentenkit_ by default because a production database is usually shared with the caller's own schema. Hand it any *sql.DB opened with a Postgres driver; the package never imports one.
Index ¶
- Constants
- type Bus
- type BusOptions
- type ConnectNotifier
- type Handler
- type Kv
- func (k *Kv) Del(ctx context.Context, key string) error
- func (k *Kv) DeleteExpired(ctx context.Context) (int64, error)
- func (k *Kv) Get(ctx context.Context, key string) (string, bool, error)
- func (k *Kv) Incr(ctx context.Context, key string) (int64, error)
- func (k *Kv) Set(ctx context.Context, key, value string, opts ports.SetOptions) (bool, error)
- type Listener
- type Option
- type Queue
- type QueueOptions
- type Storage
Constants ¶
const NotifyChannel = "agentenkit_events"
NotifyChannel is the one Postgres channel every thread's events travel on. LISTEN is per connection, not per channel-pattern, so one channel and an in-process fan-out by thread id costs one connection per process however many threads it watches.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bus ¶
type Bus struct {
// contains filtered or unexported fields
}
Bus is an EventBus over LISTEN/NOTIFY.
At-most-once by contract, so a dropped notification is fine; the durable log is the record. What the adapter must get right is the size cap: a CHUNK carrying a tool result can exceed 8000 bytes, and NOTIFY refuses it. Such an event, durable or not, is parked in the kv under a short TTL and travels as a reference to that key. The subscriber reads it back from the kv, which needs no storage scope: a storage that requires a tenant (§2.10) cannot be read from the listener, which serves every tenant.
Each subscription remembers the last durable seq it delivered. When the LISTEN connection comes back after a drop, the bus replays what each subscriber missed from the durable store, scoped with the subscriber's own state, and delivers nothing twice.
func NewBus ¶
func NewBus(db *sql.DB, listener Listener, events ports.EventStore, kv *Kv, opts BusOptions) *Bus
NewBus builds the bus. Publishing goes through db (pg_notify); listening through listener. kv parks oversized events; events is the durable store a reconnect replays from, and the fallback for a reference the kv no longer has. Pass the storage's Events() and the NewKv Kv.
type BusOptions ¶
type BusOptions struct {
// Heartbeat is the interval of the bus-only HEARTBEAT notice each
// subscription emits. Zero means one minute.
Heartbeat time.Duration
}
BusOptions tunes NewBus.
type ConnectNotifier ¶ added in v0.2.3
type ConnectNotifier interface {
OnConnect(fn func())
}
ConnectNotifier is what a Listener can add to that: a callback for every LISTEN connection it brings up, the first one included. The bus uses it to replay, from the durable log, whatever a dropped connection missed, so a missed NOTIFY can never stall an open stream. pgxlisten implements it.
type Kv ¶
type Kv struct {
// contains filtered or unexported fields
}
Kv is a Kv over Postgres: the hot state, the run locks, the HITL handoff keys and the per-thread seq counters (§3.4), in one table beside the storage tables so a single database runs the whole platform.
Expiry is enforced on read. SET NX and Incr are single statements, so two workers can never both win a lock or both take the same seq.
func NewKv ¶
NewKv creates the table if it is missing and returns the Kv. The prefix matches the storage's (default "agentenkit_").
func (*Kv) DeleteExpired ¶
DeleteExpired drops rows past their expiry. Expiry is already enforced on read, so this is housekeeping: call it from a periodic job.
type Listener ¶
type Listener interface {
// Listen subscribes to channel and calls handler for every notification
// until ctx ends. It reconnects on its own; a payload lost while it was
// down is recovered by the client's cursor replay, which is what an
// at-most-once bus is for (§2.2).
Listen(ctx context.Context, channel string, handler func(payload string)) error
}
Listener is what the bus needs from a driver: one long-lived LISTEN connection that hands over every payload on a channel. The package never imports a driver; pgxlisten implements this over pgx.
type Option ¶
type Option func(*Storage)
Option tunes New.
func WithPrefix ¶
WithPrefix sets the table prefix. Default: "agentenkit_".
type Queue ¶
type Queue struct {
// contains filtered or unexported fields
}
Queue is a durable Queue over one Postgres table.
Enqueue is one INSERT; a delay is a future runAt. The consumer claims with SELECT ... FOR UPDATE SKIP LOCKED, so several processes can consume the same table, and each claim carries a lease that is renewed while the job runs and lapses when its worker dies. At-least-once: a job whose worker crashed is redelivered once its lease expires, and the engine's run lock makes the duplicate a no-op.
func NewQueue ¶
func NewQueue(ctx context.Context, db *sql.DB, opts QueueOptions, storageOpts ...Option) (*Queue, error)
NewQueue creates the table if it is missing and returns the queue. Nothing is consumed until Bind.
func (*Queue) Close ¶
func (q *Queue) Close()
Close stops the consumer and waits for running jobs to return.
type QueueOptions ¶
type QueueOptions struct {
// Poll is how often an idle consumer looks for work. Zero means 500ms.
Poll time.Duration
// Lease is how long a claimed job stays invisible to other consumers. It
// is renewed while the handler runs, so it only needs to outlast one
// renewal gap plus a crash. Zero means 2 minutes.
Lease time.Duration
// Concurrency is how many jobs one consumer runs at once. Zero means 4.
// The per-thread run lock keeps two jobs on one thread apart regardless.
Concurrency int
// MaxAttempts is how many deliveries a job gets before it is dropped as
// dead. The engine's own §2.8 policy re-enqueues a run that fails; this
// only guards against a handler that keeps dying. Zero means 5.
MaxAttempts int
}
QueueOptions tunes NewQueue.
type Storage ¶
type Storage struct {
// contains filtered or unexported fields
}
Storage is a Storage over Postgres.
func (*Storage) Events ¶
func (s *Storage) Events() ports.EventStore
func (*Storage) Messages ¶
func (s *Storage) Messages() ports.MessageStore
func (*Storage) Threads ¶
func (s *Storage) Threads() ports.ThreadStore
func (*Storage) Usage ¶
func (s *Storage) Usage() ports.UsageStore
Directories
¶
| Path | Synopsis |
|---|---|
|
Package pgxlisten is the postgres bus's Listener over pgx v5: one dedicated connection that LISTENs and hands every notification over.
|
Package pgxlisten is the postgres bus's Listener over pgx v5: one dedicated connection that LISTENs and hands every notification over. |