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

Try reconnecting Mongo on EOF #3269

Merged
merged 3 commits into from
Aug 31, 2017
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
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