Skip to content

Commit dfe503d

Browse files
committed
Add retry to ldap reconnect code
1 parent b04af75 commit dfe503d

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

ldap.go

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package main
22

33
import (
4-
"crypto/tls"
54
"fmt"
5+
"strconv"
6+
"time"
7+
8+
"crypto/tls"
69
log "github.com/sirupsen/logrus"
710
"gopkg.in/ldap.v2"
8-
"strconv"
911
)
1012

1113
type LDAPConnection struct {
@@ -33,15 +35,40 @@ func (c *LDAPConnection) Init(
3335
func (c *LDAPConnection) reconnectToLDAP() {
3436
lcon, err := ldap.DialTLS("tcp", c.host,
3537
&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++
3954
}
55+
4056
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++
4470
}
71+
4572
c.con = lcon
4673
}
4774

routes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
csh_auth "github.com/liam-middlebrook/csh-auth"
66
log "github.com/sirupsen/logrus"
77
"image"
8+
_ "image/gif"
89
_ "image/jpeg"
910
_ "image/png"
10-
_ "image/gif"
1111
"io"
1212
"net/http"
1313
"strconv"

0 commit comments

Comments
 (0)