mirror

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 35 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package mirror keeps a one-way mirror from motte to external git remotes.

Index

Constants

View Source
const (

	// DefaultDiskBudget is the default size of the clone cache.
	DefaultDiskBudget = 20 << 30
	// DefaultIdleTTL is the default idle time of a clone.
	DefaultIdleTTL = 168 * time.Hour
)
View Source
const (

	// StateSeeding and StateSeedRefused are the mirror states that a seed reports.
	StateSeeding     = "seeding"
	StateSeedRefused = "seed_refused"

	// DefaultSeedMaxObjects and DefaultSeedMaxBytes are the size of one seed push. Both are below the limits of a push to motte.
	DefaultSeedMaxObjects = 50000
	DefaultSeedMaxBytes   = 32 << 20
	// DefaultSeedMaxInflated bounds the whole size of the objects of one seed push, as motte bounds its inflated data.
	DefaultSeedMaxInflated = 32 << 20
)
View Source
const DefaultGitHubAPI = forgeapp.DefaultAPI

DefaultGitHubAPI is the API base URL of the GitHub App credential when the configuration names none.

Variables

View Source
var (
	// ErrSignature is the error of a delivery that the secret does not sign.
	ErrSignature = errors.New("mirror: bad signature")
	// ErrBody is the error of a signed delivery that is not an event.
	ErrBody = errors.New("mirror: bad event body")
)

Functions

func GitRef

func GitRef(stored string) (string, bool)

GitRef maps a stored ref name to the git ref name. A stored name under refs/heads/ has none.

func MaybeAskpass

func MaybeAskpass(args []string, stdout io.Writer) bool

MaybeAskpass runs the askpass helper when the environment asks for it. It returns false for a normal start. git runs the program of GIT_ASKPASS with the prompt as its only argument, and the program is the ward binary.

func Mirrorable added in v0.2.1

func Mirrorable(stored string) bool

Mirrorable reports whether a stored ref name is a branch (a bare name) or a tag (under refs/tags/). ward never touches any other name on the remote.

func StoredRef

func StoredRef(git string) (string, bool)

StoredRef is the inverse of GitRef.

Types

type Class

type Class string

Class is the class of a failed action.

const (
	ClassMotte             Class = "motte"
	ClassRemoteUnreachable Class = "remote_unreachable"
	ClassRemoteAuth        Class = "remote_auth"
	ClassRemoteRejected    Class = "remote_rejected"
	ClassLocal             Class = "local"
)

type Clock

type Clock interface {
	Now() time.Time
	After(d time.Duration) <-chan time.Time
}

Clock is the source of time and of waits. Inside a synctest bubble the real clock is a fake one.

type Config

type Config struct {
	DataDir    string
	Motte      MotteConfig
	PublicURL  string
	HookSecret []byte
	Mirrors    []Entry
	// GitHubAPI is the API base URL for the GitHub App credential. Empty means DefaultGitHubAPI.
	GitHubAPI  string
	DiskBudget int64
	IdleTTL    time.Duration
	// SeedMaxObjects and SeedMaxBytes bound one push of a seed. Zero means the default.
	SeedMaxObjects int
	SeedMaxBytes   int64
	// SeedMaxInflated bounds the sum of the object sizes of one push of a seed.
	SeedMaxInflated int64
	// Askpass is the program that git runs for the remote credential. It is the ward binary.
	Askpass string
}

Config is the configuration of the engine.

type Engine

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

Engine runs the mirror.

func New

func New(cfg Config, store Store, opts Options) *Engine

New builds an engine over a state store.

func (*Engine) Deliver

func (e *Engine) Deliver(ctx context.Context, body []byte, signature string) error

Deliver handles one delivery of a ref event. It answers before any git work: it only records state.

func (*Engine) Mirror

func (e *Engine) Mirror(name string) (Entry, bool)

Mirror returns the entry with the name.

func (*Engine) Mirrors

func (e *Engine) Mirrors() []Entry

Mirrors returns the entries in configuration order.

func (*Engine) Refs

func (e *Engine) Refs(ctx context.Context, name, after string, limit int) ([]Ref, error)

Refs returns the refs of a mirror after the ref name, in name order.

func (*Engine) Registry

func (e *Engine) Registry() *prometheus.Registry

Registry is the Prometheus registry of the engine.

func (*Engine) RequestReconcile

func (e *Engine) RequestReconcile(name string) bool

RequestReconcile asks the worker of a mirror for a Reconcile. It returns at once.

func (*Engine) RequestReset

func (e *Engine) RequestReset()

RequestReset asks for a Reset. It returns at once.

func (*Engine) Run

func (e *Engine) Run(ctx context.Context) error

Run drives the engine until ctx ends. A failed step of the control work retries with the backoff of a failed action.

func (*Engine) SeedState added in v0.2.0

func (e *Engine) SeedState(name string) (state, message string)

SeedState returns StateSeeding or StateSeedRefused while a seed of the mirror runs or was refused, else empty, and the reason of a refusal.

func (*Engine) Stats

func (e *Engine) Stats(ctx context.Context) ([]Stats, error)

Stats returns the summary of every mirror that has state.

type Entry

type Entry struct {
	Name           string     `json:"name"`
	Repo           string     `json:"repo"`
	Remote         string     `json:"remote"`
	Refs           []string   `json:"refs"`
	Username       string     `json:"username"`
	CredentialFile string     `json:"credential_file,omitempty"`
	GitHubApp      *GitHubApp `json:"github_app,omitempty"`
	PruneUnknown   bool       `json:"prune_unknown"`
	Seed           bool       `json:"seed"`
}

