storage

package
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package storage provides storage backend implementations for reputation scores.

The storage package provides implementations of the reputation.Storage interface:

  • memory: In-memory storage (single instance, lost on restart)
  • redis: Redis-based storage (shared across instances, persistent)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MemoryStorage

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

MemoryStorage implements Storage using an in-memory map. This implementation is suitable for single-instance deployments or as a fallback when Redis is unavailable.

Note: Data is lost on process restart and is not shared across instances.

func NewMemoryStorage

func NewMemoryStorage(ttl time.Duration) *MemoryStorage

NewMemoryStorage creates a new in-memory storage. If ttl is zero, entries never expire.

func (*MemoryStorage) Cleanup

func (m *MemoryStorage) Cleanup()

Cleanup removes expired entries from the storage. This is called periodically to prevent memory bloat.

func (*MemoryStorage) Close

func (m *MemoryStorage) Close() error

Close releases resources held by the storage.

func (*MemoryStorage) Delete

Delete removes the score for an endpoint.

func (*MemoryStorage) Get

Get retrieves the score for an endpoint.

func (*MemoryStorage) GetMultiple

GetMultiple retrieves scores for multiple endpoints.

func (*MemoryStorage) Len

func (m *MemoryStorage) Len() int

Len returns the number of entries in the storage. This is primarily for testing purposes.

func (*MemoryStorage) List

func (m *MemoryStorage) List(ctx context.Context, serviceID string) ([]reputation.EndpointKey, error)

List returns all stored endpoint keys, optionally filtered by service ID.

func (*MemoryStorage) Set

Set stores or updates the score for an endpoint.

func (*MemoryStorage) SetMultiple

func (m *MemoryStorage) SetMultiple(ctx context.Context, scores map[reputation.EndpointKey]reputation.Score) error

SetMultiple stores or updates scores for multiple endpoints.

type RedisConfig

type RedisConfig = reputation.RedisConfig

RedisConfig is an alias to reputation.RedisConfig for use in the storage package.

type RedisStorage

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

RedisStorage implements Storage using Redis as the backend. This implementation is suitable for multi-instance deployments where reputation data needs to be shared across PATH instances.

Keys are stored as Redis Hashes with the format: {keyPrefix}{serviceID}:{endpointAddr}

Each hash contains the following fields: - value: the score value (float64) - last_updated: Unix timestamp of last update (int64) - success_count: total successful requests (int64) - error_count: total failed requests (int64)

func NewRedisStorage

func NewRedisStorage(ctx context.Context, config reputation.RedisConfig, ttl time.Duration) (*RedisStorage, error)

NewRedisStorage creates a new Redis-backed storage. It validates the connection by sending a PING command.

func (*RedisStorage) Close

func (r *RedisStorage) Close() error

Close releases resources held by the storage.

func (*RedisStorage) Delete

Delete removes the score for an endpoint.

func (*RedisStorage) Get

Get retrieves the score for an endpoint.

func (*RedisStorage) GetMultiple

GetMultiple retrieves scores for multiple endpoints using a pipeline.

func (*RedisStorage) List

func (r *RedisStorage) List(ctx context.Context, serviceID string) ([]reputation.EndpointKey, error)

List returns all stored endpoint keys, optionally filtered by service ID. Uses SCAN for production safety (doesn't block the server).

func (*RedisStorage) Set

Set stores or updates the score for an endpoint.

func (*RedisStorage) SetMultiple

func (r *RedisStorage) SetMultiple(ctx context.Context, scores map[reputation.EndpointKey]reputation.Score) error

SetMultiple stores or updates scores for multiple endpoints using a pipeline.

type StorageConfig

type StorageConfig struct {
	// Type specifies the storage backend ("memory" or "redis").
	Type string `yaml:"type"`

	// TTL is the time-to-live for stored scores.
	// After this duration, scores are considered stale and may be removed.
	// Zero means no expiration.
	TTL time.Duration `yaml:"ttl"`

	// Redis-specific configuration (only used when Type is "redis").
	Redis *RedisConfig `yaml:"redis,omitempty"`
}

StorageConfig holds configuration for storage backends.

func DefaultStorageConfig

func DefaultStorageConfig() StorageConfig

DefaultStorageConfig returns a StorageConfig with sensible defaults.

Jump to

Keyboard shortcuts

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