requestdedup

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: 4 Imported by: 0

README

requestdedup

Request deduplication middleware for fh. Prevents duplicate processing of identical requests within a configurable time window.

Why

In distributed systems, clients may accidentally or maliciously send the same request multiple times due to retries, network duplicates, or UI double-clicks. Without deduplication, this causes duplicate charges, duplicate orders, duplicate email sends, and other side effects.

Features

  • SHA-256 based dedup keys (method + URL + body)
  • Configurable time window and max key count
  • Automatic LRU eviction when capacity is reached
  • Custom key function support
  • Returns 409 Conflict with original request metadata on duplicates

Usage

import "github.com/oarkflow/fh/mw/requestdedup"

app := fh.New()

dedup := requestdedup.New(requestdedup.Config{
    Window:  10 * time.Second,
    MaxKeys: 5000,
})

app.Post("/payments", dedup.Handler(), func(c fh.Ctx) error {
    // Only processes once per dedup window
    return c.JSON(fh.Map{"status": "success"})
})

Config

Field Type Default Description
Window time.Duration 5s Time window for dedup detection
MaxKeys int 10000 Max tracked keys before LRU eviction
KeyFunc func(Ctx) string SHA-256(method+URL+body) Custom key extraction
OnDuplicate func(Ctx, *Entry) error 409 Conflict JSON Custom duplicate handler

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewMiddleware

func NewMiddleware(cfg ...Config) fh.HandlerFunc

New is a convenience function that creates and returns middleware.

Types

type Config

type Config struct {
	// Window is the time window during which duplicate requests are detected.
	// Default: 5 seconds.
	Window time.Duration

	// MaxKeys is the maximum number of dedup keys stored before eviction.
	// Default: 10000.
	MaxKeys int

	// KeyFunc extracts the dedup key from the request. The default computes
	// SHA-256(method + path + body).
	KeyFunc func(fh.Ctx) string

	// OnDuplicate is called when a duplicate request is detected. The default
	// returns 409 Conflict with the original request's response.
	OnDuplicate func(fh.Ctx, *Entry) error
}

Config controls deduplication behavior.

type Deduplicator

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

Deduplicator tracks in-flight requests and deduplicates within a window.

func New

func New(cfg ...Config) *Deduplicator

New creates a deduplicator. If cfg is zero, defaults are applied.

func (*Deduplicator) Handler

func (d *Deduplicator) Handler() fh.HandlerFunc

Handler returns middleware that deduplicates requests within the configured window.

func (*Deduplicator) Stats

func (d *Deduplicator) Stats() (active int, evictions int)

Stats returns current deduplication statistics.

type Entry

type Entry struct {
	Key        string
	Status     int
	Body       []byte
	Headers    map[string]string
	ReceivedAt time.Time
	ExpiresAt  time.Time
}

Entry represents a tracked request.

Jump to

Keyboard shortcuts

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