57 lines
1.4 KiB
Go
57 lines
1.4 KiB
Go
// Package streamoffsetstub provides an in-process ports.StreamOffsetStore
|
|
// used by worker-level tests that do not need Redis. Production code
|
|
// never wires this stub.
|
|
package streamoffsetstub
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"sync"
|
|
|
|
"galaxy/lobby/internal/ports"
|
|
)
|
|
|
|
// Store is a concurrency-safe in-memory ports.StreamOffsetStore.
|
|
type Store struct {
|
|
mu sync.Mutex
|
|
offsets map[string]string
|
|
}
|
|
|
|
// NewStore constructs an empty Store.
|
|
func NewStore() *Store {
|
|
return &Store{offsets: make(map[string]string)}
|
|
}
|
|
|
|
// Load returns the last saved entry id for streamLabel.
|
|
func (store *Store) Load(ctx context.Context, streamLabel string) (string, bool, error) {
|
|
if ctx == nil {
|
|
return "", false, errors.New("load offset: nil context")
|
|
}
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
value, ok := store.offsets[streamLabel]
|
|
return value, ok, nil
|
|
}
|
|
|
|
// Save records entryID as the offset for streamLabel.
|
|
func (store *Store) Save(ctx context.Context, streamLabel, entryID string) error {
|
|
if ctx == nil {
|
|
return errors.New("save offset: nil context")
|
|
}
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
store.offsets[streamLabel] = entryID
|
|
return nil
|
|
}
|
|
|
|
// Set forces the in-memory value for streamLabel; useful in tests to
|
|
// pre-populate state.
|
|
func (store *Store) Set(streamLabel, entryID string) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
store.offsets[streamLabel] = entryID
|
|
}
|
|
|
|
// Compile-time interface assertion.
|
|
var _ ports.StreamOffsetStore = (*Store)(nil)
|