From 0b366d53435ce6c7f9a913827de425ded38559f6 Mon Sep 17 00:00:00 2001 From: Sergey Tsalkov Date: Tue, 21 Sep 2021 00:07:31 +0000 Subject: [PATCH] redis.socket option lets you connect to redis through unix socket --- CONFIG.md | 4 ++++ config/config.go | 1 + redis/redis.go | 22 ++++++++++++++-------- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/CONFIG.md b/CONFIG.md index 4f0e5d5..47ac5d9 100644 --- a/CONFIG.md +++ b/CONFIG.md @@ -25,6 +25,7 @@ The first time brooce runs, it will create a `~/.brooce` dir in your home direct }, "redis": { "host": "localhost:6379", + "socket": "", "password": "", "db": 0 }, @@ -75,6 +76,9 @@ By default, job stdout/stderr is only logged to redis for review through the web ### `redis.host` / `redis.password` The hostname and password to access your redis server. Defaults to localhost and no-password. +### `redis.socket` +If specified, connect through a unix sock file instead of a hostname. Example: `/var/run/redis/redis-server.sock` + ### `redis.db` The db which will be used by brooce on your redis server. Defaults to 0. diff --git a/config/config.go b/config/config.go index 3f333a5..c83555a 100644 --- a/config/config.go +++ b/config/config.go @@ -41,6 +41,7 @@ type ConfigType struct { Redis struct { Host string `json:"host"` + Socket string `json:"socket"` Password string `json:"password"` DB int `json:"db"` } `json:"redis"` diff --git a/redis/redis.go b/redis/redis.go index ab49f49..443b9ba 100644 --- a/redis/redis.go +++ b/redis/redis.go @@ -19,8 +19,16 @@ func Get() *redis.Client { once.Do(func() { threads := len(config.Threads) + 10 + network := "tcp" + addr := config.Config.Redis.Host + if config.Config.Redis.Socket != "" { + network = "unix" + addr = config.Config.Redis.Socket + } + redisClient = redis.NewClient(&redis.Options{ - Addr: config.Config.Redis.Host, + Network: network, + Addr: addr, Password: config.Config.Redis.Password, MaxRetries: 10, PoolSize: threads, @@ -31,14 +39,12 @@ func Get() *redis.Client { DB: config.Config.Redis.DB, }) - for { - err := redisClient.Ping().Err() - if err == nil { - break - } - log.Println("Can't reach redis at", config.Config.Redis.Host, "-- are your redis addr and password right?", err) - time.Sleep(5 * time.Second) + err := redisClient.Ping().Err() + if err != nil { + log.Fatalln("Can't reach redis at", addr, "-- are your redis addr and password right?", err) } + + log.Println("Connected to redis at", addr) }) return redisClient