gate

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 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, the first wish of the rank first. A holder keeps its gate until it gives it back, its connection ends, its process ends or its timeout passes. 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) 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() int

Outside counts the gates held outside any running worker, by a person's terminal, a lead or a script: 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, or it held it past its timeout; "" 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.
	Timeout time.Duration
}

Request is what a holder asks a gate for.

type State

type State struct {
	Name, Holder, HolderTaskID string
	Since, Until               time.Time
	Waiting                    []string
	TakesSlot                  bool              // held outside a running worker
	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
	Note(taskID string, ev harness.Event)
	Works(taskID string) bool
}

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

Jump to

Keyboard shortcuts

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