labkit

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package labkit holds the small pieces every PackLab reference app shares: JSON helpers, fix flags, bearer tokens, a minimal OpenAPI document, a work counter used to show where the planted bottlenecks spend their time, and the App shape of the apps that also listen on another protocol.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bearer

func Bearer(r *http.Request) string

Bearer returns the token from an Authorization: Bearer header.

func Decode

func Decode(w http.ResponseWriter, r *http.Request, v any) bool

Decode reads a JSON body into v, answering 400 itself on failure.

func Dialable

func Dialable(addr net.Addr) string

Dialable returns a listener's address as a client on this machine can dial it: an unspecified host (":8111", "[::]:8111") becomes localhost.

func Error

func Error(w http.ResponseWriter, status int, code, msg string)

Error writes {"error": {"code": code, "message": msg}}.

func JSON

func JSON(w http.ResponseWriter, status int, v any)

JSON writes v with a status code.

func OpenAPI

func OpenAPI(title, description string, routes []Route) http.HandlerFunc

OpenAPI serves a minimal OpenAPI 3.1 document listing the routes. It is what `stampede init` reads to recognise the product.

func Token

func Token(prefix string) string

Token returns a random hex token with a prefix.

Types

type App

type App struct {
	// Handler serves the HTTP side: the API, /healthz and /openapi.json.
	Handler http.Handler
	// Env holds what the pack's scenarios need besides TARGET_URL to
	// reach the other listeners, such as MQTT_BROKER=tcp://127.0.0.1:8111.
	Env map[string]string
	// Close stops the other listeners.
	Close func()
}

App is a reference app that serves more than HTTP: a broker, a cache or a UDP server next to its HTTP API.

type Config

type Config struct {
	// Fixes switches planted bottlenecks off.
	Fixes Fixes
	// Fast shortens deliberate waits (token pacing, admission ticks) so
	// tests run quickly. Bottlenecks stay in place.
	Fast bool
	// Listen is the address of an app's second listener (its MQTT broker,
	// Kafka cluster, Redis server or UDP game server), for the apps that
	// have one. Tests use "127.0.0.1:0".
	Listen string
	// Postgres is the PostgreSQL connection string DBLab seeds its tables
	// into.
	Postgres string
}

Config is what every reference app is built from.

type Counter

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

Counter counts units of work so tests and /debug/work can show what a bottleneck costs.

func (*Counter) Add

func (c *Counter) Add(n int)

Add records n units of work.

func (*Counter) Load

func (c *Counter) Load() int64

Load returns the total.

type Fixes

type Fixes map[string]bool

Fixes says which planted bottlenecks are switched off. The zero value keeps every bottleneck in place.

func ParseFixes

func ParseFixes(s string) Fixes

ParseFixes reads a comma-separated list such as "lock,index" or "all".

func (Fixes) On

func (f Fixes) On(name string) bool

On reports whether the fix for a bottleneck is switched on.

type Gauge

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

Gauge tracks how many things are in progress and the most there ever were at once.

func (*Gauge) Enter

func (g *Gauge) Enter() func()

Enter starts one; call the returned function when it ends.

func (*Gauge) Peak

func (g *Gauge) Peak() int64

Peak returns the highest concurrency seen.

type Route

type Route struct {
	Method, Path, Tag, Summary string
}

Route is one documented endpoint.

Jump to

Keyboard shortcuts

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