57 lines
1.6 KiB
Go
57 lines
1.6 KiB
Go
// Package redisadapter provides the Redis client helpers used by Auth/Session
|
|
// Service runtime wiring. The helpers wrap `pkg/redisconn` so the runtime
|
|
// keeps the same construction surface as the other Galaxy services.
|
|
package redisadapter
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"galaxy/authsession/internal/config"
|
|
"galaxy/authsession/internal/telemetry"
|
|
"galaxy/redisconn"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
// NewClient constructs one Redis client from cfg using the shared
|
|
// `pkg/redisconn` helper, which enforces the master/replica/password env-var
|
|
// shape.
|
|
func NewClient(cfg config.RedisConfig) *redis.Client {
|
|
return redisconn.NewMasterClient(cfg.Conn)
|
|
}
|
|
|
|
// InstrumentClient attaches Redis tracing and metrics exporters to client
|
|
// when telemetryRuntime is available.
|
|
func InstrumentClient(client *redis.Client, telemetryRuntime *telemetry.Runtime) error {
|
|
if client == nil {
|
|
return fmt.Errorf("instrument redis client: nil client")
|
|
}
|
|
if telemetryRuntime == nil {
|
|
return nil
|
|
}
|
|
|
|
return redisconn.Instrument(
|
|
client,
|
|
redisconn.WithTracerProvider(telemetryRuntime.TracerProvider()),
|
|
redisconn.WithMeterProvider(telemetryRuntime.MeterProvider()),
|
|
)
|
|
}
|
|
|
|
// Ping performs the startup Redis connectivity check bounded by
|
|
// cfg.Conn.OperationTimeout.
|
|
func Ping(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.Conn.OperationTimeout)
|
|
defer cancel()
|
|
|
|
if err := client.Ping(pingCtx).Err(); err != nil {
|
|
return fmt.Errorf("ping redis: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|