handler

package
v0.33.0 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: BSD-2-Clause Imports: 30 Imported by: ? 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 handler owns the HTTP surface: the five canonical admin endpoints and the typed /api/v1 read endpoints.

Index

Constants

View Source
const DefaultLeaseSeconds = 300

DefaultLeaseSeconds is the lease a claim gets when the caller names none. Five minutes is long enough that a working run renews once rather than continuously, and short enough that a run that died frees its key quickly instead of blocking it until someone notices.

View Source
const MaxLeaseSeconds = 86400

MaxLeaseSeconds bounds a requested lease. The bound exists because time.Duration(ttl) * time.Second overflows int64 nanoseconds above ~9.2e9 s, and an overflowed lease renders lease_until in the past — a claim that is expired the moment it is taken. A day is longer than any run needs.

View Source
const RenewLeadSeconds = 60

RenewLeadSeconds is how long before expiry a holder should renew. The response states the instant rather than leaving the caller to re-derive it.

Variables

View Source
var MergeCallerLog = glog.Infof

MergeCallerLog records one line per merge request naming its caller. It is a variable rather than a direct glog call, matching vaultapi.MergeLog, so the specs can assert the line without depending on glog's file sinks. Production never reassigns it.

Functions

func AcquireClaimHandler added in v0.14.0

func AcquireClaimHandler(
	deps ClaimDeps,
	scope func(*http.Request) (ClaimScope, bool),
) http.HandlerFunc

AcquireClaimHandler takes a claim. A conflict means another holder has it and answers 409 claim_held with the holder that won.

func ListClaimsHandler added in v0.18.0

func ListClaimsHandler(deps ClaimDeps) http.HandlerFunc

ListClaimsHandler enumerates claims, optionally narrowed to one holder.

The route exists because a claim surface that can only be read one key at a time cannot answer the question a restarting executor actually asks: which claims do I already hold? Without it, an executor that restarts cannot see the refs it left behind and has to wait out every lease instead of re-adopting or fencing its own work.

Filtering by holder is a convenience, not a security boundary: the gateway secret is what separates callers, and a holder that names someone else simply sees someone else's claims. It is offered because the caller's own filter is the common case and doing it server-side keeps the response small.

An expired claim is reported with its stored lease_until rather than flagged or omitted. Expiry is a comparison against the caller's own clock, and computing it here would bake this server's clock into an answer that goes stale the moment it is sent — the caller already has lease_until and knows what now is.

func NewHandler

func NewHandler(
	ctx context.Context,
	index *vaultapi.Index,
	store storage.PageStorage,
	gatewaySecret string,
	vaultPath string,
	tasksDir string,
	vaultName string,
	writer gitrest.Client,
	clock libtime.CurrentDateTime,
	taskPublisher publisher.TaskPublisher,
	refs gitrest.RefClient,
) http.Handler

