bulkhead

package
v0.0.25 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2026 License: MIT Imports: 4 Imported by: 0

README

Bulkhead Middleware

What it does

Limits concurrent execution globally or by key, isolating expensive routes, tenants, or dependencies so one hot path cannot consume the whole server.

How to implement

package main

import (
	"github.com/oarkflow/fh"
	"github.com/oarkflow/fh/mw/bulkhead"
)

func main() {
	app := fh.New()
	app.Use(bulkhead.New(bulkhead.Config{MaxConcurrent: 128}))

	app.Get("/", func(c fh.Ctx) error {
		return c.Status(fh.StatusOK).SendString("ok")
	})
}

Impact

Improves fault isolation. Excess requests are rejected instead of increasing latency for every request.

Ordering guidance

Place around expensive route groups or before handlers that call constrained dependencies. Use route-level bulkheads for high-cost operations.

Production considerations

Tune limits per dependency capacity. Expose metrics for in-flight, accepted, and rejected requests. Combine with timeouts and circuit breakers.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultReject

func DefaultReject(c fh.Ctx, r Result) error

func New

func New(cfg Config) fh.HandlerFunc

Types

type Config

type Config struct {
	MaxConcurrent int
	Queue         int
	Timeout       time.Duration
	KeyFunc       KeyFunc
	Reject        RejectHandler
	Headers       bool
}

type KeyFunc

type KeyFunc func(fh.Ctx) string

type RejectHandler

type RejectHandler func(fh.Ctx, Result) error

type Result

type Result struct {
	Key        string
	Limit      int
	InFlight   int
	RetryAfter time.Duration
}

Jump to

Keyboard shortcuts

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