Skip to content

Commit

Permalink
better sentinels support
Browse files Browse the repository at this point in the history
  • Loading branch information
silentroach committed Feb 26, 2024
1 parent 136707c commit 31f1920
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 33 deletions.
28 changes: 17 additions & 11 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package config

type Config struct {
YandexAPIKey string `env:"YANDEX_APIKEY,default=''"`
YandexEnabled bool `env:"YANDEX_ENABLED,default=false"`
DadataEnabled bool `env:"DADATA_ENABLED,default=false"`
RedisSentinel bool `env:"REDIS_SENTINEL,default=false"`
RedisSentinelName string `env:"REDIS_SENTINEL_NAME,default='mymaster'"`
YandexAPIKey string `env:"YANDEX_APIKEY,default=''"`
YandexEnabled bool `env:"YANDEX_ENABLED,default=false"`
DadataEnabled bool `env:"DADATA_ENABLED,default=false"`
RedisSentinel bool `env:"REDIS_SENTINEL,default=false"`

// Deprecated: Use RedisSentinelList instead
RedisSentinelHost string `env:"REDIS_SENTINEL_HOST,default='127.0.0.1'"`
// Deprecated: Use RedisSentinelList instead
RedisSentinelPort string `env:"REDIS_SENTINEL_PORT,default=26379"`
RedisHost string `env:"REDIS_HOST,default=127.0.0.1"`
RedisPort string `env:"REDIS_PORT,default=6379"`
RedisAuth string `env:"REDIS_AUTH,default=''"`
RedisDB int `env:"REDIS_DB,default=0"`
AppPort string `env:"APP_PORT,default=8080"`
AppMode string `env:"APP_MODE,default=prod"`

RedisSentinelName string `env:"REDIS_SENTINEL_NAME,default='mymaster'"`
RedisSentinelList string `env:"REDIS_SENTINEL_LIST"`

RedisHost string `env:"REDIS_HOST,default=127.0.0.1"`
RedisPort string `env:"REDIS_PORT,default=6379"`
RedisAuth string `env:"REDIS_AUTH,default=''"`
RedisDB int `env:"REDIS_DB,default=0"`
AppPort string `env:"APP_PORT,default=8080"`
AppMode string `env:"APP_MODE,default=prod"`
}
41 changes: 33 additions & 8 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -1,44 +1,69 @@
version: '3.8'
version: "3.8"
services:
app:
image: address-suggestion-proxy:local
restart: unless-stopped
ports:
- '8080:8080'
- "8080:8080"
working_dir: /app
entrypoint: ["go", "run", "."]
volumes:
- .:/app
depends_on:
- redis
- redis-sentinel
networks:
- backend
redis:
image: 'bitnami/redis:latest'
image: "bitnami/redis:latest"
restart: always
environment:
- ALLOW_EMPTY_PASSWORD=no
- REDIS_PASSWORD=password
- REDIS_MASTER_PASSWORD=password
- REDIS_REPLICATION_MODE=master
ports:
- '6379:6379'
- "6379:6379"
volumes:
- suggestion-redis:/bitnami/redis/data
networks:
- backend
redis-slave:
image: "bitnami/redis:latest"
restart: always
environment:
- ALLOW_EMPTY_PASSWORD=no
- REDIS_PASSWORD=password
- REDIS_MASTER_HOST=redis
- REDIS_MASTER_PORT_NUMBER=6379
- REDIS_MASTER_PASSWORD=password
- REDIS_REPLICATION_MODE=slave
ports:
- "6380:6380"
volumes:
- suggestion-redis-replica:/bitnami/redis/data
networks:
- backend
depends_on:
- redis
redis-sentinel:
image: 'bitnami/redis-sentinel:latest'
image: "bitnami/redis-sentinel:latest"
environment:
- REDIS_MASTER_HOST=redis
- REDIS_MASTER_PASSWORD=password
- REDIS_SENTINEL_DOWN_AFTER_MILLISECONDS=1000
- REDIS_SENTINEL_QUORUM=1
ports:
- '26379:26379'
- "26379:26379"
depends_on:
- redis
- redis-slave
networks:
- backend
volumes:
suggestion-redis:
driver: local
suggestion-redis-replica:
driver: local
networks:
backend:
driver: bridge
driver: bridge
31 changes: 17 additions & 14 deletions internal/services/redis_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,34 @@ package services

import (
"address-suggesstion-proxy/config"
"context"
"fmt"
"strings"

"github.com/redis/go-redis/v9"
log "github.com/sirupsen/logrus"
)

func getSentinelsList(cfg *config.Config) []string {
if cfg.RedisSentinelList != "" {
return strings.Split(cfg.RedisSentinelList, ",")
}

log.Warnln("Using deprecated sentinels config, use REDIS_SENTINEL_LIST instead")
return []string{fmt.Sprintf("%s:%s", cfg.RedisSentinelHost, cfg.RedisSentinelPort)}
}

func NewRedisClient(cfg *config.Config) *redis.Client {
addr := fmt.Sprintf("%s:%s", cfg.RedisHost, cfg.RedisPort)

if cfg.RedisSentinel {
log.Debugln("using redis-sentinel")
log.Debugln("Using redis-sentinel")

sentinel := redis.NewSentinelClient(&redis.Options{
Addr: fmt.Sprintf("%s:%s", cfg.RedisSentinelHost, cfg.RedisSentinelPort),
return redis.NewFailoverClient(&redis.FailoverOptions{
SentinelAddrs: getSentinelsList(cfg),
MasterName: cfg.RedisSentinelName,
Password: cfg.RedisAuth,
DB: cfg.RedisDB,
})

masters, err := sentinel.GetMasterAddrByName(context.Background(), cfg.RedisSentinelName).Result()

if err != nil {
log.WithError(err).Fatal("failed to get redis sentinel master host")
}

addr = fmt.Sprintf("%s:%s", masters[0], masters[1])

log.Debugf(fmt.Sprintf("redis master from sentinel is: %s", addr))
}

return redis.NewClient(&redis.Options{
Expand Down

0 comments on commit 31f1920

Please sign in to comment.