|
1 | 1 | package main
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "crypto/tls" |
5 | 4 | "fmt"
|
| 5 | + "strconv" |
| 6 | + "time" |
| 7 | + |
| 8 | + "crypto/tls" |
6 | 9 | log "github.com/sirupsen/logrus"
|
7 | 10 | "gopkg.in/ldap.v2"
|
8 |
| - "strconv" |
9 | 11 | )
|
10 | 12 |
|
11 | 13 | type LDAPConnection struct {
|
@@ -33,15 +35,40 @@ func (c *LDAPConnection) Init(
|
33 | 35 | func (c *LDAPConnection) reconnectToLDAP() {
|
34 | 36 | lcon, err := ldap.DialTLS("tcp", c.host,
|
35 | 37 | &tls.Config{ServerName: "ldap.csh.rit.edu"})
|
36 |
| - if err != nil { |
37 |
| - c.app.db.AddLog(0, "ldap connection error: "+err.Error()) |
38 |
| - log.Fatal(err) |
| 38 | + |
| 39 | + maxReconnectionAttempts := 3 // TODO (wilnil): Use an environment variable? |
| 40 | + attempts := 0 |
| 41 | + |
| 42 | + for err != nil { |
| 43 | + if attempts < maxReconnectionAttempts { |
| 44 | + c.app.db.AddLog(0, "ldap connection error: "+err.Error()) |
| 45 | + log.Info(err) |
| 46 | + time.Sleep(2 * time.Second) |
| 47 | + lcon, err = ldap.DialTLS("tcp", c.host, |
| 48 | + &tls.Config{ServerName: "ldap.csh.rit.edu"}) |
| 49 | + } else { |
| 50 | + c.app.db.AddLog(0, "ldap connection error: "+err.Error()) |
| 51 | + log.Fatal(err) |
| 52 | + } |
| 53 | + maxReconnectionAttempts++ |
39 | 54 | }
|
| 55 | + |
40 | 56 | err = lcon.Bind(c.bind_dn, c.bind_pw)
|
41 |
| - if err != nil { |
42 |
| - c.app.db.AddLog(0, "ldap bind error: "+err.Error()) |
43 |
| - log.Fatal(err) |
| 57 | + |
| 58 | + attempts = 0 |
| 59 | + for err != nil { |
| 60 | + if attempts < maxReconnectionAttempts { |
| 61 | + c.app.db.AddLog(0, "ldap bind error: "+err.Error()) |
| 62 | + log.Info(err) |
| 63 | + time.Sleep(2 * time.Second) |
| 64 | + err = lcon.Bind(c.bind_dn, c.bind_pw) |
| 65 | + } else { |
| 66 | + c.app.db.AddLog(0, "ldap bind error: "+err.Error()) |
| 67 | + log.Fatal(err) |
| 68 | + } |
| 69 | + maxReconnectionAttempts++ |
44 | 70 | }
|
| 71 | + |
45 | 72 | c.con = lcon
|
46 | 73 | }
|
47 | 74 |
|
|
0 commit comments