Files
gochat/backend/internal/app/redis.go
T
Rogee 92f0d51375 refactor: 统一 DB/Redis 配置为 DSN 模式 + 移除 Helm/K8s 部署
- DatabaseConfig: Host/Port/User/Password/Name/DBName/SSLMode → 单个 DSN 字段
- RedisConfig: Host/Port/Password/DB/URL → 单个 DSN 字段
- 环境变量: GOCHAT_DATABASE_* (7个) → GOCHAT_DATABASE_DSN, GOCHAT_REDIS_* (5个) → GOCHAT_REDIS_DSN
- validator.go: DSN URL 解析校验 (scheme + host)
- redis.go: redis.ParseURL(cfg.DSN) 直连
- 所有 docker-compose / CI / shell 脚本 / .env 同步更新
- 删除 deploy/helm/ 整个目录 (20个文件)
- CI 删除 helm-validate / deploy-staging / deploy-production 三个 job
- 文档同步更新 (README, 架构设计, PRD, 滚动升级)
2026-07-29 20:58:10 +08:00

41 lines
972 B
Go

package app
import (
"context"
"fmt"
"time"
"github.com/gochat/gochat/internal/config"
"github.com/gochat/gochat/pkg/logger"
"github.com/redis/go-redis/v9"
)
// NewRedisClient creates a Redis client for caching, Pub/Sub, and sessions.
// Reference: Chatwoot config/cable.yml Redis adapter configuration
func NewRedisClient(cfg *config.RedisConfig) (*redis.Client, error) {
opts, err := redis.ParseURL(cfg.DSN)
if err != nil {
return nil, fmt.Errorf("failed to parse Redis DSN: %w", err)
}
poolSize := cfg.PoolSize
if poolSize <= 0 {
poolSize = 50
}
opts.PoolSize = poolSize
client := redis.NewClient(opts)
// Verify connection
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := client.Ping(ctx).Err(); err != nil {
return nil, fmt.Errorf("failed to connect to Redis: %w", err)
}
logger.L().Infof("Connected to Redis: %s (db=%d, pool=%d)", opts.Addr, opts.DB, poolSize)
return client, nil
}