Documentation
¶
Overview ¶
Package completer batches job state updates and heartbeats. Results are written by size (100) or interval (50 ms) in single statements, retried with backoff on database errors, never dropped, and flushed on shutdown.
c, err := completer.New(completer.Config{ClientID: id, Store: store, Clock: clk})
go c.Run(ctx)
c.Add(completion)
Index ¶
Constants ¶
const ( DefaultBatchSize = 100 DefaultFlushInterval = 50 * time.Millisecond DefaultBufferSize = 10_000 DefaultMaxRetryDelay = 5 * time.Second DefaultFlushTimeout = 10 * time.Second DefaultCallTimeout = 30 * time.Second )
Defaults.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Completer ¶
type Completer struct {
// contains filtered or unexported fields
}
Completer batches job state updates (by size or interval) into single statements. Results are never dropped: on a database error they stay in memory and the write is retried with jittered exponential backoff. The buffer is unbounded in memory but producers stop fetching while Full reports true, which bounds it to BufferSize plus running jobs.
func (*Completer) Add ¶
func (c *Completer) Add(comp job.Completion)
Add queues one result. It never blocks and never drops.
type Config ¶
type Config struct {
ClientID string
Store job.CompletionWriter
Clock clock.Clock
Logger *slog.Logger
// BatchSize is the maximum number of results per statement.
BatchSize int
// FlushInterval is the longest a result waits before being written.
FlushInterval time.Duration
// BufferSize is the number of pending results at which Full reports true.
BufferSize int
// MaxRetryDelay caps the backoff between failed writes.
MaxRetryDelay time.Duration
// FlushTimeout bounds the final flush on shutdown.
FlushTimeout time.Duration
// CallTimeout bounds one write.
CallTimeout time.Duration
// OnApplied receives the completions that passed the guard and the number rejected as stale.
OnApplied func(applied []job.Completion, stale int)
// OnDrained is called when the buffer stops being full.
OnDrained func()
// Rand returns a float in [0, 1) for retry jitter; nil uses math/rand/v2.
Rand func() float64
}
Config configures a Completer.
type HeartbeatConfig ¶
type HeartbeatConfig struct {
ClientID string
Store job.HeartbeatWriter
Clock clock.Clock
Logger *slog.Logger
// Interval between heartbeats (Config.HeartbeatInterval).
Interval time.Duration
// Running lists the attempts this client is executing.
Running func() []job.Ref
}
HeartbeatConfig configures a Heartbeater.
type Heartbeater ¶
type Heartbeater struct {
// contains filtered or unexported fields
}
Heartbeater refreshes heartbeat_at of this client's running jobs in batches every Interval. The completion guard applies.
func NewHeartbeater ¶
func NewHeartbeater(cfg HeartbeatConfig) (*Heartbeater, error)
NewHeartbeater validates cfg.