NewHandler builds the service router. When gatewaySecret is non-empty every /api/v1/* request must carry it in X-Gateway-Secret; the admin endpoints stay unauthenticated, matching the fleet's contract.

The write endpoints need five things the read endpoints do not: vaultPath, to read the original bytes before re-rendering them; vaultName, which stamps the change event so a consumer sharing one topic across vaults can tell its own tasks apart; writer, to hand the rendered file to git-rest; clock, so an approval's instant is injected rather than read from the wall clock; and taskPublisher, which turns a landed write into a change event.

func PRClaimScope added in v0.19.0

func PRClaimScope() func(*http.Request) (ClaimScope, bool)

PRClaimScope resolves {owner}, {repo} and {number} to a per-PR claim scope. Like a repository, a pull request needs no index entry to be claimed.

func ReadClaimHandler added in v0.14.0

func ReadClaimHandler(
	deps ClaimDeps,
	scope func(*http.Request) (ClaimScope, bool),
) http.HandlerFunc

ReadClaimHandler reports the current holder, or 404 when the claim is free.

func ReleaseClaimHandler added in v0.14.0

func ReleaseClaimHandler(
	deps ClaimDeps,
	scope func(*http.Request) (ClaimScope, bool),
) http.HandlerFunc

ReleaseClaimHandler releases a claim. The token is mandatory: releasing someone else's claim because you hold a stale one is the failure the compare-and-swap exists to prevent.

func RenewClaimHandler added in v0.14.0

func RenewClaimHandler(
	deps ClaimDeps,
	scope func(*http.Request) (ClaimScope, bool),
) http.HandlerFunc

RenewClaimHandler extends a held claim. A conflict means the lease was lost and answers 409 lease_lost.

func RepoClaimScope added in v0.14.0

func RepoClaimScope() func(*http.Request) (ClaimScope, bool)

RepoClaimScope resolves the {owner} and {repo} path variables to a repo claim scope. It fails only for a name that cannot be part of a claim key: a repository needs no index entry to be claimed.

func TaskClaimScope added in v0.14.0

func TaskClaimScope(index *vaultapi.Index) func(*http.Request) (ClaimScope, bool)

TaskClaimScope resolves the {id} path variable to a task claim scope.

Types

type ClaimDeps added in v0.14.0

type ClaimDeps struct {
	Refs  gitrest.RefClient
	Clock libtime.CurrentDateTime
}

ClaimDeps is what the claim handlers need: the index to resolve an identifier, the ref client that carries the claim to git-rest, and an injected clock so a lease instant is computed from a supplied time rather than the wall clock.

type ClaimListResponse added in v0.18.0

type ClaimListResponse struct {
	Items   []ClaimSummary `json:"items"`
	Total   int            `json:"total"`
	Skipped int            `json:"skipped"`
}

ClaimListResponse is the body of GET /api/v1/claims.

It carries no index_age_seconds, unlike the task and goal listings: claims are read live from git-rest and no index describes them, so an age here would be a fabricated number a caller could gate on. Skipped counts refs under the claims prefix that are not addressable claims — counted rather than dropped, for the same reason the task index counts unparsable pages instead of coercing them.

type ClaimScope added in v0.14.0

type ClaimScope struct {
	Scope string
	ID    string
	Key   string
}

ClaimScope is one claim's address: the key the transport stores it under and the identifier the typed API answers with.

func NewClaimScope added in v0.14.0

func NewClaimScope(scope string, id string, key string) ClaimScope

NewClaimScope builds an address for a claim. It is exported so the handlers can be exercised without an index: the resolver's job is to produce one of these, and a test that supplies it directly is testing the handler, not the resolver.

type ClaimSummary added in v0.18.0

type ClaimSummary struct {
	Scope       string `json:"scope"`
	ID          string `json:"id"`
	Holder      string `json:"holder"`
	TaskID      string `json:"task_id"`
	LeaseUntil  string `json:"lease_until"`
	RenewBefore string `json:"renew_before"`
	LeaseToken  string `json:"lease_token"`
	// SpawnedAt is absent on a claim whose holder has not created its Job.
	SpawnedAt string `json:"spawned_at,omitempty"`
}

ClaimSummary is one claim as the listing reports it.

It carries claimResponse's identity fields plus task_id, and it deliberately omits the transport key: the key is this API's business, never the caller's. A caller addresses a claim by scope and id, which is exactly what the renew and release routes accept — so a listed claim is actionable without the caller ever learning how it is stored.

task_id is the field that makes the listing worth having. A repository claim belongs to no task, so the claiming run's own task identifier is what the transport stores; a restarting executor groups its claims by it to recover one run's holdings together rather than key by key.

type TaskDocument added in v0.15.0

type TaskDocument struct {
	ID          string         `json:"id"`
	Title       string         `json:"title"`
	Frontmatter map[string]any `json:"frontmatter"`
	Body        string         `json:"body"`
	// Reopened marks a create that replaced a terminal occupant rather than
	// writing a free path. Absent on every ordinary create, so a caller that
	// ignores it is unaffected and one that logs it can record the reopen.
	Reopened bool `json:"reopened,omitempty"`
}

TaskDocument is one task read in full: the identifier the index resolves, the title, the whole frontmatter block and the markdown body. The listing serves the flat projection; this serves the page, which is what a caller that needs to read or rewrite a task's content actually wants.

Jump to

Keyboard shortcuts

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