-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpool.go
48 lines (43 loc) · 1.02 KB
/
pool.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
package ldap
import "log"
// Pool handles a concurrency-safe pool of connections to LDAP servers.
type Pool struct {
retries int
// connCh holds open connections to servers.
connCh chan *conn
}
// NewPool constructs a connection pool that queries for baseDn from servers.
func NewPool(servers []string, baseDn string, retries int) *Pool {
p := &Pool{
retries: retries,
connCh: make(chan *conn, len(servers)),
}
for _, s := range servers {
c := &conn{
server: s,
baseDn: baseDn,
}
go p.reconnect(c)
}
return p
}
func (p *Pool) reconnect(c *conn) {
c.reconnect()
p.connCh <- c
}
// ResolvePool attempts to resolve the pool for hostname to an IP address, returned as a string.
func (p *Pool) ResolvePool(hostname string) (string, error) {
var ip string
var err error
for i := 0; i < p.retries; i++ {
c := <-p.connCh
ip, err = c.resolvePool(hostname)
if err == nil {
p.connCh <- c
return ip, err
}
log.Printf("resolving %q on %s: %v", hostname, c.server, err)
go p.reconnect(c)
}
return ip, err
}