Skip to content

Commit 624a646

Browse files
committed
Merge pull request cesanta#79 from carsonoid/mongo_failover_retry
Retry connection to mongo when we get a EOF error.
2 parents cb71892 + c4cb4ac commit 624a646

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

auth_server/authn/mongo_auth.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
package authn
1818

1919
import (
20+
"errors"
2021
"fmt"
22+
"io"
23+
"time"
2124

2225
"github.com/cesanta/docker_auth/auth_server/mgo_session"
2326
"github.com/golang/glog"
@@ -80,6 +83,20 @@ func NewMongoAuth(c *MongoAuthConfig) (*MongoAuth, error) {
8083
}
8184

8285
func (mauth *MongoAuth) Authenticate(account string, password PasswordString) (bool, error) {
86+
for true {
87+
result, err := mauth.authenticate(account, password)
88+
if err == io.EOF {
89+
glog.Warningf("EOF error received from Mongo. Retrying connection")
90+
time.Sleep(time.Second)
91+
continue
92+
}
93+
return result, err
94+
}
95+
96+
return false, errors.New("Unable to communicate with Mongo.")
97+
}
98+
99+
func (mauth *MongoAuth) authenticate(account string, password PasswordString) (bool, error) {
83100
// Copy our session
84101
tmp_session := mauth.session.Copy()
85102
// Close up when we are done

auth_server/authz/acl_mongo.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ package authz
33
import (
44
"errors"
55
"fmt"
6-
"sync"
7-
"time"
8-
96
"github.com/cesanta/docker_auth/auth_server/mgo_session"
107
"github.com/golang/glog"
118
"gopkg.in/mgo.v2"
129
"gopkg.in/mgo.v2/bson"
10+
"io"
11+
"sync"
12+
"time"
1313
)
1414

1515
type MongoACL []MongoACLEntry
@@ -115,13 +115,20 @@ func (ma *aclMongoAuthorizer) continuouslyUpdateACLCache() {
115115
aclAge := time.Now().Sub(ma.lastCacheUpdate)
116116
glog.V(2).Infof("Updating ACL at %s (ACL age: %s. CacheTTL: %s)", tick, aclAge, ma.config.CacheTTL)
117117

118-
err := ma.updateACLCache()
119-
if err == nil {
120-
continue
118+
for true {
119+
err := ma.updateACLCache()
120+
if err == nil {
121+
break
122+
} else if err == io.EOF {
123+
glog.Warningf("EOF error received from Mongo. Retrying connection")
124+
time.Sleep(time.Second)
125+
continue
126+
} else {
127+
glog.Errorf("Failed to update ACL. ERROR: %s", err)
128+
glog.Warningf("Using stale ACL (Age: %s, TTL: %s)", aclAge, ma.config.CacheTTL)
129+
break
130+
}
121131
}
122-
123-
glog.Errorf("Failed to update ACL. ERROR: %s", err)
124-
glog.Warningf("Using stale ACL (Age: %s, TTL: %s)", aclAge, ma.config.CacheTTL)
125132
}
126133
}
127134

0 commit comments

Comments
 (0)