163 lines
4.8 KiB
Go
163 lines
4.8 KiB
Go
// Package lifecycleevents implements the Redis Streams-backed publisher for
|
|
// trusted user-lifecycle events consumed by `Game Lobby`.
|
|
package lifecycleevents
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"galaxy/user/internal/ports"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
"go.opentelemetry.io/otel/trace"
|
|
)
|
|
|
|
// Config configures one Redis-backed user-lifecycle publisher. The
|
|
// connection is supplied externally by the runtime so multiple publishers
|
|
// can share one *redis.Client.
|
|
type Config struct {
|
|
// Stream identifies the Redis Stream key used for lifecycle events. The
|
|
// default platform key is `user:lifecycle_events`.
|
|
Stream string
|
|
|
|
// StreamMaxLen bounds the stream with approximate trimming via
|
|
// `XADD MAXLEN ~`.
|
|
StreamMaxLen int64
|
|
|
|
// OperationTimeout bounds each Redis round trip performed by the adapter.
|
|
OperationTimeout time.Duration
|
|
}
|
|
|
|
// Publisher publishes trusted user-lifecycle events into the dedicated Redis
|
|
// Stream consumed by `Game Lobby` for Race Name Directory cascade release.
|
|
type Publisher struct {
|
|
client *redis.Client
|
|
stream string
|
|
streamMaxLen int64
|
|
operationTimeout time.Duration
|
|
}
|
|
|
|
// New constructs a Redis-backed lifecycle-event publisher backed by the
|
|
// supplied client. The publisher does not own the client; the runtime is
|
|
// responsible for closing it.
|
|
func New(client *redis.Client, cfg Config) (*Publisher, error) {
|
|
switch {
|
|
case client == nil:
|
|
return nil, errors.New("new redis lifecycle-event publisher: redis client must not be nil")
|
|
case strings.TrimSpace(cfg.Stream) == "":
|
|
return nil, errors.New("new redis lifecycle-event publisher: stream must not be empty")
|
|
case cfg.StreamMaxLen <= 0:
|
|
return nil, errors.New("new redis lifecycle-event publisher: stream max len must be positive")
|
|
case cfg.OperationTimeout <= 0:
|
|
return nil, errors.New("new redis lifecycle-event publisher: operation timeout must be positive")
|
|
}
|
|
|
|
return &Publisher{
|
|
client: client,
|
|
stream: cfg.Stream,
|
|
streamMaxLen: cfg.StreamMaxLen,
|
|
operationTimeout: cfg.OperationTimeout,
|
|
}, nil
|
|
}
|
|
|
|
// Close is a no-op: the client is owned by the runtime.
|
|
func (publisher *Publisher) Close() error {
|
|
return nil
|
|
}
|
|
|
|
// Ping verifies that the configured Redis backend is reachable within the
|
|
// adapter operation timeout budget.
|
|
func (publisher *Publisher) Ping(ctx context.Context) error {
|
|
operationCtx, cancel, err := publisher.operationContext(ctx, "ping redis lifecycle-event publisher")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer cancel()
|
|
|
|
if err := publisher.client.Ping(operationCtx).Err(); err != nil {
|
|
return fmt.Errorf("ping redis lifecycle-event publisher: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// PublishUserLifecycleEvent publishes one committed lifecycle event to the
|
|
// configured Redis Stream.
|
|
func (publisher *Publisher) PublishUserLifecycleEvent(ctx context.Context, event ports.UserLifecycleEvent) error {
|
|
if err := event.Validate(); err != nil {
|
|
return fmt.Errorf("publish user lifecycle event: %w", err)
|
|
}
|
|
|
|
traceID := traceIDFromContext(ctx, event.TraceID)
|
|
|
|
values := map[string]any{
|
|
"event_type": string(event.EventType),
|
|
"user_id": event.UserID.String(),
|
|
"occurred_at_ms": strconv.FormatInt(event.OccurredAt.UTC().UnixMilli(), 10),
|
|
"source": event.Source.String(),
|
|
"actor_type": event.Actor.Type.String(),
|
|
"reason_code": event.ReasonCode.String(),
|
|
}
|
|
if !event.Actor.ID.IsZero() {
|
|
values["actor_id"] = event.Actor.ID.String()
|
|
}
|
|
if traceID != "" {
|
|
values["trace_id"] = traceID
|
|
}
|
|
|
|
operationCtx, cancel, err := publisher.operationContext(ctx, "publish user lifecycle event")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer cancel()
|
|
|
|
if err := publisher.client.XAdd(operationCtx, &redis.XAddArgs{
|
|
Stream: publisher.stream,
|
|
MaxLen: publisher.streamMaxLen,
|
|
Approx: true,
|
|
Values: values,
|
|
}).Err(); err != nil {
|
|
return fmt.Errorf("publish user lifecycle event: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (publisher *Publisher) operationContext(ctx context.Context, operation string) (context.Context, context.CancelFunc, error) {
|
|
if publisher == nil || publisher.client == nil {
|
|
return nil, nil, fmt.Errorf("%s: nil publisher", operation)
|
|
}
|
|
if ctx == nil {
|
|
return nil, nil, fmt.Errorf("%s: nil context", operation)
|
|
}
|
|
|
|
operationCtx, cancel := context.WithTimeout(ctx, publisher.operationTimeout)
|
|
return operationCtx, cancel, nil
|
|
}
|
|
|
|
func traceIDFromContext(ctx context.Context, fallback string) string {
|
|
if strings.TrimSpace(fallback) != "" {
|
|
return fallback
|
|
}
|
|
if ctx == nil {
|
|
return ""
|
|
}
|
|
|
|
spanContext := trace.SpanContextFromContext(ctx)
|
|
if !spanContext.IsValid() {
|
|
return ""
|
|
}
|
|
|
|
return spanContext.TraceID().String()
|
|
}
|
|
|
|
var (
|
|
_ interface{ Close() error } = (*Publisher)(nil)
|
|
_ interface{ Ping(context.Context) error } = (*Publisher)(nil)
|
|
_ ports.UserLifecyclePublisher = (*Publisher)(nil)
|
|
)
|