gate

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: Apache-2.0 Imports: 25 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 gate grants the gates: shared resources (code generation, the local stack, an end-to-end run, a paid model run) that one holder uses at a time, granted only while the machine is not under pressure and has the memory the command peaked at. Djinn's own requests to merge a task's work, settle a merge or check a push go first, the first come first; then the others, the first wish of the rank first (Q26), then the work of its first azima (Task.rank). A holder keeps its gate until it gives it back, its connection ends, its process ends or its timeout passes: none is taken back for a request that goes first. A lit ifrit's gate has no timeout: it is held until the ifrit goes out or ends, at its out time at the latest (T51), and goes to no one first for it. A gate held outside a running worker takes a worker's slot.

Index

Constants

View Source
const DefaultTimeout = time.Hour

DefaultTimeout is how long a gate may be held when its holder states no timeout.

Variables

This section is empty.

Functions

func Handler

func Handler(g *Gates) (string, http.Handler)

Handler returns the Connect handler of GateService on g, and its path prefix.

func Name

func Name(s string) string

Name is a gate's name as Djinn keeps it: names ignore case.

func Run

func Run(ctx context.Context, client machinev1connect.GateServiceClient, name, taskID string, c Command) (int, error)

Run takes the gate name from the server, for the task taskID when not empty, runs the command once it holds the gate, and gives the gate back when the command ends, fails or is interrupted. It returns the command's exit code. When the caller is killed, its connection ends, and the server gives the gate back.

Types

type Command

type Command struct {
	Args                   []string
	Dir                    string
	Stdin                  io.Reader
	Stdout, Stderr, Notice io.Writer // Notice gets what djinn says: why it waits, a gate lost
	// Costs, when not nil, records what the command cost once it ended by itself: CPU time, peak memory, duration.
	Costs machinev1connect.CommandServiceClient
}

Command is a command to run under a gate, with its standard streams.

type Costs

type Costs interface {
	Peak(ctx context.Context, taskID, dir, command string) uint64
	Ask(ctx context.Context, gate, taskID, dir, command string)
}

Costs gives the highest peak memory a command was measured at, in its project: the task's, else the one holding dir; 0 when never measured. Ask counts a run of the command asked under the gate, whether it then runs to its end or not. machine.Peaks is one.

type Gates

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

Gates grants the gates. Its zero value is not usable: New makes one.

func New

func New(m Machine, costs Costs, tasks Tasks) *Gates

New returns the gates of machine m (nil: never under pressure, its memory unknown), the commands' measured costs being costs (nil: none measured), the holders' tasks being tasks (nil: none known).

func (*Gates) AddInterval added in v0.1.1

func (g *Gates) AddInterval(iv *machinev1.GateInterval)

AddInterval records a gate interval into history (used in tests).

func (*Gates) Freed

func (g *Gates) Freed(f func())

Freed calls f each time a gate is given or taken back: a slot may have freed. djinn up wakes the scheduler.

func (*Gates) Held

func (g *Gates) Held(taskID string) []string

Held gives the names of the gates the task holds now, in order.

func (*Gates) IfritWaits added in v0.2.0

func (g *Gates) IfritWaits() map[string][]string

IfritWaits names, by the task of each lit ifrit, who waits for a gate it holds, the first come first: "W15 (Its title): go tool task e2e, for stack". The lead's brief says it, to offer the developer to end the ifrit.

func (*Gates) Intervals added in v0.1.1

func (g *Gates) Intervals(cutoff time.Time) []*machinev1.GateInterval

Intervals returns recent gate intervals (past and currently held) since cutoff.

func (*Gates) List

func (g *Gates) List() []State

List gives the gates held or waited for, by name.

func (*Gates) Measure

func (g *Gates) Measure(every time.Duration, read Read)

Measure reads, every interval, what each holder whose process is known uses, with read: djinn up gives machine.ReadWorker, where the system lets it measure (machine.NotMeasured).

func (*Gates) Outside

func (g *Gates) Outside() (n, merges int)

Outside counts the gates held outside any running worker, by a person's terminal, a lead, a script or Djinn's merges and pushes, and of them the merges' and pushes' (Request.First): each takes a worker's slot, so no new worker starts on it while it is held. A worker's gate is in the worker's own slot.

func (*Gates) ProjectResolver added in v0.1.1

