-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathredis.go
43 lines (36 loc) · 943 Bytes
/
redis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package kettle
import (
"os"
"strconv"
"time"
"github.com/gomodule/redigo/redis"
"github.com/pkg/errors"
)
func NewRedisPool() (*redis.Pool, error) {
addr := os.Getenv("REDIS_HOST")
if addr == "" {
return nil, errors.Errorf("REDIS_HOST env variable must be set (e.g host:port, redis://password@host:port)")
}
var dialOpts []redis.DialOption
password := os.Getenv("REDIS_PASSWORD")
if password != "" {
dialOpts = append(dialOpts, redis.DialPassword(password))
}
tm := os.Getenv("REDIS_TIMEOUT_SECONDS")
if tm != "" {
tmsec, err := strconv.Atoi(tm)
if err != nil {
return nil, err
} else {
dialOpts = append(dialOpts, redis.DialConnectTimeout(time.Duration(tmsec)*time.Second))
}
}
rp := &redis.Pool{
MaxIdle: 3,
MaxActive: 4,
Wait: true,
IdleTimeout: 240 * time.Second,
Dial: func() (redis.Conn, error) { return redis.Dial("tcp", addr, dialOpts...) },
}
return rp, nil
}