leasing

package
v0.1.13 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package leasing runs scan batches across processes. A batch (kept in a catalog.BatchStore) is leased by one run of it at a time, which beats while it runs: it goes on when the console that started it is closed, every replica shows it, and when its owner stops beating, as on a restart, another process takes it over and does what is left. Every write of a run moves one item from the state it expects, on the condition that the run still owns the unfinished batch (catalog.BatchStore.MoveItem), so runs never overwrite each other. An item that fails is tried again after a backoff, a few times.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options struct {
	// Parallel bounds the items of one batch run at once (2).
	Parallel int
	// Beat is how often a run records that it still runs a batch (5s); a
	// batch whose owner missed Stale (30s) is taken over.
	Beat, Stale time.Duration
	// Attempts bounds the runs of an item (3); Backoff is the wait before
	// the second (5s), doubling for each one after.
	Attempts int
	Backoff  time.Duration
	// MoveTries bounds the writes of one move when the store fails (4); the
	// first retry waits MoveBackoff (100ms), doubling after.
	MoveTries   int
	MoveBackoff time.Duration
	// Now is the clock (time.Now); Bus runs the goroutines (nil runs them
	// plain).
	Now func() time.Time
	Bus *bus.Bus
}

Options tune a Runner; a zero value takes its default.

type Resolver

type Resolver func(ctx context.Context, datasource string) (Work, error)

Resolver gives the Work of a datasource as configured now, for a batch taken over.

type Runner

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

Runner runs the batches this process owns.

func New

func New(store catalog.BatchStore, opts Options) *Runner

New is a Runner of the batches in store.

func (*Runner) Close

func (r *Runner) Close()

Close stops the batches this process runs (they stay unfinished, for a takeover) and waits for them.

func (*Runner) Owner

func (r *Runner) Owner() string

Owner names this process; a run of a batch is named Owner().<run>.

func (*Runner) Running

func (r *Runner) Running(id string) bool

Running reports whether this process runs the batch.

func (*Runner) Start

func (r *Runner) Start(ctx context.Context, b catalog.Batch, maxOpen int, work Work) (catalog.Batch, bool, error)

Start creates b, its ID, owner and times set here, and runs it; false with the batch started before by b's RequestKey instead, which is not run again. catalog.ErrTooManyBatches when b's datasource has maxOpen unfinished batches.

func (*Runner) Stop

func (r *Runner) Stop(id string)

Stop stops a batch this process runs, at once, settling what was left.

func (*Runner) TakeOver

func (r *Runner) TakeOver(ctx context.Context, resolve Resolver)

TakeOver runs the unfinished batches whose owner stopped beating: what they did not do yet is done here, and the items they were doing again. resolve says how to work a datasource as configured now.

func (*Runner) Watch

func (r *Runner) Watch(resolve Resolver)

Watch takes over the batches other processes stopped running, now and every Stale, until Close.

type Work

type Work func(ctx context.Context, datasource, schema string) (state, msg string)

Work does one item of a batch of datasource, the schema, and says how it went: catalog.ItemDone, catalog.ItemFailed with why, or catalog.ItemCanceled; "" when ctx ended first (whoever runs the batch next, or its end, settles the item).

Jump to

Keyboard shortcuts

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