Documentation
¶
Index ¶
- type RedisSessionDataStorage
- func (s *RedisSessionDataStorage) Close() error
- func (s *RedisSessionDataStorage) Create(ctx context.Context, id string, metadata map[string]string) (bool, error)
- func (s *RedisSessionDataStorage) Delete(ctx context.Context, id string) error
- func (s *RedisSessionDataStorage) Load(ctx context.Context, id string) (map[string]string, error)
- func (s *RedisSessionDataStorage) LoadMetadata(ctx context.Context, id string) (map[string]string, error)
- func (s *RedisSessionDataStorage) Update(ctx context.Context, id string, metadata map[string]string) (bool, error)
- type RedisStorage
- func (s *RedisStorage) Close() error
- func (s *RedisStorage) Create(ctx context.Context, session transportsession.Session) error
- func (s *RedisStorage) Delete(ctx context.Context, id string) error
- func (*RedisStorage) DeleteExpired(_ context.Context, _ time.Time) error
- func (s *RedisStorage) DeleteIfOwner(ctx context.Context, id, expectedOwner string) (bool, error)
- func (s *RedisStorage) Load(ctx context.Context, id string) (transportsession.Session, error)
- func (s *RedisStorage) LoadIfOwner(ctx context.Context, id, expectedOwner string) (transportsession.Session, error)
- func (s *RedisStorage) LoadMetadata(ctx context.Context, id string) (map[string]string, error)
- func (s *RedisStorage) Store(ctx context.Context, session transportsession.Session) error
- func (s *RedisStorage) StoreIfOwner(ctx context.Context, session transportsession.Session, expectedOwner string) (bool, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type RedisSessionDataStorage ¶
type RedisSessionDataStorage struct {
// contains filtered or unexported fields
}
RedisSessionDataStorage implements DataStorage backed by Redis/Valkey.
Metadata is serialized as a JSON object and stored with a sliding-window TTL: each Create/Update/Load call refreshes the key's expiry so that active sessions never expire while they are in use.
Because only plain map[string]string is stored, there are no type assertions or deserialization tricks — Redis round-trips the map cleanly.
func NewRedisSessionDataStorage ¶
func NewRedisSessionDataStorage( ctx context.Context, cfg redisconn.Config, keyPrefix string, ttl time.Duration, ) (*RedisSessionDataStorage, error)
NewRedisSessionDataStorage constructs a RedisSessionDataStorage. Connection-mode topology, timeouts, TLS, and credentials are configured through cfg; keyPrefix is the per-tenant key prefix (e.g. "thv:vmcp:session:") and must end with ':' to avoid key collisions. ttl is the sliding-window expiry applied on every Create/Update and Load. The caller must call Close when done.
Connection-mode validation, timeout defaults, client construction (standalone, cluster, or sentinel), TLS plumbing, and connectivity verification are delegated to the shared toolhive-core redis package. A connection without verified TLS is allowed but logged at WARN.
func (*RedisSessionDataStorage) Close ¶
func (s *RedisSessionDataStorage) Close() error
Close closes the underlying Redis client.
func (*RedisSessionDataStorage) Create ¶
func (s *RedisSessionDataStorage) Create(ctx context.Context, id string, metadata map[string]string) (bool, error)
Create atomically creates session metadata only if the key does not already exist. Uses Redis SET NX (set-if-not-exists) to avoid the read-then-write TOCTOU race that a Load-then-unconditional-write pattern would introduce in multi-pod deployments.
func (*RedisSessionDataStorage) Delete ¶
func (s *RedisSessionDataStorage) Delete(ctx context.Context, id string) error
Delete removes the Redis key. A missing key is not an error.
func (*RedisSessionDataStorage) Load ¶
Load retrieves metadata from Redis and refreshes the key's TTL via GETEX. Returns ErrSessionNotFound if the key does not exist.
func (*RedisSessionDataStorage) LoadMetadata ¶
func (s *RedisSessionDataStorage) LoadMetadata(ctx context.Context, id string) (map[string]string, error)
LoadMetadata retrieves metadata from Redis with a plain GET, leaving the key's TTL unchanged. Returns ErrSessionNotFound if the key does not exist.
func (*RedisSessionDataStorage) Update ¶
func (s *RedisSessionDataStorage) Update(ctx context.Context, id string, metadata map[string]string) (bool, error)
Update overwrites session metadata only if the key already exists. Uses Redis SET XX (set-if-exists) to prevent resurrecting a session that was deleted by a concurrent Delete call (e.g. from another pod). Returns (true, nil) if updated, (false, nil) if the key was not found.
type RedisStorage ¶
type RedisStorage struct {
// contains filtered or unexported fields
}
RedisStorage implements the Storage interface backed by Redis.
func NewRedisStorage ¶
func NewRedisStorage(ctx context.Context, cfg redisconn.Config, keyPrefix string, ttl time.Duration) (*RedisStorage, error)
NewRedisStorage constructs a RedisStorage. Connection-mode topology, timeouts, TLS, and credentials are configured through cfg; keyPrefix is the per-tenant key prefix (e.g. "thv:proxy:session:") and must end with ':' to avoid key collisions. ttl is the sliding-window expiry applied to every key on Store and refreshed on every Load (sliding window): sessions remain valid indefinitely while actively used; revocation requires an explicit Delete call.
Connection-mode validation, timeout defaults, client construction (standalone, cluster, or sentinel), TLS plumbing, and connectivity verification are delegated to the shared toolhive-core redisconn package. A connection without verified TLS is allowed but logged at WARN.
func (*RedisStorage) Close ¶
func (s *RedisStorage) Close() error
Close closes the underlying Redis client connection.
func (*RedisStorage) Create ¶
func (s *RedisStorage) Create(ctx context.Context, session transportsession.Session) error
Create persists the complete session atomically with SET NX and an expiry.
func (*RedisStorage) Delete ¶
func (s *RedisStorage) Delete(ctx context.Context, id string) error
Delete removes the Redis key. A missing key is not an error.
func (*RedisStorage) DeleteExpired ¶
DeleteExpired is a no-op. Redis TTL handles key expiry natively.
func (*RedisStorage) DeleteIfOwner ¶
DeleteIfOwner atomically removes a session only while its ownership binding matches.
func (*RedisStorage) Load ¶
func (s *RedisStorage) Load(ctx context.Context, id string) (transportsession.Session, error)
Load retrieves a session by ID. Returns ErrSessionNotFound when the key does not exist. The Redis eviction TTL is refreshed atomically via GETEX on every read so that active sessions are not evicted between accesses. The session's UpdatedAt timestamp is not modified.
Lifetime note: this implements a sliding-window TTL. A session accessed at least once per TTL window will never expire and can live indefinitely while the client keeps making requests. Session revocation therefore depends entirely on explicit Delete calls (e.g. on logout or token invalidation); there is no absolute maximum session lifetime enforced here. If a hard cap is required in future, a MaxLifetime field checked against the session's CreatedAt would be the path forward.
func (*RedisStorage) LoadIfOwner ¶
func (s *RedisStorage) LoadIfOwner(ctx context.Context, id, expectedOwner string) (transportsession.Session, error)
LoadIfOwner atomically validates ownership, refreshes the TTL, and returns the validated record.
func (*RedisStorage) LoadMetadata ¶
LoadMetadata retrieves authoritative metadata with GET, without refreshing the key TTL.
func (*RedisStorage) Store ¶
func (s *RedisStorage) Store(ctx context.Context, session transportsession.Session) error
Store serializes the session and persists it with SET … EX, refreshing the TTL on every call.
func (*RedisStorage) StoreIfOwner ¶
func (s *RedisStorage) StoreIfOwner(ctx context.Context, session transportsession.Session, expectedOwner string) (bool, error)
StoreIfOwner atomically replaces a session only while its ownership binding matches.