feat: use postgres
This commit is contained in:
@@ -6,28 +6,23 @@ import (
|
||||
|
||||
"galaxy/lobby/internal/config"
|
||||
"galaxy/lobby/internal/telemetry"
|
||||
"galaxy/redisconn"
|
||||
|
||||
"github.com/redis/go-redis/extra/redisotel/v9"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// newRedisClient builds a Redis client wired with the configured timeouts
|
||||
// and TLS settings taken from cfg.
|
||||
// newRedisClient builds the master Redis client from cfg via the shared
|
||||
// `pkg/redisconn` helper. Replica clients are not opened in this iteration
|
||||
// per ARCHITECTURE.md §Persistence Backends; they will be wired when read
|
||||
// routing is introduced.
|
||||
func newRedisClient(cfg config.RedisConfig) *redis.Client {
|
||||
return redis.NewClient(&redis.Options{
|
||||
Addr: cfg.Addr,
|
||||
Username: cfg.Username,
|
||||
Password: cfg.Password,
|
||||
DB: cfg.DB,
|
||||
TLSConfig: cfg.TLSConfig(),
|
||||
DialTimeout: cfg.OperationTimeout,
|
||||
ReadTimeout: cfg.OperationTimeout,
|
||||
WriteTimeout: cfg.OperationTimeout,
|
||||
})
|
||||
return redisconn.NewMasterClient(cfg.Conn)
|
||||
}
|
||||
|
||||
// instrumentRedisClient attaches the OpenTelemetry tracing and metrics
|
||||
// instrumentation to client when telemetryRuntime is available.
|
||||
// instrumentation to client when telemetryRuntime is available. The actual
|
||||
// instrumentation lives in `pkg/redisconn` so every Galaxy service shares one
|
||||
// surface.
|
||||
func instrumentRedisClient(client *redis.Client, telemetryRuntime *telemetry.Runtime) error {
|
||||
if client == nil {
|
||||
return fmt.Errorf("instrument redis client: nil client")
|
||||
@@ -35,37 +30,14 @@ func instrumentRedisClient(client *redis.Client, telemetryRuntime *telemetry.Run
|
||||
if telemetryRuntime == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := redisotel.InstrumentTracing(
|
||||
client,
|
||||
redisotel.WithTracerProvider(telemetryRuntime.TracerProvider()),
|
||||
redisotel.WithDBStatement(false),
|
||||
); err != nil {
|
||||
return fmt.Errorf("instrument redis client tracing: %w", err)
|
||||
}
|
||||
if err := redisotel.InstrumentMetrics(
|
||||
client,
|
||||
redisotel.WithMeterProvider(telemetryRuntime.MeterProvider()),
|
||||
); err != nil {
|
||||
return fmt.Errorf("instrument redis client metrics: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
return redisconn.Instrument(client,
|
||||
redisconn.WithTracerProvider(telemetryRuntime.TracerProvider()),
|
||||
redisconn.WithMeterProvider(telemetryRuntime.MeterProvider()),
|
||||
)
|
||||
}
|
||||
|
||||
// pingRedis performs a single Redis PING bounded by cfg.OperationTimeout to
|
||||
// confirm that the configured Redis endpoint is reachable at startup.
|
||||
// pingRedis performs a single Redis PING bounded by cfg.Conn.OperationTimeout
|
||||
// to confirm that the configured Redis endpoint is reachable at startup.
|
||||
func pingRedis(ctx context.Context, cfg config.RedisConfig, client *redis.Client) error {
|
||||
if client == nil {
|
||||
return fmt.Errorf("ping redis: nil client")
|
||||
}
|
||||
|
||||
pingCtx, cancel := context.WithTimeout(ctx, cfg.OperationTimeout)
|
||||
defer cancel()
|
||||
|
||||
if err := client.Ping(pingCtx).Err(); err != nil {
|
||||
return fmt.Errorf("ping redis: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
return redisconn.Ping(ctx, client, cfg.Conn.OperationTimeout)
|
||||
}
|
||||
|
||||
@@ -6,20 +6,28 @@ import (
|
||||
"time"
|
||||
|
||||
"galaxy/lobby/internal/config"
|
||||
"galaxy/redisconn"
|
||||
|
||||
"github.com/alicebob/miniredis/v2"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func newTestRedisCfg(addr string) config.RedisConfig {
|
||||
return config.RedisConfig{
|
||||
Conn: redisconn.Config{
|
||||
MasterAddr: addr,
|
||||
Password: "test",
|
||||
OperationTimeout: time.Second,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func TestPingRedisSucceedsAgainstMiniredis(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
server := miniredis.RunT(t)
|
||||
|
||||
redisCfg := config.RedisConfig{
|
||||
Addr: server.Addr(),
|
||||
OperationTimeout: time.Second,
|
||||
}
|
||||
redisCfg := newTestRedisCfg(server.Addr())
|
||||
client := newRedisClient(redisCfg)
|
||||
t.Cleanup(func() { _ = client.Close() })
|
||||
|
||||
@@ -31,10 +39,7 @@ func TestPingRedisReturnsErrorWhenClosed(t *testing.T) {
|
||||
|
||||
server := miniredis.RunT(t)
|
||||
|
||||
redisCfg := config.RedisConfig{
|
||||
Addr: server.Addr(),
|
||||
OperationTimeout: time.Second,
|
||||
}
|
||||
redisCfg := newTestRedisCfg(server.Addr())
|
||||
client := newRedisClient(redisCfg)
|
||||
require.NoError(t, client.Close())
|
||||
|
||||
@@ -45,7 +50,7 @@ func TestPingRedisReturnsErrorWhenClosed(t *testing.T) {
|
||||
func TestPingRedisNilClient(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
err := pingRedis(context.Background(), config.RedisConfig{OperationTimeout: time.Second}, nil)
|
||||
err := pingRedis(context.Background(), newTestRedisCfg("127.0.0.1:0"), nil)
|
||||
require.Error(t, err)
|
||||
require.Contains(t, err.Error(), "nil client")
|
||||
}
|
||||
@@ -62,10 +67,7 @@ func TestInstrumentRedisClientNilTelemetryIsNoop(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
server := miniredis.RunT(t)
|
||||
client := newRedisClient(config.RedisConfig{
|
||||
Addr: server.Addr(),
|
||||
OperationTimeout: time.Second,
|
||||
})
|
||||
client := newRedisClient(newTestRedisCfg(server.Addr()))
|
||||
t.Cleanup(func() { _ = client.Close() })
|
||||
|
||||
require.NoError(t, instrumentRedisClient(client, nil))
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"galaxy/lobby/internal/adapters/postgres/migrations"
|
||||
"galaxy/lobby/internal/adapters/redisstate"
|
||||
"galaxy/lobby/internal/api/internalhttp"
|
||||
"galaxy/lobby/internal/api/publichttp"
|
||||
@@ -14,6 +15,7 @@ import (
|
||||
"galaxy/lobby/internal/domain/game"
|
||||
"galaxy/lobby/internal/ports"
|
||||
"galaxy/lobby/internal/telemetry"
|
||||
"galaxy/postgres"
|
||||
)
|
||||
|
||||
// activeGamesProbe adapts ports.GameStore to telemetry.ActiveGamesProbe by
|
||||
@@ -110,7 +112,31 @@ func NewRuntime(ctx context.Context, cfg config.Config, logger *slog.Logger) (*R
|
||||
return cleanupOnError(fmt.Errorf("new lobby runtime: %w", err))
|
||||
}
|
||||
|
||||
wiring, err := newWiring(cfg, redisClient, time.Now, logger, telemetryRuntime)
|
||||
pgPool, err := postgres.OpenPrimary(ctx, cfg.Postgres.Conn,
|
||||
postgres.WithTracerProvider(telemetryRuntime.TracerProvider()),
|
||||
postgres.WithMeterProvider(telemetryRuntime.MeterProvider()),
|
||||
)
|
||||
if err != nil {
|
||||
return cleanupOnError(fmt.Errorf("new lobby runtime: open postgres: %w", err))
|
||||
}
|
||||
runtime.cleanupFns = append(runtime.cleanupFns, pgPool.Close)
|
||||
unregisterPGStats, err := postgres.InstrumentDBStats(pgPool,
|
||||
postgres.WithMeterProvider(telemetryRuntime.MeterProvider()),
|
||||
)
|
||||
if err != nil {
|
||||
return cleanupOnError(fmt.Errorf("new lobby runtime: instrument postgres: %w", err))
|
||||
}
|
||||
runtime.cleanupFns = append(runtime.cleanupFns, func() error {
|
||||
return unregisterPGStats()
|
||||
})
|
||||
if err := postgres.Ping(ctx, pgPool, cfg.Postgres.Conn.OperationTimeout); err != nil {
|
||||
return cleanupOnError(fmt.Errorf("new lobby runtime: ping postgres: %w", err))
|
||||
}
|
||||
if err := postgres.RunMigrations(ctx, pgPool, migrations.FS(), "."); err != nil {
|
||||
return cleanupOnError(fmt.Errorf("new lobby runtime: run postgres migrations: %w", err))
|
||||
}
|
||||
|
||||
wiring, err := newWiring(cfg, redisClient, pgPool, time.Now, logger, telemetryRuntime)
|
||||
if err != nil {
|
||||
return cleanupOnError(fmt.Errorf("new lobby runtime: wiring: %w", err))
|
||||
}
|
||||
|
||||
@@ -1,159 +0,0 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"galaxy/lobby/internal/api/internalhttp"
|
||||
"galaxy/lobby/internal/api/publichttp"
|
||||
"galaxy/lobby/internal/config"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
testcontainers "github.com/testcontainers/testcontainers-go"
|
||||
rediscontainer "github.com/testcontainers/testcontainers-go/modules/redis"
|
||||
)
|
||||
|
||||
const (
|
||||
realRuntimeSmokeEnv = "LOBBY_REAL_RUNTIME_SMOKE"
|
||||
realRuntimeRedisImage = "redis:7"
|
||||
)
|
||||
|
||||
// TestRealRuntimeCompatibility boots the full Runtime against a real Redis
|
||||
// container, verifies that both HTTP listeners serve /healthz and /readyz,
|
||||
// and asserts graceful shutdown on context cancellation. The test is skipped
|
||||
// unless LOBBY_REAL_RUNTIME_SMOKE=1 because it depends on Docker.
|
||||
func TestRealRuntimeCompatibility(t *testing.T) {
|
||||
if os.Getenv(realRuntimeSmokeEnv) != "1" {
|
||||
t.Skipf("set %s=1 to run the real runtime smoke suite", realRuntimeSmokeEnv)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
redisContainer, err := rediscontainer.Run(ctx, realRuntimeRedisImage)
|
||||
require.NoError(t, err)
|
||||
testcontainers.CleanupContainer(t, redisContainer)
|
||||
|
||||
redisAddr, err := redisContainer.Endpoint(ctx, "")
|
||||
require.NoError(t, err)
|
||||
|
||||
cfg := config.DefaultConfig()
|
||||
cfg.Redis.Addr = redisAddr
|
||||
cfg.UserService.BaseURL = "http://127.0.0.1:1"
|
||||
cfg.GM.BaseURL = "http://127.0.0.1:1"
|
||||
cfg.PublicHTTP.Addr = mustFreeAddr(t)
|
||||
cfg.InternalHTTP.Addr = mustFreeAddr(t)
|
||||
cfg.ShutdownTimeout = 2 * time.Second
|
||||
cfg.Telemetry.TracesExporter = "none"
|
||||
cfg.Telemetry.MetricsExporter = "none"
|
||||
|
||||
runtime, err := NewRuntime(context.Background(), cfg, testLogger())
|
||||
require.NoError(t, err)
|
||||
defer func() {
|
||||
require.NoError(t, runtime.Close())
|
||||
}()
|
||||
|
||||
runCtx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
runErrCh := make(chan error, 1)
|
||||
go func() {
|
||||
runErrCh <- runtime.Run(runCtx)
|
||||
}()
|
||||
|
||||
client := newTestHTTPClient(t)
|
||||
|
||||
waitForRuntimeReady(t, client, cfg.PublicHTTP.Addr, publichttp.ReadyzPath)
|
||||
waitForRuntimeReady(t, client, cfg.InternalHTTP.Addr, internalhttp.ReadyzPath)
|
||||
|
||||
assertHTTPStatus(t, client, "http://"+cfg.PublicHTTP.Addr+publichttp.HealthzPath, http.StatusOK)
|
||||
assertHTTPStatus(t, client, "http://"+cfg.PublicHTTP.Addr+publichttp.ReadyzPath, http.StatusOK)
|
||||
assertHTTPStatus(t, client, "http://"+cfg.InternalHTTP.Addr+internalhttp.HealthzPath, http.StatusOK)
|
||||
assertHTTPStatus(t, client, "http://"+cfg.InternalHTTP.Addr+internalhttp.ReadyzPath, http.StatusOK)
|
||||
|
||||
cancel()
|
||||
waitForRunResult(t, runErrCh, cfg.ShutdownTimeout+2*time.Second)
|
||||
}
|
||||
|
||||
func testLogger() *slog.Logger {
|
||||
return slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
}
|
||||
|
||||
func newTestHTTPClient(t *testing.T) *http.Client {
|
||||
t.Helper()
|
||||
|
||||
transport := &http.Transport{DisableKeepAlives: true}
|
||||
t.Cleanup(transport.CloseIdleConnections)
|
||||
|
||||
return &http.Client{
|
||||
Timeout: 500 * time.Millisecond,
|
||||
Transport: transport,
|
||||
}
|
||||
}
|
||||
|
||||
func waitForRuntimeReady(t *testing.T, client *http.Client, addr string, path string) {
|
||||
t.Helper()
|
||||
|
||||
require.Eventually(t, func() bool {
|
||||
request, err := http.NewRequest(http.MethodGet, "http://"+addr+path, nil)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
defer response.Body.Close()
|
||||
_, _ = io.Copy(io.Discard, response.Body)
|
||||
|
||||
return response.StatusCode == http.StatusOK
|
||||
}, 5*time.Second, 25*time.Millisecond, "lobby runtime did not become reachable on %s", addr)
|
||||
}
|
||||
|
||||
func waitForRunResult(t *testing.T, runErrCh <-chan error, waitTimeout time.Duration) {
|
||||
t.Helper()
|
||||
|
||||
var err error
|
||||
require.Eventually(t, func() bool {
|
||||
select {
|
||||
case err = <-runErrCh:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}, waitTimeout, 10*time.Millisecond, "lobby runtime did not stop")
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func assertHTTPStatus(t *testing.T, client *http.Client, target string, want int) {
|
||||
t.Helper()
|
||||
|
||||
request, err := http.NewRequest(http.MethodGet, target, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
response, err := client.Do(request)
|
||||
require.NoError(t, err)
|
||||
defer response.Body.Close()
|
||||
_, _ = io.Copy(io.Discard, response.Body)
|
||||
|
||||
require.Equal(t, want, response.StatusCode)
|
||||
}
|
||||
|
||||
func mustFreeAddr(t *testing.T) string {
|
||||
t.Helper()
|
||||
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
require.NoError(t, err)
|
||||
defer func() {
|
||||
assert.NoError(t, listener.Close())
|
||||
}()
|
||||
|
||||
return listener.Addr().String()
|
||||
}
|
||||
@@ -1,151 +0,0 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"galaxy/lobby/internal/api/internalhttp"
|
||||
"galaxy/lobby/internal/api/publichttp"
|
||||
"galaxy/lobby/internal/config"
|
||||
|
||||
"github.com/alicebob/miniredis/v2"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// newTestConfig builds a valid Config that listens on ephemeral ports and a
|
||||
// miniredis instance provided by redisServer.
|
||||
func newTestConfig(t *testing.T, redisAddr string) config.Config {
|
||||
t.Helper()
|
||||
|
||||
reserve := func() string {
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
require.NoError(t, err)
|
||||
addr := listener.Addr().String()
|
||||
require.NoError(t, listener.Close())
|
||||
return addr
|
||||
}
|
||||
|
||||
cfg := config.DefaultConfig()
|
||||
cfg.Redis.Addr = redisAddr
|
||||
cfg.UserService.BaseURL = "http://127.0.0.1:1"
|
||||
cfg.GM.BaseURL = "http://127.0.0.1:1"
|
||||
cfg.PublicHTTP.Addr = reserve()
|
||||
cfg.InternalHTTP.Addr = reserve()
|
||||
|
||||
return cfg
|
||||
}
|
||||
|
||||
func TestNewRuntimeValidatesContext(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, err := NewRuntime(nil, config.Config{}, nil) //nolint:staticcheck // test exercises the nil-context guard.
|
||||
require.Error(t, err)
|
||||
require.Contains(t, err.Error(), "nil context")
|
||||
}
|
||||
|
||||
func TestNewRuntimeRejectsInvalidConfig(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, err := NewRuntime(context.Background(), config.Config{}, nil)
|
||||
require.Error(t, err)
|
||||
require.Contains(t, err.Error(), "new lobby runtime")
|
||||
}
|
||||
|
||||
func TestNewRuntimeSucceedsWithMiniredis(t *testing.T) {
|
||||
redisServer := miniredis.RunT(t)
|
||||
|
||||
runtime, err := NewRuntime(context.Background(), newTestConfig(t, redisServer.Addr()), nil)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, runtime)
|
||||
t.Cleanup(func() { _ = runtime.Close() })
|
||||
|
||||
assert.NotNil(t, runtime.PublicServer())
|
||||
assert.NotNil(t, runtime.InternalServer())
|
||||
}
|
||||
|
||||
func TestNewRuntimeWiresRaceNameDirectory(t *testing.T) {
|
||||
redisServer := miniredis.RunT(t)
|
||||
|
||||
runtime, err := NewRuntime(context.Background(), newTestConfig(t, redisServer.Addr()), nil)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { _ = runtime.Close() })
|
||||
|
||||
require.NotNil(t, runtime.wiring)
|
||||
assert.NotNil(t, runtime.wiring.raceNameDirectory)
|
||||
}
|
||||
|
||||
func TestNewRuntimeFailsWhenRedisUnreachable(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
cfg := newTestConfig(t, "127.0.0.1:1") // guaranteed unreachable
|
||||
cfg.Redis.OperationTimeout = 100 * time.Millisecond
|
||||
|
||||
_, err := NewRuntime(context.Background(), cfg, nil)
|
||||
require.Error(t, err)
|
||||
require.Contains(t, err.Error(), "ping redis")
|
||||
}
|
||||
|
||||
func TestRuntimeCloseIsIdempotent(t *testing.T) {
|
||||
redisServer := miniredis.RunT(t)
|
||||
runtime, err := NewRuntime(context.Background(), newTestConfig(t, redisServer.Addr()), nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.NoError(t, runtime.Close())
|
||||
require.NoError(t, runtime.Close())
|
||||
}
|
||||
|
||||
func TestRuntimeRunServesProbesAndStopsOnCancel(t *testing.T) {
|
||||
redisServer := miniredis.RunT(t)
|
||||
cfg := newTestConfig(t, redisServer.Addr())
|
||||
|
||||
runtime, err := NewRuntime(context.Background(), cfg, nil)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { _ = runtime.Close() })
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
t.Cleanup(cancel)
|
||||
|
||||
runErr := make(chan error, 1)
|
||||
go func() {
|
||||
runErr <- runtime.Run(ctx)
|
||||
}()
|
||||
|
||||
require.Eventually(t, func() bool {
|
||||
return runtime.PublicServer().Addr() != "" && runtime.InternalServer().Addr() != ""
|
||||
}, 2*time.Second, 10*time.Millisecond)
|
||||
|
||||
for _, probe := range []struct {
|
||||
label string
|
||||
url string
|
||||
}{
|
||||
{"public healthz", "http://" + runtime.PublicServer().Addr() + publichttp.HealthzPath},
|
||||
{"public readyz", "http://" + runtime.PublicServer().Addr() + publichttp.ReadyzPath},
|
||||
{"internal healthz", "http://" + runtime.InternalServer().Addr() + internalhttp.HealthzPath},
|
||||
{"internal readyz", "http://" + runtime.InternalServer().Addr() + internalhttp.ReadyzPath},
|
||||
} {
|
||||
resp, err := http.Get(probe.url)
|
||||
require.NoError(t, err, probe.label)
|
||||
_ = resp.Body.Close()
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode, probe.label)
|
||||
}
|
||||
|
||||
cancel()
|
||||
|
||||
select {
|
||||
case err := <-runErr:
|
||||
require.NoError(t, err)
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("runtime did not stop after cancel")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuntimeRunNilContext(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var runtime *Runtime
|
||||
require.Error(t, runtime.Run(context.Background()))
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
@@ -10,6 +11,11 @@ import (
|
||||
"galaxy/lobby/internal/adapters/idgen"
|
||||
"galaxy/lobby/internal/adapters/metricsintentpub"
|
||||
"galaxy/lobby/internal/adapters/metricsracenamedir"
|
||||
pgapplicationstore "galaxy/lobby/internal/adapters/postgres/applicationstore"
|
||||
pggamestore "galaxy/lobby/internal/adapters/postgres/gamestore"
|
||||
pginvitestore "galaxy/lobby/internal/adapters/postgres/invitestore"
|
||||
pgmembershipstore "galaxy/lobby/internal/adapters/postgres/membershipstore"
|
||||
pgracenamedir "galaxy/lobby/internal/adapters/postgres/racenamedir"
|
||||
"galaxy/lobby/internal/adapters/racenameintents"
|
||||
"galaxy/lobby/internal/adapters/racenamestub"
|
||||
"galaxy/lobby/internal/adapters/redisstate"
|
||||
@@ -234,6 +240,7 @@ type wiring struct {
|
||||
func newWiring(
|
||||
cfg config.Config,
|
||||
redisClient *redis.Client,
|
||||
pgPool *sql.DB,
|
||||
clock func() time.Time,
|
||||
logger *slog.Logger,
|
||||
telemetryRuntime *telemetry.Runtime,
|
||||
@@ -249,29 +256,47 @@ func newWiring(
|
||||
logger = slog.Default()
|
||||
}
|
||||
|
||||
rawDirectory, err := buildRaceNameDirectory(cfg, redisClient, policy, clock)
|
||||
if redisClient == nil {
|
||||
return nil, errors.New("new lobby wiring: nil redis client")
|
||||
}
|
||||
if pgPool == nil {
|
||||
return nil, errors.New("new lobby wiring: nil postgres pool")
|
||||
}
|
||||
|
||||
rawDirectory, err := buildRaceNameDirectory(cfg, pgPool, policy, clock)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("new lobby wiring: %w", err)
|
||||
}
|
||||
directory := metricsracenamedir.New(rawDirectory, telemetryRuntime)
|
||||
|
||||
if redisClient == nil {
|
||||
return nil, errors.New("new lobby wiring: nil redis client")
|
||||
pgStoreCfg := struct {
|
||||
DB *sql.DB
|
||||
OperationTimeout time.Duration
|
||||
}{
|
||||
DB: pgPool,
|
||||
OperationTimeout: cfg.Postgres.Conn.OperationTimeout,
|
||||
}
|
||||
|
||||
gameStore, err := redisstate.NewGameStore(redisClient)
|
||||
gameStore, err := pggamestore.New(pggamestore.Config{
|
||||
DB: pgStoreCfg.DB, OperationTimeout: pgStoreCfg.OperationTimeout,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("new lobby wiring: %w", err)
|
||||
}
|
||||
applicationStore, err := redisstate.NewApplicationStore(redisClient)
|
||||
applicationStore, err := pgapplicationstore.New(pgapplicationstore.Config{
|
||||
DB: pgStoreCfg.DB, OperationTimeout: pgStoreCfg.OperationTimeout,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("new lobby wiring: %w", err)
|
||||
}
|
||||
inviteStore, err := redisstate.NewInviteStore(redisClient)
|
||||
inviteStore, err := pginvitestore.New(pginvitestore.Config{
|
||||
DB: pgStoreCfg.DB, OperationTimeout: pgStoreCfg.OperationTimeout,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("new lobby wiring: %w", err)
|
||||
}
|
||||
membershipStore, err := redisstate.NewMembershipStore(redisClient)
|
||||
membershipStore, err := pgmembershipstore.New(pgmembershipstore.Config{
|
||||
DB: pgStoreCfg.DB, OperationTimeout: pgStoreCfg.OperationTimeout,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("new lobby wiring: %w", err)
|
||||
}
|
||||
@@ -763,20 +788,21 @@ func newWiring(
|
||||
// selected by cfg.RaceNameDirectory.Backend.
|
||||
func buildRaceNameDirectory(
|
||||
cfg config.Config,
|
||||
redisClient *redis.Client,
|
||||
pgPool *sql.DB,
|
||||
policy *racename.Policy,
|
||||
clock func() time.Time,
|
||||
) (ports.RaceNameDirectory, error) {
|
||||
switch cfg.RaceNameDirectory.Backend {
|
||||
case config.RaceNameDirectoryBackendRedis:
|
||||
if redisClient == nil {
|
||||
return nil, errors.New("redis race name directory backend requires a Redis client")
|
||||
case config.RaceNameDirectoryBackendPostgres:
|
||||
if pgPool == nil {
|
||||
return nil, errors.New("postgres race name directory backend requires a Postgres pool")
|
||||
}
|
||||
return redisstate.NewRaceNameDirectory(
|
||||
redisClient,
|
||||
policy,
|
||||
redisstate.WithRaceNameDirectoryClock(clock),
|
||||
)
|
||||
return pgracenamedir.New(pgracenamedir.Config{
|
||||
DB: pgPool,
|
||||
OperationTimeout: cfg.Postgres.Conn.OperationTimeout,
|
||||
Policy: policy,
|
||||
Clock: clock,
|
||||
})
|
||||
case config.RaceNameDirectoryBackendStub:
|
||||
return racenamestub.NewDirectory(racenamestub.WithClock(clock))
|
||||
default:
|
||||
|
||||
Reference in New Issue
Block a user