func (g *Gates) ProjectResolver(fn func(taskID, dir string) string)

ProjectResolver sets the function that resolves a task and directory to a project name.

func (*Gates) Take

func (g *Gates) Take(ctx context.Context, r Request, waiting func(why string)) (*Hold, error)

Take waits until the gate is granted, and returns it held. waiting is called each time the reason to wait changes. Take fails when ctx ends first.

func (*Gates) Track added in v0.1.2

func (g *Gates) Track(f func(pid int, taskID string) func())

Track calls f with the process of each worker's command that holds a gate, djinn gate run, and its task, as it takes it; what f returns is called once the gate is given back. djinn up records them: none outlives it (W514).

func (*Gates) Waiting

func (g *Gates) Waiting(taskID string) []string

Waiting gives the names of the gates the task waits for now, in order.

type Hold

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

Hold is a gate held.

func (*Hold) Done

func (h *Hold) Done() <-chan struct{}

Done is closed once the gate is no longer held: given back, or taken back (TakenBack says why).

func (*Hold) Give

func (h *Hold) Give()

Give gives the gate back; calling it again, or once Djinn took it back, does nothing.

func (*Hold) TakenBack

func (h *Hold) TakenBack() string

TakenBack says why Djinn took the gate back before its holder gave it: its process ended, it held it past its timeout, or its ifrit went out or ended; "" when it did not.

type Machine

type Machine interface {
	Snapshot() machine.Snapshot
	Policy() machine.Policy
}

Machine is what the gates need from the machine: its pressure, its memory, and the policy that judges them. The monitor is one.

type Read

type Read func(pid int) (machine.Group, error)

Read reads what the process pid uses, with every process it started: machine.ReadWorker.

type Request

type Request struct {
	Name string
	// TaskID, when not empty, is the task the gate is taken for: its events say when it waits, takes and gives the
	// gate back.
	TaskID string
	// What says what the holder runs, in the folder Dir: a command measured in its project waits until the machine
	// has the memory it peaked at.
	What, Dir string
	// PID is the holder's process, 0 when unknown: it is measured while it holds the gate, and the gate goes back
	// once it ends.
	PID int
	// Timeout is how long the gate may be held: Djinn takes it back after that. 0: DefaultTimeout, but none for a lit
	// ifrit, which holds its gates until it goes out or ends.
	Timeout time.Duration
	// First tells that Djinn takes the gate to merge a task's work into its branch (its wish's or its azima's), to
	// settle such a merge, or to check a push: it blocks what comes after the task, so it goes before every other
	// request, the first come first, and never waits for one that came after it (Q26).
	First bool
}

Request is what a holder asks a gate for.

type State

type State struct {
	Name, Holder, HolderTaskID string
	Since, Until               time.Time // Until: when it is taken back at the latest; zero when not known
	Waiting                    []string
	WaitingTaskIDs             []string          // the tasks of the waiters, in the same order; a waiter outside any task left out
	TakesSlot                  bool              // held outside a running worker
	Ifrit                      bool              // held by a lit ifrit, until it goes out or ends
	Use                        *planv1.Resources // what the holder's process uses; nil when not read
}

State is a gate held or waited for.

type Tasks

type Tasks interface {
	Describe(ctx context.Context, taskID string) (string, error)
	Ranks(ctx context.Context, taskIDs []string) map[string]int
	// Places gives the position of each task's azima among the azimas of its wish (Task.rank), from 0; a task part of
	// no azima, or unknown, is not in the map.
	Places(ctx context.Context, taskIDs []string) map[string]int
	Note(taskID string, ev harness.Event)
	Works(taskID string) bool
	// Ifrit tells whether the task is an ifrit lit now, and when it goes out unless the developer comes back (zero:
	// not known yet); once it no longer is, why says why: "went out: …", "ended". A task that is no ifrit is not lit,
	// and has no why. It must not call the gates back.
	Ifrit(ctx context.Context, taskID string) (lit bool, out time.Time, why string)
}

Tasks is what the gates need from the tasks: to name a holder, to rank waiters by their wish and their azima, to tell a task's events, whether a task's worker runs, which holds its gates in its own slot, and whether a task is a lit ifrit, which holds its gates until it goes out or ends. The harness is one.

Jump to

Keyboard shortcuts

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