Skip to content

Commit

Permalink
Enforce index in users collection
Browse files Browse the repository at this point in the history
  • Loading branch information
Carson Anderson committed Feb 11, 2016
1 parent d92bf60 commit 67d2e58
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions auth_server/authn/mongo_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,30 @@ func NewMongoAuth(c *MongoAuthConfig) (*MongoAuth, error) {
return nil, err
}

// Copy our session
tmp_session := session.Copy()
// Close up when we are done
defer tmp_session.Close()

// determine collection
collection := tmp_session.DB(c.MongoConfig.DialInfo.Database).C(c.Collection)

// Create username index obj
index := mgo.Index{
Key: []string{"username"},
Unique: true,
DropDups: false, // Error on duplicate key document instead of drop.
}

// Enforce a username index. This is fine to do frequently per the docs:
// https://godoc.org/gopkg.in/mgo.v2#Collection.EnsureIndex:
// Once EnsureIndex returns successfully, following requests for the same index
// will not contact the server unless Collection.DropIndex is used to drop the same
// index, or Session.ResetIndexCache is called.
if err := collection.EnsureIndex(index); err != nil {
return nil, err
}

return &MongoAuth{
config: c,
session: session,
Expand Down

0 comments on commit 67d2e58

Please sign in to comment.