feat: game lobby service
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
// Package runtimemanager provides the Redis Streams write-only adapter
|
||||
// for ports.RuntimeManager. The publisher emits one event per call to
|
||||
// the configured start-jobs or stop-jobs stream so Runtime Manager (when
|
||||
// implemented) can consume them via XREAD.
|
||||
//
|
||||
// The two streams are intentionally separate: each one carries a single
|
||||
// command kind, which keeps the consumer-side logic in Runtime Manager
|
||||
// simple and avoids a `kind` discriminator inside the message body.
|
||||
package runtimemanager
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"galaxy/lobby/internal/ports"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// Config groups the parameters required to construct a Publisher.
|
||||
type Config struct {
|
||||
// Client appends events to Redis Streams.
|
||||
Client *redis.Client
|
||||
|
||||
// StartJobsStream stores the Redis Stream key receiving start jobs.
|
||||
StartJobsStream string
|
||||
|
||||
// StopJobsStream stores the Redis Stream key receiving stop jobs.
|
||||
StopJobsStream string
|
||||
|
||||
// Clock supplies the wall-clock used for the requested-at timestamp.
|
||||
// Defaults to time.Now when nil.
|
||||
Clock func() time.Time
|
||||
}
|
||||
|
||||
// Validate reports whether cfg stores a usable Publisher configuration.
|
||||
func (cfg Config) Validate() error {
|
||||
switch {
|
||||
case cfg.Client == nil:
|
||||
return errors.New("runtime manager publisher: nil redis client")
|
||||
case strings.TrimSpace(cfg.StartJobsStream) == "":
|
||||
return errors.New("runtime manager publisher: start jobs stream must not be empty")
|
||||
case strings.TrimSpace(cfg.StopJobsStream) == "":
|
||||
return errors.New("runtime manager publisher: stop jobs stream must not be empty")
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// Publisher implements ports.RuntimeManager on top of Redis Streams.
|
||||
type Publisher struct {
|
||||
client *redis.Client
|
||||
startJobsStream string
|
||||
stopJobsStream string
|
||||
clock func() time.Time
|
||||
}
|
||||
|
||||
// NewPublisher constructs a Publisher from cfg.
|
||||
func NewPublisher(cfg Config) (*Publisher, error) {
|
||||
if err := cfg.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
clock := cfg.Clock
|
||||
if clock == nil {
|
||||
clock = time.Now
|
||||
}
|
||||
return &Publisher{
|
||||
client: cfg.Client,
|
||||
startJobsStream: cfg.StartJobsStream,
|
||||
stopJobsStream: cfg.StopJobsStream,
|
||||
clock: clock,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// PublishStartJob appends one start-job event for gameID to the
|
||||
// configured start-jobs stream.
|
||||
func (publisher *Publisher) PublishStartJob(ctx context.Context, gameID string) error {
|
||||
return publisher.publish(ctx, "publish start job", publisher.startJobsStream, gameID)
|
||||
}
|
||||
|
||||
// PublishStopJob appends one stop-job event for gameID to the configured
|
||||
// stop-jobs stream. In Lobby publishes stop jobs only from the
|
||||
// orphan-container path inside the runtimejobresult worker.
|
||||
func (publisher *Publisher) PublishStopJob(ctx context.Context, gameID string) error {
|
||||
return publisher.publish(ctx, "publish stop job", publisher.stopJobsStream, gameID)
|
||||
}
|
||||
|
||||
func (publisher *Publisher) publish(ctx context.Context, op, stream, gameID string) error {
|
||||
if publisher == nil || publisher.client == nil {
|
||||
return fmt.Errorf("%s: nil publisher", op)
|
||||
}
|
||||
if ctx == nil {
|
||||
return fmt.Errorf("%s: nil context", op)
|
||||
}
|
||||
if strings.TrimSpace(gameID) == "" {
|
||||
return fmt.Errorf("%s: game id must not be empty", op)
|
||||
}
|
||||
|
||||
values := map[string]any{
|
||||
"game_id": gameID,
|
||||
"requested_at_ms": publisher.clock().UTC().UnixMilli(),
|
||||
}
|
||||
if _, err := publisher.client.XAdd(ctx, &redis.XAddArgs{
|
||||
Stream: stream,
|
||||
Values: values,
|
||||
}).Result(); err != nil {
|
||||
return fmt.Errorf("%s: xadd: %w", op, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Compile-time assertion: Publisher implements ports.RuntimeManager.
|
||||
var _ ports.RuntimeManager = (*Publisher)(nil)
|
||||
@@ -0,0 +1,110 @@
|
||||
package runtimemanager_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"galaxy/lobby/internal/adapters/runtimemanager"
|
||||
|
||||
"github.com/alicebob/miniredis/v2"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func newTestPublisher(t *testing.T, clock func() time.Time) (*runtimemanager.Publisher, *miniredis.Miniredis, *redis.Client) {
|
||||
t.Helper()
|
||||
|
||||
server := miniredis.RunT(t)
|
||||
client := redis.NewClient(&redis.Options{Addr: server.Addr()})
|
||||
t.Cleanup(func() { _ = client.Close() })
|
||||
|
||||
publisher, err := runtimemanager.NewPublisher(runtimemanager.Config{
|
||||
Client: client,
|
||||
StartJobsStream: "runtime:start_jobs",
|
||||
StopJobsStream: "runtime:stop_jobs",
|
||||
Clock: clock,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
return publisher, server, client
|
||||
}
|
||||
|
||||
func TestPublisherRejectsInvalidConfig(t *testing.T) {
|
||||
_, err := runtimemanager.NewPublisher(runtimemanager.Config{
|
||||
StartJobsStream: "runtime:start_jobs",
|
||||
StopJobsStream: "runtime:stop_jobs",
|
||||
})
|
||||
require.Error(t, err)
|
||||
|
||||
server := miniredis.RunT(t)
|
||||
client := redis.NewClient(&redis.Options{Addr: server.Addr()})
|
||||
t.Cleanup(func() { _ = client.Close() })
|
||||
|
||||
_, err = runtimemanager.NewPublisher(runtimemanager.Config{
|
||||
Client: client,
|
||||
StopJobsStream: "runtime:stop_jobs",
|
||||
})
|
||||
require.Error(t, err)
|
||||
|
||||
_, err = runtimemanager.NewPublisher(runtimemanager.Config{
|
||||
Client: client,
|
||||
StartJobsStream: "runtime:start_jobs",
|
||||
})
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestPublishStartJobAppendsToStartStream(t *testing.T) {
|
||||
now := time.Date(2026, 4, 25, 12, 0, 0, 0, time.UTC)
|
||||
publisher, _, client := newTestPublisher(t, func() time.Time { return now })
|
||||
|
||||
require.NoError(t, publisher.PublishStartJob(context.Background(), "game-1"))
|
||||
|
||||
entries, err := client.XRange(context.Background(), "runtime:start_jobs", "-", "+").Result()
|
||||
require.NoError(t, err)
|
||||
require.Len(t, entries, 1)
|
||||
assert.Equal(t, "game-1", entries[0].Values["game_id"])
|
||||
assert.Equal(t, strconv.FormatInt(now.UnixMilli(), 10), entries[0].Values["requested_at_ms"])
|
||||
|
||||
stop, err := client.XLen(context.Background(), "runtime:stop_jobs").Result()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, int64(0), stop, "stop stream must remain empty")
|
||||
}
|
||||
|
||||
func TestPublishStopJobAppendsToStopStream(t *testing.T) {
|
||||
now := time.Date(2026, 4, 25, 13, 0, 0, 0, time.UTC)
|
||||
publisher, _, client := newTestPublisher(t, func() time.Time { return now })
|
||||
|
||||
require.NoError(t, publisher.PublishStopJob(context.Background(), "game-2"))
|
||||
|
||||
entries, err := client.XRange(context.Background(), "runtime:stop_jobs", "-", "+").Result()
|
||||
require.NoError(t, err)
|
||||
require.Len(t, entries, 1)
|
||||
assert.Equal(t, "game-2", entries[0].Values["game_id"])
|
||||
assert.Equal(t, strconv.FormatInt(now.UnixMilli(), 10), entries[0].Values["requested_at_ms"])
|
||||
|
||||
startLen, err := client.XLen(context.Background(), "runtime:start_jobs").Result()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, int64(0), startLen, "start stream must remain empty")
|
||||
}
|
||||
|
||||
func TestPublishRejectsEmptyGameID(t *testing.T) {
|
||||
publisher, _, _ := newTestPublisher(t, nil)
|
||||
|
||||
require.Error(t, publisher.PublishStartJob(context.Background(), ""))
|
||||
require.Error(t, publisher.PublishStopJob(context.Background(), " "))
|
||||
}
|
||||
|
||||
func TestPublishRejectsNilContext(t *testing.T) {
|
||||
publisher, _, _ := newTestPublisher(t, nil)
|
||||
|
||||
require.Error(t, publisher.PublishStartJob(nilContext(), "game-1"))
|
||||
require.Error(t, publisher.PublishStopJob(nilContext(), "game-1"))
|
||||
}
|
||||
|
||||
// nilContext returns an explicit untyped nil to exercise the defensive
|
||||
// nil-context guards on Publisher methods. The indirection silences the
|
||||
// SA1012 hint where it is intentional.
|
||||
func nilContext() context.Context { return nil }
|
||||
Reference in New Issue
Block a user