Skip to content

Commit

Permalink
Remove writeConcern struct, move JSON unmarshal to Initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
calvn committed Dec 5, 2017
1 parent 24f37a2 commit 6934d5c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 33 deletions.
49 changes: 26 additions & 23 deletions plugins/database/mongodb/connection_producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type mongoDBConnectionProducer struct {
Initialized bool
Type string
session *mgo.Session
safe *mgo.Safe
sync.Mutex
}

Expand All @@ -45,6 +46,29 @@ func (c *mongoDBConnectionProducer) Initialize(conf map[string]interface{}, veri
return fmt.Errorf("connection_url cannot be empty")
}

if c.WriteConcern != "" {
input := c.WriteConcern

// Try to base64 decode the input. If successful, consider the decoded
// value as input.
inputBytes, err := base64.StdEncoding.DecodeString(input)
if err == nil {
input = string(inputBytes)
}

concern := &mgo.Safe{}
err = json.Unmarshal([]byte(input), concern)
if err != nil {
return fmt.Errorf("error mashalling write concern: %s", err)
}

// Only set c.safe if anything got marshaled into concern. We don't want to
// pass an empty, non-nil mgo.Safe object into mgo.SetSafe in Connection().
if (mgo.Safe{} != *concern) {
c.safe = concern
}
}

// Set initialized to true at this point since all fields are set,
// and the connection can be established at a later time.
c.Initialized = true
Expand Down Expand Up @@ -82,29 +106,8 @@ func (c *mongoDBConnectionProducer) Connection() (interface{}, error) {
return nil, err
}

if c.WriteConcern != "" {
input := c.WriteConcern

// Try to base64 decode the input. If successful, consider the decoded
// value as input.
inputBytes, err := base64.StdEncoding.DecodeString(input)
if err == nil {
input = string(inputBytes)
}

var concern writeConcern
err = json.Unmarshal([]byte(input), &concern)
if err != nil {
return nil, err
}

c.session.SetSafe(&mgo.Safe{
W: concern.W,
WMode: concern.WMode,
WTimeout: concern.WTimeout,
FSync: concern.FSync,
J: concern.J,
})
if c.safe != nil {
c.session.SetSafe(c.safe)
}

c.session.SetSyncTimeout(1 * time.Minute)
Expand Down
10 changes: 0 additions & 10 deletions plugins/database/mongodb/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,6 @@ type mongoDBStatement struct {
Roles mongodbRoles `json:"roles"`
}

// writeConcern is the mgo.Safe struct with JSON tags
// More info: https://godoc.org/gopkg.in/mgo.v2#Safe
type writeConcern struct {
W int `json:"w"` // Min # of servers to ack before success
WMode string `json:"w_mode"` // Write mode for MongoDB 2.0+ (e.g. "majority")
WTimeout int `json:"wtimeout"` // Milliseconds to wait for W before timing out
FSync bool `json:"fsync"` // Sync via the journal if present, or via data files sync otherwise
J bool `json:"j"` // Sync via the journal if present
}

// Convert array of role documents like:
//
// [ { "role": "readWrite" }, { "role": "readWrite", "db": "test" } ]
Expand Down

0 comments on commit 6934d5c

Please sign in to comment.