pg

package
v0.1.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: Apache-2.0 Imports: 24 Imported by: ? 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 pg keeps Lagrange's state in Postgres: the a2a-go task store, push config store, work queue and event queue that cluster mode needs, plus the tables the platform itself uses (users, addresses, tokens, task links, activity).

Postgres is the only dependency. The work queue uses FOR UPDATE SKIP LOCKED with leases, and LISTEN/NOTIFY wakes waiting workers and live views without polling.

Index

Constants

View Source
const (
	// ChannelWork is notified when a work item is written.
	ChannelWork = "lagrange_work"
	// ChannelActivity is notified with the seq of each new activity row.
	ChannelActivity = "lagrange_activity"
)

Notification channels.

Variables

This section is empty.

Functions

func IsForeignKeyViolation

func IsForeignKeyViolation(err error) bool

IsForeignKeyViolation reports whether err is a foreign key violation.

func IsUniqueViolation

func IsUniqueViolation(err error) bool

IsUniqueViolation reports whether err is a unique constraint violation.

func NewEventQueueManager

func NewEventQueueManager(db *DB, poll time.Duration) eventqueue.Manager

NewEventQueueManager returns a2a-go's pull event queue reading the task_events table, so a subscriber on any replica follows a task executed on another one.

func NewWorkQueue

func NewWorkQueue(db *DB, workerID string, cfg WorkQueueConfig) workqueue.Queue

NewWorkQueue returns an a2a-go work queue backed by db for cluster mode. workerID identifies this process in work_items.worker.

Types

type DB

type DB struct {
	Pool *pgxpool.Pool
	// contains filtered or unexported fields
}

DB is a connection pool plus a listener that fans out notifications.

func Open

func Open(ctx context.Context, url string) (*DB, error)

Open connects to url, applies migrations and starts listening for notifications.

func (*DB) Close

func (db *DB) Close()

Close stops the listener and closes the pool.

func (*DB) Notify

func (db *DB) Notify(ctx context.Context, channel, payload string) error

Notify sends payload on channel.

func (*DB) Subscribe

func (db *DB) Subscribe(channel string) (<-chan string, func())

Subscribe returns a channel that receives the payload of every notification on channel, and a function that unsubscribes. Slow receivers miss notifications rather than block others, so treat a notification as a hint to re-read.

type PushStore

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

PushStore implements push.ConfigStore.

func NewPushStore

func NewPushStore(db *DB) *PushStore

NewPushStore returns a push config store backed by db.

func (*PushStore) Delete

func (s *PushStore) Delete(ctx context.Context, taskID a2a.TaskID, configID string) error

Delete implements push.ConfigStore.

func (*PushStore) DeleteAll

func (s *PushStore) DeleteAll(ctx context.Context, taskID a2a.TaskID) error

DeleteAll implements push.ConfigStore.

func (*PushStore) Get

func (s *PushStore) Get(ctx context.Context, taskID a2a.TaskID, configID string) (*a2a.PushConfig, error)

Get implements push.ConfigStore.

func (*PushStore) List

func (s *PushStore) List(ctx context.Context, taskID a2a.TaskID) ([]*a2a.PushConfig, error)

List implements push.ConfigStore.

func (*PushStore) Save

func (s *PushStore) Save(ctx context.Context, taskID a2a.TaskID, config *a2a.PushConfig) (*a2a.PushConfig, error)

Save implements push.ConfigStore.

type TaskStore

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

TaskStore implements taskstore.Store. A task belongs to the tenant (the address) and the user (the sender) of the call that created it, both taken from the a2asrv CallContext. Calls from another user or tenant see a2a.ErrTaskNotFound, as the spec asks (3.3.2). Each change also appends an event for the pull event queue.

func NewTaskStore

func NewTaskStore(db *DB) *TaskStore

NewTaskStore returns a task store backed by db.

func (*TaskStore) Create

func (s *TaskStore) Create(ctx context.Context, task *a2a.Task) (taskstore.TaskVersion, error)

Create implements taskstore.Store.

func (*TaskStore) Get

func (s *TaskStore) Get(ctx context.Context, taskID a2a.TaskID) (*taskstore.StoredTask, error)

Get implements taskstore.Store.

func (*TaskStore) List

List implements taskstore.Store. Like a2a-go's in-memory store it lists the caller's own tasks, newest first, and rejects anonymous callers.

func (*TaskStore) Load

func (s *TaskStore) Load(ctx context.Context, taskID a2a.TaskID) (*taskstore.StoredTask, string, error)

Load returns a task and its tenant without checking who is asking. It is for the platform's own code (the inbox, the graph), never for A2A callers.

func (*TaskStore) Update

Update implements taskstore.Store.

type WorkQueue

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

WorkQueue is the workqueue.ReadWriter behind NewWorkQueue.

func NewWorkQueueBackend

func NewWorkQueueBackend(db *DB, workerID string, cfg WorkQueueConfig) *WorkQueue

NewWorkQueueBackend returns the ReadWriter that NewWorkQueue wraps.

func (*WorkQueue) Close

func (q *WorkQueue) Close(wait time.Duration)

Close stops claiming work and cancels running executions, then waits up to wait for them to hand their items back. Items returned this way don't count as an attempt and are free for another worker at once; items that aren't returned in time are picked up when their lease expires.

func (*WorkQueue) Queue

func (q *WorkQueue) Queue() workqueue.Queue

Queue returns the a2a-go queue that pulls from q. Executions run with a context that Close cancels.

func (*WorkQueue) Read

func (q *WorkQueue) Read(ctx context.Context) (workqueue.Message, error)

Read implements workqueue.ReadWriter. It blocks until an item can be claimed.

func (*WorkQueue) Write

func (q *WorkQueue) Write(ctx context.Context, payload *workqueue.Payload) (a2a.TaskID, error)

Write implements workqueue.Writer. Only one execution per task can be pending or running at a time.

type WorkQueueConfig

type WorkQueueConfig struct {
	// Lease is how long a claimed item stays with a worker without a heartbeat (default 10s).
	Lease time.Duration
	// MaxAttempts is how many times an item that fails is retried (default 3).
	MaxAttempts int
	// Poll is the longest a worker waits between claim attempts when no notification
	// arrives (default 1s).
	Poll time.Duration
}

WorkQueueConfig tunes the work queue. Zero values mean the defaults.

Directories

Path Synopsis
Package pgtest gives tests a real Postgres.
Package pgtest gives tests a real Postgres.

Jump to

Keyboard shortcuts

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