Skip to content

Commit 2b05121

Browse files
authored
Merge pull request #51 from netlify/salvador/connect-correct-replset
[mongo] Use the primary node only if it belongs to the given replica set
2 parents 6065f07 + 1dae327 commit 2b05121

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

mongo/db.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ type Config struct {
2424
TLS *nftls.Config `mapstructure:"tls_conf"`
2525
DB string `mapstructure:"db"`
2626
Servers []string `mapstructure:"servers"`
27+
ReplSetName string `mapstructure:"replset_name"`
2728
ConnTimeout int64 `mapstructure:"conn_timeout"`
2829
}
2930

3031
func Connect(config *Config, log *logrus.Entry) (*mgo.Database, error) {
3132
info := &mgo.DialInfo{
32-
Addrs: config.Servers,
33-
Timeout: time.Second * time.Duration(config.ConnTimeout),
33+
Addrs: config.Servers,
34+
ReplicaSetName: config.ReplSetName,
35+
Timeout: time.Second * time.Duration(config.ConnTimeout),
3436
}
3537

3638
if config.TLS != nil {
@@ -53,7 +55,11 @@ func Connect(config *Config, log *logrus.Entry) (*mgo.Database, error) {
5355
log.Debug("Skipping TLS config")
5456
}
5557

56-
log.WithField("servers", strings.Join(info.Addrs, ",")).Debug("Dialing database")
58+
log.WithFields(logrus.Fields{
59+
"servers": strings.Join(info.Addrs, ","),
60+
"replica_set": config.ReplSetName,
61+
}).Debug("Dialing database")
62+
5763
sess, err := mgo.DialWithInfo(info)
5864
if err != nil {
5965
return nil, err

0 commit comments

Comments
 (0)