Help us shape the future of Go! Take the 2026 Go Developer Survey

managementhttp

package
v1.1.5 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: MIT Imports: 16 Imported by: 2

Documentation

Overview

Package managementhttp transports queue management contracts over a bounded bearer-authenticated HTTP boundary. Authentication grants every configured route; this package adds no tenant/actor authorization or server. Providers and HTTP transports are borrowed and must honor contexts. Response byte bounds apply to clients, not total server serialization or fleet work.

Index

Examples

Constants

View Source
const MaxFleetEndpoints = 100

MaxFleetEndpoints bounds one resolved management fleet and all operation fan-out.

Variables

View Source
var (
	// ErrInvalidFleetConfiguration reports an unusable dynamic fleet client.
	ErrInvalidFleetConfiguration = errors.New("managementhttp: invalid fleet configuration")
	// ErrInvalidFleetEndpoints reports malformed, duplicate, or unbounded resolved endpoints.
	ErrInvalidFleetEndpoints = errors.New("managementhttp: invalid fleet endpoints")
	// ErrFleetTargetUnavailable reports a worker target absent from the current fleet snapshot.
	ErrFleetTargetUnavailable = errors.New("managementhttp: fleet target unavailable")
	// ErrFleetUnavailable reports a fleet operation with no trustworthy remote result.
	ErrFleetUnavailable = errors.New("managementhttp: fleet unavailable")
)
View Source
var (
	// ErrInvalidConfiguration reports invalid endpoint, credential or bound configuration.
	ErrInvalidConfiguration = errors.New("management HTTP: invalid configuration")
	// ErrInvalidRequest reports rejected caller request shape.
	ErrInvalidRequest = errors.New("management HTTP: invalid request")
	// ErrInvalidResponse reports invalid remote JSON or contract shape.
	ErrInvalidResponse = errors.New("management HTTP: invalid response")
	// ErrResponseTooLarge reports a remote response above its configured byte limit.
	ErrResponseTooLarge = errors.New("management HTTP: response too large")
	// ErrRemoteFailure reports remote transport/status failure without formatting its cause.
	ErrRemoteFailure = errors.New("management HTTP: remote failure")
)

Functions

func NewHandler

func NewHandler(config HandlerConfig) (http.Handler, error)

NewHandler borrows configured providers and protects their routes with one shared bearer token. That token grants all configured operations, including explicit reveal; per-actor/tenant authorization and server timeouts/limits are deployment-owned. Provider calls are synchronous and may run concurrently.

Types

type Client

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

Client implements remote management contracts.

func NewClient

func NewClient(config ClientConfig) (*Client, error)

NewClient accepts an operator-approved HTTP(S) origin, copies the HTTP client shallowly and rejects redirects. Zero response limit selects four mebibytes; nil HTTPClient selects a 30-second client. Transports remain shared; TLS, proxy and destination policy are caller-owned. Plain HTTP does not encrypt the token.

func (*Client) Execute

func (c *Client) Execute(
	ctx context.Context,
	request management.Command,
) (management.CommandResult, error)

Execute sends one validated management command to the configured worker endpoint and returns its validated acknowledgement.

Example
package main

import (
	"context"
	"fmt"
	"net/http/httptest"
	"time"

	"github.com/faustbrian/go-queue/management"
	"github.com/faustbrian/go-queue/managementhttp"
)

