Documentation
¶
Overview ¶
Package sqlqueue is the Postgres queue behind the llm-d-async sql transport. Requests hash into Partitions partitions per queue, and each dispatcher leases a share of them. A request is pending while its dispatch_epoch is 0; dispatch stamps the partition epoch, and a new owner bumps the epoch and resets older stamps, which redelivers whatever the previous owner had in flight.
Index ¶
- Constants
- Variables
- type Completion
- type Consumer
- func (c *Consumer) Abandon(stamps ...Stamp)
- func (c *Consumer) Ack(ctx context.Context, completions []Completion) ([]bool, error)
- func (c *Consumer) Close(ctx context.Context) error
- func (c *Consumer) Defer(ctx context.Context, stamps []Stamp, notBefore int64) error
- func (c *Consumer) Poll(ctx context.Context, now time.Time, limit int) ([]Request, error)
- func (c *Consumer) Rebalance(ctx context.Context, now time.Time) error
- func (c *Consumer) Retry(ctx context.Context, stamp Stamp, notBefore int64, envelope string) (bool, error)
- func (c *Consumer) Stop()
- func (c *Consumer) Undispatch(ctx context.Context, stamps []Stamp) error
- type Key
- type Lease
- type OpenOption
- type Request
- type Result
- type Stamp
- type Store
- func (s *Store) Ack(ctx context.Context, owner string, completions []Completion) ([]bool, error)
- func (s *Store) AcquirePartitions(ctx context.Context, queue, owner string, now time.Time, ttl time.Duration, ...) ([]Lease, error)
- func (s *Store) AcquireQuotaSlots(ctx context.Context, holder, key string, n, limit int) (int, error)
- func (s *Store) AdmitQuota(ctx context.Context, key string, n, limit int, window time.Duration) (int, error)
- func (s *Store) Backlog(ctx context.Context, queue string, now int64, windows []int64) (depth int64, expiring []int64, err error)
- func (s *Store) Cancel(ctx context.Context, ids []string) error
- func (s *Store) CancelledKeys(ctx context.Context, keys []Key) (map[Key]bool, error)
- func (s *Store) Close() error
- func (s *Store) Defer(ctx context.Context, owner string, stamps []Stamp, notBefore int64) error
- func (s *Store) DeleteQuotaHolder(ctx context.Context, holder string) error
- func (s *Store) Dispatch(ctx context.Context, queue, owner string, now time.Time, limit int) ([]Request, error)
- func (s *Store) Enqueue(ctx context.Context, reqs ...Request) error
- func (s *Store) EnsurePartitions(ctx context.Context, queue string) error
- func (s *Store) ExpireResults(ctx context.Context, now int64, limit int) (int64, error)
- func (s *Store) HasRequests(ctx context.Context, queue string) (bool, error)
- func (s *Store) Heartbeat(ctx context.Context, queue, owner string, now time.Time, ttl time.Duration, ...) ([]Lease, int, error)
- func (s *Store) InFlight(ctx context.Context, queue, owner string) ([]Stamp, error)
- func (s *Store) Leave(ctx context.Context, queue, owner string) error
- func (s *Store) Migrate(ctx context.Context) error
- func (s *Store) Now(ctx context.Context) (time.Time, error)
- func (s *Store) Ping(ctx context.Context) error
- func (s *Store) PopResults(ctx context.Context, route string, now int64, limit int) ([]Result, error)
- func (s *Store) ReapQuota(ctx context.Context, limit int) (int, error)
- func (s *Store) ReapQuotaHolders(ctx context.Context, grace time.Duration) error
- func (s *Store) RegisterQuotaHolder(ctx context.Context, holder string, ttl time.Duration) error
- func (s *Store) ReleasePartitions(ctx context.Context, queue, owner string, partitions []int) error
- func (s *Store) ReleaseQuotaSlots(ctx context.Context, holder string, keys []string, counts []int) error
- func (s *Store) RenewQuotaHolder(ctx context.Context, holder string, ttl time.Duration) (bool, error)
- func (s *Store) ResetStale(ctx context.Context, queue, owner string, partitions []int) error
- func (s *Store) ResultDepth(ctx context.Context, route string) (int64, error)
- func (s *Store) Retry(ctx context.Context, owner string, stamp Stamp, notBefore int64, ...) (bool, error)
- func (s *Store) SetDraining(ctx context.Context, queue, owner string, partitions []int, draining bool) error
- func (s *Store) TruncateForTest(ctx context.Context) error
- func (s *Store) Undispatch(ctx context.Context, owner string, stamps []Stamp) error
Constants ¶
const DefaultMaxConns = 32
DefaultMaxConns is how many connections a Store opens and keeps when Open is not given WithMaxConns.
const Partitions = 64
Variables ¶
var ErrQuotaHolderLapsed = errors.New("sqlqueue: quota holder lease lapsed")
ErrQuotaHolderLapsed means the holder's lease expired; its slots no longer count and it must register again under a new name.
Functions ¶
This section is empty.
Types ¶
type Completion ¶
type Consumer ¶
type Consumer struct {
// contains filtered or unexported fields
}
Consumer is one dispatcher's partitions and in-flight requests on a queue.
owned ──(above share)──> draining ──(nothing in flight, or drainTimeout)──> released ^ │ └───────(below share)──────┘
func NewConsumer ¶
func (*Consumer) Abandon ¶
Abandon hands requests whose outcome could not be written to the next Rebalance.
func (*Consumer) Defer ¶
Defer is Undispatch for requests that must not dispatch again before notBefore.
func (*Consumer) Poll ¶
Poll marks the consumer for reconciliation on error, since the dispatch may have committed.
type OpenOption ¶
type OpenOption func(*openOptions)
OpenOption configures Open.
func WithMaxConns ¶
func WithMaxConns(n int) OpenOption
WithMaxConns caps the Store's open connections and keeps that many idle; n <= 0 means DefaultMaxConns.
func WithTracerProvider ¶
func WithTracerProvider(tp trace.TracerProvider) OpenOption
WithTracerProvider records a span per database statement.
type Request ¶
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
func (*Store) Ack ¶
Ack deletes each request and records its result in one transaction. Fenced, missing, and duplicate completions report false.
func (*Store) AcquirePartitions ¶
func (s *Store) AcquirePartitions(ctx context.Context, queue, owner string, now time.Time, ttl time.Duration, limit int) ([]Lease, error)
AcquirePartitions takes unowned or lapsed partitions and bumps their epoch.
func (*Store) AcquireQuotaSlots ¶
func (s *Store) AcquireQuotaSlots(ctx context.Context, holder, key string, n, limit int) (int, error)
AcquireQuotaSlots takes up to n of limit concurrent slots for key on behalf of holder and returns how many it took.
func (*Store) AdmitQuota ¶
func (s *Store) AdmitQuota(ctx context.Context, key string, n, limit int, window time.Duration) (int, error)
AdmitQuota admits up to n requests for key while fewer than limit were admitted in the trailing window, and returns how many it admitted. Each window keeps its own log, so gates with different windows on one key count only their own admissions.
func (*Store) CancelledKeys ¶
CancelledKeys returns the subset of keys marked cancelled. A key with no row is absent from the result: an unknown request is not cancelled.
func (*Store) Defer ¶
Defer returns dispatched requests to pending, not dispatched again before notBefore.
func (*Store) DeleteQuotaHolder ¶
DeleteQuotaHolder deletes a holder and its slots, freeing everything it still counts.
func (*Store) Dispatch ¶
func (s *Store) Dispatch(ctx context.Context, queue, owner string, now time.Time, limit int) ([]Request, error)
Dispatch stamps up to limit pending requests in partitions owner holds and is not draining.
func (*Store) Enqueue ¶
Enqueue inserts all requests or none. Equal deadlines dispatch in submission order.
func (*Store) EnsurePartitions ¶
func (*Store) ExpireResults ¶
ExpireResults deletes up to limit results on any route whose TTL ended before now and returns how many it deleted.
func (*Store) HasRequests ¶
func (*Store) Heartbeat ¶
func (s *Store) Heartbeat(ctx context.Context, queue, owner string, now time.Time, ttl time.Duration, member bool) ([]Lease, int, error)
Heartbeat renews owner's leases and reports live dispatchers. A non-member renews without counting toward the share, so peers take over its partitions while it drains.
func (*Store) Migrate ¶
Migrate creates the schema in a database that has none and refuses one that another build created. It is safe to run concurrently; a current schema takes no DDL locks.
func (*Store) PopResults ¶
func (s *Store) PopResults(ctx context.Context, route string, now int64, limit int) ([]Result, error)
PopResults deletes the results it returns.
func (*Store) ReapQuota ¶
ReapQuota deletes up to limit rows of each kind that no longer count: expired rate admissions, windows and slots that hold nothing, and keys no slot names. It returns how many windows and rows it changed.
func (*Store) ReapQuotaHolders ¶
ReapQuotaHolders deletes holders whose lease lapsed more than grace ago, along with their slots.
func (*Store) RegisterQuotaHolder ¶
RegisterQuotaHolder creates a holder whose concurrency slots count until its lease lapses.
func (*Store) ReleasePartitions ¶
func (*Store) ReleaseQuotaSlots ¶
func (s *Store) ReleaseQuotaSlots(ctx context.Context, holder string, keys []string, counts []int) error
ReleaseQuotaSlots returns counts[i] of holder's slots for keys[i].
func (*Store) RenewQuotaHolder ¶
func (s *Store) RenewQuotaHolder(ctx context.Context, holder string, ttl time.Duration) (bool, error)
RenewQuotaHolder extends a live holder's lease. It returns false once the lease has lapsed, and a lapsed holder is never renewed.
func (*Store) ResetStale ¶
ResetStale returns requests stamped by an older epoch of owner's partitions to pending, limited to the given partitions unless nil. Rows another transaction holds are skipped and left for a later call.
func (*Store) ResultDepth ¶
func (*Store) Retry ¶
func (s *Store) Retry(ctx context.Context, owner string, stamp Stamp, notBefore int64, envelope string) (bool, error)
Retry parks a dispatched request until notBefore with an updated envelope; its payload is unchanged.