Skip to content

Commit

Permalink
Try reconnecting Mongo on EOF (#3269)
Browse files Browse the repository at this point in the history
  • Loading branch information
jefferai authored Aug 31, 2017
1 parent 7230d4d commit 2748d9b
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions plugins/database/mongodb/mongodb.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package mongodb

import (
"io"
"strings"
"time"

"encoding/json"

"fmt"

"github.com/hashicorp/errwrap"
"github.com/hashicorp/vault/api"
"github.com/hashicorp/vault/builtin/logical/database/dbplugin"
"github.com/hashicorp/vault/plugins"
Expand Down Expand Up @@ -124,7 +127,21 @@ func (m *MongoDB) CreateUser(statements dbplugin.Statements, usernameConfig dbpl
}

err = session.DB(mongoCS.DB).Run(createUserCmd, nil)
if err != nil {
switch {
case err == nil:
case err == io.EOF, strings.Contains(err.Error(), "EOF"):
if err := m.ConnectionProducer.Close(); err != nil {
return "", "", errwrap.Wrapf("error closing EOF'd mongo connection: {{err}}", err)
}
session, err := m.getConnection()
if err != nil {
return "", "", err
}
err = session.DB(mongoCS.DB).Run(createUserCmd, nil)
if err != nil {
return "", "", err
}
default:
return "", "", err
}

Expand Down Expand Up @@ -165,7 +182,21 @@ func (m *MongoDB) RevokeUser(statements dbplugin.Statements, username string) er
}

err = session.DB(db).RemoveUser(username)
if err != nil && err != mgo.ErrNotFound {
switch {
case err == nil, err == mgo.ErrNotFound:
case err == io.EOF, strings.Contains(err.Error(), "EOF"):
if err := m.ConnectionProducer.Close(); err != nil {
return errwrap.Wrapf("error closing EOF'd mongo connection: {{err}}", err)
}
session, err := m.getConnection()
if err != nil {
return err
}
err = session.DB(db).RemoveUser(username)
if err != nil {
return err
}
default:
return err
}

Expand Down

0 comments on commit 2748d9b

Please sign in to comment.