throttle

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package throttle provides a flush rate limiter to prevent thundering herd.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Throttle

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

Throttle prevents thundering herd flushes by enforcing a minimum cooldown between flush operations. Without throttling, every Record() call above a batch threshold can spawn a new flush goroutine, creating unbounded HTTP POSTs.

Usage: call TryFlush() before spawning a flush goroutine. It atomically claims a flush slot if the cooldown has elapsed. Call RecordFlush() from the flush function itself (e.g., ticker-triggered flushes that bypass TryFlush).

func New

func New(cooldown time.Duration) *Throttle

New creates a Throttle with the given minimum interval between flushes.

func (*Throttle) RecordFlush

func (t *Throttle) RecordFlush()

RecordFlush updates the last flush timestamp. Use this for flushes triggered by the background ticker (which bypass TryFlush).

func (*Throttle) TryFlush

func (t *Throttle) TryFlush() bool

TryFlush atomically checks if the cooldown has elapsed and claims the flush slot. Returns true if the caller should proceed with flushing.

Jump to

Keyboard shortcuts

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