backpressure

package
v0.0.26 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2026 License: MIT Imports: 3 Imported by: 0

README

Backpressure Middleware

What it does

Rejects or slows request admission when queue or worker saturation indicates that the service cannot safely accept more work.

How to implement

package main

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

func main() {
	app := fh.New()
	app.Use(backpressure.New(backpressure.Config{}))

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

Impact

Prevents unbounded memory growth and protects downstream durable queues. It can return 503/429 instead of allowing the process to overload.

Ordering guidance

Place before endpoints that enqueue background work. Combine with load shedding and retry-after responses.

Production considerations

Set thresholds according to queue depth, lag, and worker capacity. Monitor rejection counts, queue lag, and DLQ growth.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(cfg Config) fh.HandlerFunc

Types

type Config

type Config struct {
	Queue         QueueStatsProvider
	MaxPending    int
	MaxProcessing int
	RetryAfter    time.Duration
	Reject        RejectHandler
}

type QueueStatsProvider

type QueueStatsProvider interface{ Stats() (fh.QueueStats, error) }

type RejectHandler

type RejectHandler func(fh.Ctx, fh.QueueStats) error

Jump to

Keyboard shortcuts

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