-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpool.go
More file actions
73 lines (59 loc) · 1.95 KB
/
pool.go
File metadata and controls
73 lines (59 loc) · 1.95 KB
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
package disco
import(
"os"
"strings"
"time"
"github.com/garyburd/redigo/redis"
)
// A Disque connection pool.
type Pool struct {
Connections redis.Pool
Cycle int
Nodes []string
}
// Creates a new Pool with connections to the disque nodes specified in
// the `DISQUE_NODES` environment variable.
func NewPool(maxIdle, maxActive, cycle int, idleTimeout time.Duration) (Pool, error) {
return NewPoolToURLS(maxIdle, maxActive, cycle, idleTimeout, os.Getenv("DISQUE_NODES"))
}
// Creates a new Pool with connections to a list of comma-separated disque node URLs.
func NewPoolToURLS(maxIdle, maxActive, cycle int, idleTimeout time.Duration, urls string) (Pool, error) {
return NewPoolToNodes(maxIdle, maxActive, cycle, idleTimeout, strings.Split(urls, ",")...)
}
// Creates a new Pool with connections to an array of Disque nodes.
func NewPoolToNodes(maxIdle, maxActive, cycle int, idleTimeout time.Duration, nodes ...string) (Pool, error) {
disquePool := redis.Pool{
MaxIdle: maxIdle,
MaxActive: maxActive,
IdleTimeout: idleTimeout,
Dial: func () (redis.Conn, error) {
return connectToFirstAvailableNode(nodes...)
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
}
c := disquePool.Get()
defer c.Close()
_, err := c.Do("PING")
p := Pool{
Connections: disquePool,
Cycle: cycle,
Nodes: nodes,
}
return p, err
}
// Returns a disco.Connection from the Pool.
func (p *Pool) Get() Connection {
c := p.Connections.Get()
return Connection{c, p.Cycle, p.Nodes}
}
// Creates a funnel using this Pool.
func (p *Pool) NewFunnel(queues ...string) Funnel {
return NewFunnel(p, 1, time.Second * 100, queues...)
}
// Creates a funnel using this Pool, allowing for custom configuration options.
func (p *Pool) NewFunnelWithOptions(fetchCount int, fetchTimeout time.Duration, queues ...string) Funnel {
return NewFunnel(p, fetchCount, fetchTimeout, queues...)
}