forked from jrallison/go-workers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
80 lines (72 loc) · 1.7 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
package workers
import (
"strconv"
"time"
"github.com/garyburd/redigo/redis"
)
type config struct {
processId string
Namespace string
PollInterval int
Pool *redis.Pool
Fetch func(queue string) Fetcher
}
var Config *config
func Configure(options map[string]string) {
var poolSize int
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
}
poolSize, _ = strconv.Atoi(options["pool"])
Config = &config{
options["process"],
namespace,
pollInterval,
&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
}
}
if options["database"] != "" {
if _, err := c.Do("SELECT", options["database"]); 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
},
},
func(queue string) Fetcher {
return NewFetch(queue, make(chan *Msg), make(chan bool))
},
}
}