forked from jrallison/go-workers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
92 lines (73 loc) · 1.97 KB
/
config.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package workers
import (
"crypto/tls"
"strconv"
"time"
"github.com/go-redis/redis"
)
type config struct {
processId string
Namespace string
PollInterval int
Pool *redis.Client
Fetch func(queue string) Fetcher
}
var Config *config
func Configure(options map[string]string) {
var namespace string
var pollInterval int
if options["server"] == "" {
panic("Configure requires a 'server' option, which identifies a Redis instance")
}
if options["process"] == "" {
panic("Configure requires a 'process' option, which uniquely identifies this instance")
}
if options["pool"] == "" {
options["pool"] = "1"
}
if options["namespace"] != "" {
namespace = options["namespace"] + ":"
}
if seconds, err := strconv.Atoi(options["poll_interval"]); err == nil {
pollInterval = seconds
} else {
pollInterval = 15
}
Config = &config{
processId: options["process"],
Namespace: namespace,
PollInterval: pollInterval,
Pool: createRedisPool(options),
Fetch: func(queue string) Fetcher {
return NewFetch(queue, make(chan *Msg), make(chan bool))
},
}
if _, err := Config.Pool.Ping().Result(); err != nil {
Logger.Printf("Couldn't connect to redis: %s", err.Error())
}
}
func createRedisPool(options map[string]string) *redis.Client {
poolSize, _ := strconv.Atoi(options["pool"])
dbID, _ := strconv.Atoi(options["database"])
redisClientOpts := &redis.Options{
Addr: options["server"],
DB: dbID,
ReadTimeout: 15 * time.Second,
WriteTimeout: 30 * time.Second,
MaxRetries: 3,
PoolSize: poolSize,
MinIdleConns: 3,
MaxConnAge: 60 * time.Second,
IdleTimeout: 240 * time.Second,
IdleCheckFrequency: 500 * time.Millisecond,
}
if "" != options["password"] {
redisClientOpts.Password = options["password"]
}
if useTLS, ok := options["useTLS"]; ok {
if "yes" == useTLS {
redisClientOpts.TLSConfig = &tls.Config{}
}
}
return redis.NewClient(redisClientOpts)
}