Entry is one mirror of the configuration.

func ParseEntries

func ParseEntries(raw []byte) ([]Entry, error)

ParseEntries decodes and checks the WARD_MIRRORS list. It rejects an unknown field.

func (Entry) Matches

func (e Entry) Matches(stored string) bool

Matches reports whether a pattern of e matches the stored ref name and the name is mirrorable.

type GitHubApp

type GitHubApp struct {
	AppID          int64  `json:"app_id"`
	InstallationID int64  `json:"installation_id"`
	KeyFile        string `json:"key_file"`
}

GitHubApp is a GitHub App installation that supplies the remote credential.

type Hit

type Hit struct {
	Event   api.Event
	Mirrors []string
}

Hit is an event and the names of the mirrors whose patterns match it.

type HookResult

type HookResult int

HookResult is how the sequence of a delivered event compares with the partition state.

const (
	HookBaseline HookResult = iota
	HookInOrder
	HookRepeat
	HookGap
)

type MotteConfig

type MotteConfig struct {
	URL string
	Kid string
	Key ed25519.PrivateKey
}

MotteConfig says how ward reaches motte and signs its grants.

type Options

type Options struct {
	Clock  Clock
	Logger *slog.Logger
	// Remote talks to the git remotes of the mirrors. Nil means the git binary.
	Remote gitremote.Remote
	// Done is called after a unit of work ends: kind is batch, reconcile, catchup, reset, init, seed or seed_refused, and mirror is empty
	// for work that belongs to no mirror. It must not block.
	Done func(kind, mirror string)
}

Options are the parts that a caller may replace.

type Ref

type Ref struct {
	Mirror        string
	Name          string
	LastPartition int
	LastSeq       int64
	Dirty         bool
	Version       int64
	DirtySince    time.Time
	Pushed        string
	PushedSet     bool
	Attempts      int
	NextAttempt   time.Time
	Failure       Class
	Message       string
	FailureSince  time.Time
	PushedAt      time.Time
}

Ref is the state of one ref of one mirror. PushedSet is false while ward knows nothing about the remote value.

type SeedRef added in v0.2.0

type SeedRef struct{ Commit, Next string }

SeedRef is the motte value that a seed expects for a ref. Commit is the last value that ward wrote or found (empty for absence). Next is the value of a push that may or may not have landed (empty when none is under way).

type Service

type Service struct {
	SubscriptionID string
	EventsCursor   string
	LastSeq        map[int]int64
}

Service is the state that belongs to the process and not to a mirror.

type Stats

type Stats struct {
	Mirror      string
	Reconciled  bool
	Dirty       int
	Failing     map[Class]int
	OldestDirty time.Time
}

Stats summarises the refs of one mirror.

type Store

type Store interface {
	Ping(ctx context.Context) error
	Service(ctx context.Context) (Service, error)
	// SetSubscription stores the subscription and the events cursor. With clearSeq it also clears last_seq of every partition.
	SetSubscription(ctx context.Context, id, cursor string, clearSeq bool) error
	// RecordHook compares the sequence of the event with its partition, stores last_seq and the triggers, and says how.
	RecordHook(ctx context.Context, hit Hit, now time.Time) (HookResult, error)
	// RecordPage stores the triggers of a catch-up page, last_seq where it is higher, and the new cursor.
	RecordPage(ctx context.Context, hits []Hit, cursor string, now time.Time) error
	// Due returns the dirty refs of a mirror that may be acted on now, and the earliest later next_attempt (zero if none).
	Due(ctx context.Context, mirror string, now time.Time) ([]Ref, time.Time, error)
	// RecordPush stores a push result. It clears dirty only when the version of the ref is still version.
	RecordPush(ctx context.Context, mirror, ref string, version int64, pushed string, now time.Time) error
	// RecordFailure counts a failed action. It moves next_attempt only when the version of the ref is still version.
	RecordFailure(ctx context.Context, mirror, ref string, version int64, class Class, message string, next, now time.Time) error
	SetPushed(ctx context.Context, mirror, ref, pushed string) error
	// Reconciled triggers each ref of targets, sets its pushed value, and records the mirror.
	Reconciled(ctx context.Context, mirror string, targets map[string]string, now time.Time) error
	// Seed returns the seed record of a mirror: empty, started or done.
	Seed(ctx context.Context, mirror string) (string, error)
	SetSeed(ctx context.Context, mirror, state string) error
	// SeedRefs returns what a seed expects motte to hold for each ref it has written or may write.
	SeedRefs(ctx context.Context, mirror string) (map[string]SeedRef, error)
	SetSeedRef(ctx context.Context, mirror, ref string, r SeedRef) error
	// SeedCleaned reports whether the cleanup of the seed record has run.
	SeedCleaned(ctx context.Context, mirror string) (bool, error)
	// FinishSeedCleanup removes refs from the seed record and from the ref state of the mirror, and records the cleanup.
	FinishSeedCleanup(ctx context.Context, mirror string, refs []string) error
	HasMirror(ctx context.Context, name string) (bool, error)
	StateRefs(ctx context.Context, mirror string) ([]string, error)
	Refs(ctx context.Context, mirror, after string, limit int) ([]Ref, error)
	Stats(ctx context.Context) ([]Stats, error)
}

Store is the transactional state store. Each method that writes is one transaction.

Directories

Path Synopsis
Package mirrortest is the conformance suite of mirror.Store.
Package mirrortest is the conformance suite of mirror.Store.

Jump to

Keyboard shortcuts

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