Skip to content

Commit

Permalink
redis.socket option lets you connect to redis through unix socket
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyTsalkov committed Sep 21, 2021
1 parent 617d404 commit 0b366d5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CONFIG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down Expand Up @@ -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.

Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
22 changes: 14 additions & 8 deletions redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit 0b366d5

Please sign in to comment.