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 ¶
const ( // DefaultLeaderName scopes the default leadership lease row. DefaultLeaderName = "default" // DefaultLeaderTTL is the leadership lease. Failover can take one TTL. DefaultLeaderTTL = 30 * time.Second )
const ( // DefaultClaimsTable stores fire claims. DefaultClaimsTable = "cron_claims" // DefaultLeaderTable stores leadership leases. DefaultLeaderTable = "cron_leader" )
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 ¶
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 ¶
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.
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 ¶
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.