Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry mgo dial if unexpected message. #109

Merged
merged 1 commit into from
Sep 6, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion mgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ import (

"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/retry"
jc "github.com/juju/testing/checkers"
"github.com/juju/utils"
"github.com/juju/utils/clock"
"github.com/juju/version"
gc "gopkg.in/check.v1"
"gopkg.in/mgo.v2"
Expand Down Expand Up @@ -485,7 +487,23 @@ func (s *MgoSuite) TearDownSuite(c *gc.C) {

// Dial returns a new connection to the MongoDB server.
func (inst *MgoInstance) Dial() (*mgo.Session, error) {
return mgo.DialWithInfo(inst.DialInfo())
var session *mgo.Session
err := retry.Call(retry.CallArgs{
Func: func() error {
var err error
session, err = mgo.DialWithInfo(inst.DialInfo())
return err
},
// Only interested in retrying the intermittent
// 'unexpected message'.
IsFatalError: func(err error) bool {
return !strings.HasSuffix(err.Error(), "unexpected message")
},
Delay: time.Millisecond,
Clock: clock.WallClock,
Attempts: 5,
})
return session, err
}

// DialInfo returns information suitable for dialling the
Expand Down