middleware

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DoWithResult

func DoWithResult[T any](ctx context.Context, r *Retryer, fn func() (T, error)) (T, error)

DoWithResult executes a function that returns a value with retry logic.

func GetOrSetTyped

func GetOrSetTyped[T any](c *Cache, key string, fn func() (T, error)) (T, error)

GetOrSetTyped is a typed version of GetOrSet.

func GetTyped

func GetTyped[T any](c *Cache, key string) (T, bool)

GetTyped retrieves a typed value from the cache.

Types

type Cache

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

Cache provides in-memory caching with TTL support.

func NewCache

func NewCache(defaultTTL time.Duration) *Cache

NewCache creates a new cache with the specified default TTL.

func (*Cache) Clear

func (c *Cache) Clear()

Clear removes all values from the cache.

func (*Cache) Close

func (c *Cache) Close()

Close stops the background cleanup goroutine.

func (*Cache) Delete

func (c *Cache) Delete(key string)

Delete removes a value from the cache.

func (*Cache) Get

func (c *Cache) Get(key string) (interface{}, bool)

Get retrieves a value from the cache. Returns the value and true if found and not expired, otherwise nil and false.

func (*Cache) GetOrSet

func (c *Cache) GetOrSet(key string, fn func() (interface{}, error)) (interface{}, error)

GetOrSet returns the cached value if it exists, otherwise calls the function to compute the value, stores it in the cache, and returns it.

func (*Cache) Keys

func (c *Cache) Keys() []string

Keys returns all keys in the cache (including expired ones).

func (*Cache) Set

func (c *Cache) Set(key string, value interface{})

Set stores a value in the cache with the default TTL.

func (*Cache) SetWithTTL

func (c *Cache) SetWithTTL(key string, value interface{}, ttl time.Duration)

SetWithTTL stores a value in the cache with a custom TTL.

func (*Cache) Size

func (c *Cache) Size() int

Size returns the number of entries in the cache (including expired ones).

type CacheEntry

type CacheEntry struct {
	Value      interface{}
	Expiration time.Time
}

CacheEntry represents a cached value with expiration.

func (CacheEntry) IsExpired

func (e CacheEntry) IsExpired() bool

IsExpired returns true if the cache entry has expired.

type RateLimiter

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

RateLimiter provides rate limiting for API requests using a token bucket algorithm.

func NewRateLimiter

func NewRateLimiter(requestsPerSecond float64) *RateLimiter

NewRateLimiter creates a new rate limiter with the specified rate (requests per second). The bucket size (burst capacity) is set to the rate by default.

func NewRateLimiterWithBurst

func NewRateLimiterWithBurst(requestsPerSecond, burstCapacity float64) *RateLimiter

NewRateLimiterWithBurst creates a new rate limiter with custom burst capacity.

func (*RateLimiter) Available

func (r *RateLimiter) Available() float64

Available returns the current number of available tokens.

func (*RateLimiter) Rate

func (r *RateLimiter) Rate() float64

Rate returns the rate limit in requests per second.

func (*RateLimiter) TryAcquire

func (r *RateLimiter) TryAcquire() bool

TryAcquire attempts to acquire a token without blocking. Returns true if a token was acquired, false otherwise.

func (*RateLimiter) Wait

func (r *RateLimiter) Wait(ctx context.Context) error

Wait blocks until a token is available or the context is cancelled. Returns nil if a token was acquired, or the context error if cancelled.

type RetryConfig

type RetryConfig struct {
	// MaxRetries is the maximum number of retry attempts.
	MaxRetries int

	// BaseDelay is the initial delay before the first retry.
	BaseDelay time.Duration

	// MaxDelay is the maximum delay between retries.
	MaxDelay time.Duration

	// Multiplier is the factor by which the delay increases after each retry.
	Multiplier float64

	// Jitter adds randomness to the delay to prevent thundering herd.
	// Value between 0 (no jitter) and 1 (full jitter).
	Jitter float64

	// ShouldRetry is a function that determines if an error is retryable.
	// If nil, all errors are considered retryable.
	ShouldRetry func(error) bool
}

RetryConfig holds configuration for retry behavior.

func DefaultRetryConfig

func DefaultRetryConfig() RetryConfig

DefaultRetryConfig returns a RetryConfig with sensible defaults.

type Retryer

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

Retryer provides retry functionality with exponential backoff.

func NewRetryer

func NewRetryer(config RetryConfig) *Retryer

NewRetryer creates a new Retryer with the given configuration.

func (*Retryer) Config

func (r *Retryer) Config() RetryConfig

Config returns a copy of the retry configuration.

func (*Retryer) Do

func (r *Retryer) Do(ctx context.Context, fn func() error) error

Do executes the given function with retry logic. Returns the result of the function or the last error if all retries fail.

Jump to

Keyboard shortcuts

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