forked from jrallison/go-workers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
61 lines (54 loc) · 1.22 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
package workers
import (
"github.com/garyburd/redigo/redis"
"strconv"
"time"
)
type config struct {
processId string
namespace string
Pool *redis.Pool
}
var Config *config
func Configure(options map[string]string) {
var poolSize int
var namespace string
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"] + ":"
}
poolSize, _ = strconv.Atoi(options["pool"])
Config = &config{
options["process"],
namespace,
&redis.Pool{
MaxIdle: poolSize,
IdleTimeout: 240 * time.Second,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", options["server"])
if err != nil {
return nil, err
}
if options["password"] != "" {
if _, err := c.Do("AUTH", options["password"]); err != nil {
c.Close()
return nil, err
}
}
return c, err
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
},
}
}