preempt

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: AGPL-3.0 Imports: 9 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 preempt watches a cloud provider's instance metadata service for the notice that a spot or preemptible machine is about to be reclaimed, so the worker running on it can hand its tasks back at once instead of being killed mid-task.

The watcher is opt-in (worker.preemption.watch) and contacts nothing otherwise. It supports the three notices providers publish:

  • aws: the IMDSv2 spot instance-action document.
  • gcp: the instance/preempted flag.
  • azure: a Preempt event in the Scheduled Events document.

A failed poll never stops the watcher: a metadata service that is briefly unreachable must not leave a machine that is then reclaimed unwatched.

Index

Constants

View Source
const (
	VendorAWS   = "aws"
	VendorGCP   = "gcp"
	VendorAzure = "azure"
)

Vendor names, as written in worker.preemption.watch.

Variables

This section is empty.

Functions

func InstanceID

func InstanceID(ctx context.Context, vendor, endpoint string, client *http.Client) (string, error)

InstanceID reads the identifier of the cloud instance the worker runs on from the instance metadata service of vendor (aws, gcp or azure), with the same base URLs, endpoint override and client rules as New. The identifier is the one a scale-set or instance-group operation names:

  • aws: the EC2 instance ID ("i-0abc..."), read after an IMDSv2 token.
  • gcp: the instance name, which "gcloud ... delete-instances" takes.
  • azure: the scale-set instance ID, the last segment of the VM's resource ID.

Each request is bounded by the same 2 s timeout as a preemption poll. Any failure is an error; the caller decides what a missing ID costs.

Types

type Watcher

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

Watcher polls one vendor's metadata service for a preemption notice.

func New

func New(vendor, endpoint string, interval time.Duration, client *http.Client, logger *slog.Logger) (*Watcher, error)

New builds a Watcher for vendor (aws, gcp or azure), polling every interval. endpoint, when not empty, replaces the vendor's default metadata base URL (for a proxy, or a test). client may be nil for the default client, which never uses an HTTP proxy: the metadata service is link-local, and a proxy named by HTTP_PROXY would answer for a different machine or not at all.

func (*Watcher) Base

func (w *Watcher) Base() string

Base returns the metadata base URL the watcher polls.

func (*Watcher) Run

func (w *Watcher) Run(ctx context.Context, onNotice func())

Run polls until the provider announces a preemption, calls onNotice exactly once, and returns. It also returns, without calling onNotice, when ctx ends. The first poll is immediate.

Jump to

Keyboard shortcuts

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