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 ¶
- type MemoryStorage
- func (m *MemoryStorage) Cleanup()
- func (m *MemoryStorage) Close() error
- func (m *MemoryStorage) Delete(ctx context.Context, key reputation.EndpointKey) error
- func (m *MemoryStorage) Get(ctx context.Context, key reputation.EndpointKey) (reputation.Score, error)
- func (m *MemoryStorage) GetMultiple(ctx context.Context, keys []reputation.EndpointKey) (map[reputation.EndpointKey]reputation.Score, error)
- func (m *MemoryStorage) Len() int
- func (m *MemoryStorage) List(ctx context.Context, serviceID string) ([]reputation.EndpointKey, error)
- func (m *MemoryStorage) Set(ctx context.Context, key reputation.EndpointKey, score reputation.Score) error
- func (m *MemoryStorage) SetMultiple(ctx context.Context, scores map[reputation.EndpointKey]reputation.Score) error
- type RedisConfig
- type RedisStorage
- func (r *RedisStorage) Close() error
- func (r *RedisStorage) Delete(ctx context.Context, key reputation.EndpointKey) error
- func (r *RedisStorage) Get(ctx context.Context, key reputation.EndpointKey) (reputation.Score, error)
- func (r *RedisStorage) GetMultiple(ctx context.Context, keys []reputation.EndpointKey) (map[reputation.EndpointKey]reputation.Score, error)
- func (r *RedisStorage) List(ctx context.Context, serviceID string) ([]reputation.EndpointKey, error)
- func (r *RedisStorage) Set(ctx context.Context, key reputation.EndpointKey, score reputation.Score) error
- func (r *RedisStorage) SetMultiple(ctx context.Context, scores map[reputation.EndpointKey]reputation.Score) error
- type StorageConfig
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 ¶
func (m *MemoryStorage) Delete(ctx context.Context, key reputation.EndpointKey) error
Delete removes the score for an endpoint.
func (*MemoryStorage) Get ¶
func (m *MemoryStorage) Get(ctx context.Context, key reputation.EndpointKey) (reputation.Score, error)
Get retrieves the score for an endpoint.
func (*MemoryStorage) GetMultiple ¶
func (m *MemoryStorage) GetMultiple(ctx context.Context, keys []reputation.EndpointKey) (map[reputation.EndpointKey]reputation.Score, error)
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 ¶
func (m *MemoryStorage) Set(ctx context.Context, key reputation.EndpointKey, score reputation.Score) error
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 ¶
func (r *RedisStorage) Delete(ctx context.Context, key reputation.EndpointKey) error
Delete removes the score for an endpoint.
func (*RedisStorage) Get ¶
func (r *RedisStorage) Get(ctx context.Context, key reputation.EndpointKey) (reputation.Score, error)
Get retrieves the score for an endpoint.
func (*RedisStorage) GetMultiple ¶
func (r *RedisStorage) GetMultiple(ctx context.Context, keys []reputation.EndpointKey) (map[reputation.EndpointKey]reputation.Score, error)
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 ¶
func (r *RedisStorage) Set(ctx context.Context, key reputation.EndpointKey, score reputation.Score) error
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.