Documentation
¶
Index ¶
- func DoWithResult[T any](ctx context.Context, r *Retryer, fn func() (T, error)) (T, error)
- func GetOrSetTyped[T any](c *Cache, key string, fn func() (T, error)) (T, error)
- func GetTyped[T any](c *Cache, key string) (T, bool)
- type Cache
- func (c *Cache) Clear()
- func (c *Cache) Close()
- func (c *Cache) Delete(key string)
- func (c *Cache) Get(key string) (interface{}, bool)
- func (c *Cache) GetOrSet(key string, fn func() (interface{}, error)) (interface{}, error)
- func (c *Cache) Keys() []string
- func (c *Cache) Set(key string, value interface{})
- func (c *Cache) SetWithTTL(key string, value interface{}, ttl time.Duration)
- func (c *Cache) Size() int
- type CacheEntry
- type RateLimiter
- type RetryConfig
- type Retryer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DoWithResult ¶
DoWithResult executes a function that returns a value with retry logic.
func GetOrSetTyped ¶
GetOrSetTyped is a typed version of GetOrSet.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache provides in-memory caching with TTL support.
func (*Cache) Get ¶
Get retrieves a value from the cache. Returns the value and true if found and not expired, otherwise nil and false.
func (*Cache) GetOrSet ¶
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) SetWithTTL ¶
SetWithTTL stores a value in the cache with a custom TTL.
type CacheEntry ¶
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.
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.