Skip to content

Commit

Permalink
Merge pull request cesanta#79 from carsonoid/mongo_failover_retry
Browse files Browse the repository at this point in the history
Retry connection to mongo when we get a EOF error.
  • Loading branch information
rojer committed Mar 23, 2016
2 parents cb71892 + c4cb4ac commit 624a646
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
17 changes: 17 additions & 0 deletions auth_server/authn/mongo_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
package authn

import (
"errors"
"fmt"
"io"
"time"

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

func (mauth *MongoAuth) Authenticate(account string, password PasswordString) (bool, error) {
for true {
result, err := mauth.authenticate(account, password)
if err == io.EOF {
glog.Warningf("EOF error received from Mongo. Retrying connection")
time.Sleep(time.Second)
continue
}
return result, err
}

return false, errors.New("Unable to communicate with Mongo.")
}

func (mauth *MongoAuth) authenticate(account string, password PasswordString) (bool, error) {
// Copy our session
tmp_session := mauth.session.Copy()
// Close up when we are done
Expand Down
25 changes: 16 additions & 9 deletions auth_server/authz/acl_mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package authz
import (
"errors"
"fmt"
"sync"
"time"

"github.com/cesanta/docker_auth/auth_server/mgo_session"
"github.com/golang/glog"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"io"
"sync"
"time"
)

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

err := ma.updateACLCache()
if err == nil {
continue
for true {
err := ma.updateACLCache()
if err == nil {
break
} else if err == io.EOF {
glog.Warningf("EOF error received from Mongo. Retrying connection")
time.Sleep(time.Second)
continue
} else {
glog.Errorf("Failed to update ACL. ERROR: %s", err)
glog.Warningf("Using stale ACL (Age: %s, TTL: %s)", aclAge, ma.config.CacheTTL)
break
}
}

glog.Errorf("Failed to update ACL. ERROR: %s", err)
glog.Warningf("Using stale ACL (Age: %s, TTL: %s)", aclAge, ma.config.CacheTTL)
}
}

Expand Down

0 comments on commit 624a646

Please sign in to comment.