func main() {
	controller := exampleController{}
	handler, err := managementhttp.NewHandler(managementhttp.HandlerConfig{
		Token: "replace-with-a-secret", Controller: controller,
	})
	if err != nil {
		panic(err)
	}
	server := httptest.NewTLSServer(handler)
	defer server.Close()
	client, err := managementhttp.NewClient(managementhttp.ClientConfig{
		BaseURL: server.URL, Token: "replace-with-a-secret",
		HTTPClient: server.Client(),
	})
	if err != nil {
		panic(err)
	}
	requestedAt := time.Date(2026, 7, 16, 10, 0, 0, 0, time.UTC)
	result, err := client.Execute(context.Background(), management.Command{
		ID: "command-1", IdempotencyKey: "deployment-1", Actor: "operator-1",
		Reason:      "drain before deployment",
		Protocol:    management.ProtocolVersion{Major: 1},
		Action:      management.CommandDrain,
		Target:      management.Target{Kind: management.TargetWorker, Name: "worker-1"},
		RequestedAt: requestedAt, Deadline: requestedAt.Add(time.Minute),
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(result.Status)
}

type exampleController struct{}

func (exampleController) Execute(
	_ context.Context,
	command management.Command,
) (management.CommandResult, error) {
	return management.CommandResult{
		CommandID: command.ID, IdempotencyKey: command.IdempotencyKey,
		WorkerID: "worker-1", Protocol: command.Protocol,
		Status:      management.CommandAcknowledged,
		CompletedAt: command.RequestedAt.Add(time.Second),
	}, nil
}
Output:
acknowledged

func (*Client) Inspect

func (c *Client) Inspect(
	ctx context.Context,
	request management.InspectRequest,
) (management.JobRecord, error)

Inspect returns one remote record at explicit payload visibility.

func (*Client) ListDeadLetters

func (c *Client) ListDeadLetters(
	ctx context.Context,
	request management.PageRequest,
) (management.RecordPage, error)

ListDeadLetters returns one remote bounded dead-letter page.

func (*Client) ListFailures

func (c *Client) ListFailures(
	ctx context.Context,
	request management.PageRequest,
) (management.RecordPage, error)

ListFailures returns one remote bounded failure page.

Example
package main

import (
	"context"
	"fmt"
	"net/http/httptest"
	"time"

	"github.com/faustbrian/go-queue/management"
	"github.com/faustbrian/go-queue/managementhttp"
)

func main() {
	reader := exampleRecordReader{}
	handler, err := managementhttp.NewHandler(managementhttp.HandlerConfig{
		Token: "replace-with-a-secret", Records: reader,
	})
	if err != nil {
		panic(err)
	}
	server := httptest.NewTLSServer(handler)
	defer server.Close()
	client, err := managementhttp.NewClient(managementhttp.ClientConfig{
		BaseURL: server.URL, Token: "replace-with-a-secret",
		HTTPClient: server.Client(),
	})
	if err != nil {
		panic(err)
	}
	page, err := client.ListFailures(context.Background(), management.PageRequest{
		Limit: 25, Sort: management.SortOccurredAt,
		Direction: management.SortDescending,
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(page.Items[0].ID, page.Items[0].Payload.Visibility == management.PayloadHidden)
}

type exampleRecordReader struct{}

func (exampleRecordReader) ListFailures(
	context.Context,
	management.PageRequest,
) (management.RecordPage, error) {
	return management.RecordPage{Items: []management.JobRecord{{
		Kind: management.RecordFailure, ID: "failure-1", Backend: "example",
		Queue: "critical", OccurredAt: time.Unix(2, 0).UTC(), Attempts: 1,
		FailureCode: "handler_failed", Payload: management.Payload{Size: 7},
	}}}, nil
}

func (exampleRecordReader) ListDeadLetters(
	context.Context,
	management.PageRequest,
) (management.RecordPage, error) {
	return management.RecordPage{}, nil
}

func (exampleRecordReader) Inspect(
	context.Context,
	management.InspectRequest,
) (management.JobRecord, error) {
	return management.JobRecord{}, nil
}
Output:
failure-1 true

func (*Client) ListQueues

ListQueues returns one remote bounded queue-status page.

func (*Client) ListWorkers

ListWorkers returns one remote bounded worker-status page.

type ClientConfig

type ClientConfig struct {
	// BaseURL is an operator-approved http/https origin without credentials, query or fragment.
	BaseURL string
	// Token is the shared bearer credential; plain HTTP supplies no confidentiality.
	Token string
	// HTTPClient is shallow-copied; transport is shared and redirect behavior replaced.
	HTTPClient *http.Client
	// MaxResponseBytes bounds each response; zero selects four mebibytes.
	// Supply room for the internal one-byte oversize sentinel (not MaxInt64).
	MaxResponseBytes int64
}

ClientConfig provides the worker endpoint, credential, and response bound.

type Endpoint

type Endpoint struct {
	// ID is a unique endpoint label, not necessarily a reported worker ID.
	ID string
	// BaseURL is an operator-approved origin; duplicate normalized origins are rejected.
	BaseURL string
}

Endpoint is one stable worker-management transport target.

type EndpointResolver

type EndpointResolver interface {
	// ResolveEndpoints returns operator-trusted endpoints for this operation, honoring ctx.
	ResolveEndpoints(context.Context) ([]Endpoint, error)
}

EndpointResolver returns the current bounded worker-management fleet.

type EndpointResolverFunc

type EndpointResolverFunc func(context.Context) ([]Endpoint, error)

EndpointResolverFunc adapts a function into an EndpointResolver.

func (EndpointResolverFunc) ResolveEndpoints

func (resolve EndpointResolverFunc) ResolveEndpoints(ctx context.Context) ([]Endpoint, error)

ResolveEndpoints implements EndpointResolver.

type FleetClient

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

FleetClient aggregates bounded worker status and routes control to current endpoints.

func NewFleetClient

func NewFleetClient(config FleetClientConfig) (*FleetClient, error)

NewFleetClient creates a dynamic multi-endpoint management client.

func (*FleetClient) Execute

func (client *FleetClient) Execute(
	ctx context.Context,
	command management.Command,
) (management.CommandResult, error)

Execute routes worker targets after discovery; queue/group targets fan out and may return partial results.

func (*FleetClient) Inspect

func (client *FleetClient) Inspect(
	ctx context.Context,
	request management.InspectRequest,
) (management.JobRecord, error)

Inspect inspects the lexically first endpoint only at requested disclosure.

func (*FleetClient) ListDeadLetters

func (client *FleetClient) ListDeadLetters(
	ctx context.Context,
	request management.PageRequest,
) (management.RecordPage, error)

ListDeadLetters reads the lexically first endpoint only; it assumes a shared record store.

func (*FleetClient) ListFailures

func (client *FleetClient) ListFailures(
	ctx context.Context,
	request management.PageRequest,
) (management.RecordPage, error)

ListFailures reads the lexically first endpoint only; it does not aggregate record stores.

func (*FleetClient) ListQueues

func (client *FleetClient) ListQueues(
	ctx context.Context,
	request management.StatusPageRequest,
) (management.QueueStatusPage, error)

ListQueues fetches all endpoints and keeps the newest observation per queue name, even across backends.

func (*FleetClient) ListWorkers

func (client *FleetClient) ListWorkers(
	ctx context.Context,
	request management.StatusPageRequest,
) (management.WorkerStatusPage, error)

ListWorkers resolves/fetches all endpoints, then returns one sorted status page; cursors are not stable snapshots.

type FleetClientConfig

type FleetClientConfig struct {
	// Resolver is borrowed, called per operation, and must be safe for concurrent calls.
	Resolver EndpointResolver
	// Token is shared by all resolved endpoints; resolution is a trust boundary.
	Token string
	// HTTPClient is borrowed until NewClient shallow-copies it; do not mutate it concurrently.
	HTTPClient *http.Client
	// MaxResponseBytes bounds each endpoint response; zero selects NewClient defaults.
	MaxResponseBytes int64
}

FleetClientConfig configures dynamic authenticated worker-management access.

type HandlerConfig

type HandlerConfig struct {
	// Token is the shared credential granting every configured route, including explicit reveal.
	Token string
	// Status is a borrowed optional provider invoked by concurrent HTTP requests.
	Status management.StatusReader
	// Controller is a borrowed optional controller responsible for effects and idempotency.
	Controller management.Controller
	// Records is a borrowed optional reader responsible for metadata and disclosure.
	Records management.RecordReader
}

HandlerConfig provides the worker-side management services and shared transport credential.

Jump to

Keyboard shortcuts

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