httpserver

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package httpserver runs an HTTP server with graceful shutdown.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(ctx context.Context, addr string, handler http.Handler, log *slog.Logger) error

Run starts the HTTP server and blocks until ctx is cancelled, then shuts down gracefully within a 10s timeout.

func RunListeners added in v0.2.0

func RunListeners(ctx context.Context, listeners []Listener, log *slog.Logger) error

RunListeners serves all configured listeners under one coordinated lifecycle.

func RunPair added in v0.2.0

func RunPair(ctx context.Context, apiAddr string, apiHandler http.Handler, metricsAddr string, metricsHandler http.Handler, log *slog.Logger) error

RunPair serves the API and an optional metrics listener under one coordinated lifecycle: either listener's failure cancels the shared context so the other listener is joined via the same graceful shutdown, instead of leaking a goroutine. metricsHandler nil means no metrics listener is started at all.

Types

type Listener added in v0.2.0

type Listener struct {
	Name     string
	Addr     string
	Handler  http.Handler
	Timeouts Timeouts
}

Listener describes one HTTP listener in a coordinated server group.

type Timeouts added in v0.2.0

type Timeouts struct {
	ReadHeader time.Duration
	Write      time.Duration
	Idle       time.Duration
}

Timeouts bound a connection's lifetime. Zero fields take the package defaults.

ReadHeaderTimeout is the slowloris guard and is always applied. WriteTimeout in Go covers the span from the end of the request headers to the end of the response write, so it also bounds reading a large request body: the default is generous enough for a retained source archive on a slow link while still reaping a stuck connection. ReadTimeout is deliberately left unset for that same reason.

Jump to

Keyboard shortcuts

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