exit

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: AGPL-3.0 Imports: 5 Imported by: 0

Documentation

Overview

Package exit implements a keepalive.Listener that shuts the process down after a period of inactivity — the "inactivity-detection (we kill)" listener anticipated by the keepalive package doc.

It inverts the sprites lease model for providers where compute stops when the main process exits (Fly Machines with restart policy "no"): instead of renewing a lease while active, clank-host exits cleanly once idle and the provider's edge restarts it on the next request.

"Active" is the union of three signals:

  • backend events (the Tick's lastActivity, fed by agent sessions)
  • HTTP traffic (every request through TrackHTTP refreshes the clock)
  • in-flight requests (open SSE streams, tunnel bridges — any request still being served vetoes exit regardless of timestamps)

Process start counts as activity so a machine woken for a single request always gets a full idle window to serve it.

Index

Constants

View Source
const DefaultIdleThreshold = 3 * time.Minute

DefaultIdleThreshold matches the sprites listener's window: the host shuts down ~3 minutes after the last sign of life.

Variables

This section is empty.

Functions

This section is empty.

Types

type Listener

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

Listener triggers Options.Shutdown when idle. Construct via New, wrap the host's HTTP handler with TrackHTTP, and register with the keepalive Loop like any other Listener.

func New

func New(opts Options) *Listener

New constructs a Listener. Panics on a missing Shutdown — a nil trigger would silently disable the provider's only stop mechanism.

func (*Listener) Close

func (l *Listener) Close(context.Context) error

Close implements keepalive.Listener. Nothing to release — the process is already on its way down when this runs.

func (*Listener) Tick

func (l *Listener) Tick(_ context.Context, lastActivity time.Time)

Tick implements keepalive.Listener: fire Shutdown exactly once when every activity signal is older than the idle threshold and nothing is in flight.

func (*Listener) TrackHTTP

func (l *Listener) TrackHTTP(next http.Handler) http.Handler

TrackHTTP counts in-flight requests and stamps request activity. Long-lived requests (SSE, tunnel bridges) hold the in-flight count for their whole duration, vetoing exit while a client is attached.

type Options

type Options struct {
	Shutdown      func()
	IdleThreshold time.Duration // zero → DefaultIdleThreshold
	Log           *log.Logger   // nil → log.Default()

	// Now overrides the clock. Tests inject a controllable clock to
	// drive the decision table without sleeping. Nil means time.Now.
	Now func() time.Time
}

Options configures the Listener. Shutdown is required — it must initiate a graceful process shutdown (production sends SIGTERM to self so the server drains through the normal signal path).

Jump to

Keyboard shortcuts

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