postgrescoord

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package postgrescoord provides Postgres-backed cron.Claimer and cron.Elector implementations through database/sql. It uses server time, so application clock skew does not affect claims or leadership leases.

Index

Constants

View Source
const (
	// DefaultLeaderName scopes the default leadership lease row.
	DefaultLeaderName = "default"
	// DefaultLeaderTTL is the leadership lease. Failover can take one TTL.
	DefaultLeaderTTL = 30 * time.Second
)
View Source
const (
	// DefaultClaimsTable stores fire claims.
	DefaultClaimsTable = "cron_claims"
	// DefaultLeaderTable stores leadership leases.
	DefaultLeaderTable = "cron_leader"
)
View Source
const (
	// DefaultClaimTTL is the time a fire claim remains reserved. It must cover
	// the largest expected jitter, clock skew, and delayed catch-up window.
	DefaultClaimTTL = 10 * time.Minute
)

Variables

This section is empty.

Functions

func Migrate

func Migrate(ctx context.Context, db *sql.DB) error

Migrate creates the default coordination tables if they do not exist. Call it during deployment; constructors never run DDL.

func MigrateTables

func MigrateTables(ctx context.Context, db *sql.DB, claimsTable, leaderTable string) error

MigrateTables is Migrate with custom plain or schema-qualified table names. It rejects a nil ctx or db and invalid identifiers, and bounds the DDL by a five-second statement timeout.

Types

type Claimer

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

Claimer stores fire claims in Postgres. Expired claims for the same fire key are replaced atomically.

func NewClaimer

func NewClaimer(db *sql.DB, opts ...ClaimerOption) (*Claimer, error)

NewClaimer constructs a Postgres fire Claimer. The claims table must exist; see Migrate. The driver must support sql.Result.RowsAffected. It returns an error for a nil db, a nil option, an option that rejects its argument, or a failure of the system random source.

func (*Claimer) Claim

func (c *Claimer) Claim(ctx context.Context, fireKey string) (bool, error)

Claim reserves fireKey until its server-side TTL, replacing an expired claim for the same key atomically. false, nil means another scheduler instance already holds it. Each call is bounded by a five-second statement timeout. A nil ctx or an empty key is rejected; database failures are returned wrapped, so the scheduler skips the fire with cron.SkipClaimError.

func (*Claimer) Cleanup

func (c *Claimer) Cleanup(ctx context.Context) error

Cleanup deletes claims that have been expired for over one minute. Run it periodically when long-term claim history is not needed. A nil ctx is rejected; the statement is bounded by a five-second timeout.

type ClaimerOption

type ClaimerOption func(*claimerConfig) error

ClaimerOption configures a Claimer.

func WithClaimTTL

func WithClaimTTL(ttl time.Duration) ClaimerOption

WithClaimTTL overrides DefaultClaimTTL. A non-positive ttl is rejected.

func WithClaimsTable

func WithClaimsTable(table string) ClaimerOption

WithClaimsTable overrides DefaultClaimsTable. The name must be a plain or schema-qualified SQL identifier; anything else is rejected.

type Elector

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

Elector is a lease-based cron.Elector. IsLeader renews a lease already held by this process; no background goroutine or Close is required.

func NewElector

func NewElector(db *sql.DB, opts ...ElectorOption) (*Elector, error)

NewElector constructs a lease-based Postgres Elector. The leader table must exist; see Migrate. It returns an error for a nil db, a nil option, an option that rejects its argument, or a failure of the system random source.

func (*Elector) IsLeader

func (e *Elector) IsLeader(ctx context.Context) (bool, error)

IsLeader acquires the lease when it is free or expired, renews it when this process holds it, and returns false, nil while another process owns it. Each call is bounded by a five-second statement timeout. A nil ctx is rejected; database failures are returned wrapped, so the scheduler skips the fire with cron.SkipElectionError.

type ElectorOption

type ElectorOption func(*electorConfig) error

ElectorOption configures an Elector.

func WithLeaderName

func WithLeaderName(name string) ElectorOption

WithLeaderName scopes the lease row, letting several fleets share a table. The name is trimmed; an empty name is rejected.

func WithLeaderTTL

func WithLeaderTTL(ttl time.Duration) ElectorOption

WithLeaderTTL overrides DefaultLeaderTTL. A non-positive ttl is rejected.

func WithLeaderTable

func WithLeaderTable(table string) ElectorOption

WithLeaderTable overrides DefaultLeaderTable. The name must be a plain or schema-qualified SQL identifier; anything else is rejected.

Jump to

Keyboard shortcuts

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