Skip to content

Commit c840793

Browse files
authored
Merge pull request #109 from howbazaar/retry-dial
Retry mgo dial if unexpected message. Very rarely, but frequently enough in our test runs, dialing mgo will fail with "unexpected message". Wrap that call in a retry, and try five times. Hopefully this will be sufficient. A minimal delay is specified. (Review request: http://reviews.vapour.ws/r/5608/)
2 parents d325c22 + a015870 commit c840793

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

mgo.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ import (
2727

2828
"github.com/juju/errors"
2929
"github.com/juju/loggo"
30+
"github.com/juju/retry"
3031
jc "github.com/juju/testing/checkers"
3132
"github.com/juju/utils"
33+
"github.com/juju/utils/clock"
3234
"github.com/juju/version"
3335
gc "gopkg.in/check.v1"
3436
"gopkg.in/mgo.v2"
@@ -485,7 +487,23 @@ func (s *MgoSuite) TearDownSuite(c *gc.C) {
485487

486488
// Dial returns a new connection to the MongoDB server.
487489
func (inst *MgoInstance) Dial() (*mgo.Session, error) {
488-
return mgo.DialWithInfo(inst.DialInfo())
490+
var session *mgo.Session
491+
err := retry.Call(retry.CallArgs{
492+
Func: func() error {
493+
var err error
494+
session, err = mgo.DialWithInfo(inst.DialInfo())
495+
return err
496+
},
497+
// Only interested in retrying the intermittent
498+
// 'unexpected message'.
499+
IsFatalError: func(err error) bool {
500+
return !strings.HasSuffix(err.Error(), "unexpected message")
501+
},
502+
Delay: time.Millisecond,
503+
Clock: clock.WallClock,
504+
Attempts: 5,
505+
})
506+
return session, err
489507
}
490508

491509
// DialInfo returns information suitable for dialling the

0 commit comments

Comments
 (0)