-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconn.go
61 lines (55 loc) · 1.34 KB
/
conn.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 ldap
import (
"fmt"
"log"
"sync"
"time"
ldap "gopkg.in/ldap.v3"
)
type conn struct {
server, baseDn string
// mu protects conn during reconnect cycles
// TODO: The ldap package supports multiple in-flight queries;
// by using a Mutex we are only going to issue one at a
// time. We should figure out how to do retry/reconnect
// behavior with parallel queries.
mu sync.Mutex
conn *ldap.Conn
}
func (c *conn) reconnect() {
c.mu.Lock()
defer c.mu.Unlock()
if c.conn != nil {
c.conn.Close()
}
var err error
for {
log.Printf("connecting to %s", c.server)
c.conn, err = ldap.Dial("tcp", c.server)
if err == nil {
return
}
log.Printf("connecting to %s: %v", c.server, err)
time.Sleep(100 * time.Millisecond)
}
}
func (c *conn) resolvePool(hostname string) (string, error) {
c.mu.Lock()
defer c.mu.Unlock()
escapedHostname := ldap.EscapeFilter(hostname)
req := &ldap.SearchRequest{
BaseDN: c.baseDn,
Scope: ldap.ScopeWholeSubtree,
Filter: fmt.Sprintf("(|(scriptsVhostName=%s)(scriptsVhostAlias=%s))", escapedHostname, escapedHostname),
Attributes: []string{"scriptsVhostPoolIPv4"},
}
sr, err := c.conn.Search(req)
if err != nil {
return "", err
}
for _, entry := range sr.Entries {
return entry.GetAttributeValue("scriptsVhostPoolIPv4"), nil
}
// Not found is not an error
return "", nil
}