Skip to content

Commit

Permalink
add URI options: "w", "j", "wtimeoutMS" (#162)
Browse files Browse the repository at this point in the history
* add URI options: "w", "j", "wtimeoutMS"

* change "w" to "j"
  • Loading branch information
DaytonG authored and domodwyer committed May 14, 2018
1 parent 72d0ac2 commit 2e9fa92
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
28 changes: 26 additions & 2 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ func ParseURL(url string) (*DialInfo, error) {
var readPreferenceTagSets []bson.D
minPoolSize := 0
maxIdleTimeMS := 0
safe := Safe{}
for _, opt := range uinfo.options {
switch opt.key {
case "authSource":
Expand All @@ -352,6 +353,23 @@ func ParseURL(url string) (*DialInfo, error) {
service = opt.value
case "replicaSet":
setName = opt.value
case "w":
safe.WMode = opt.value
case "j":
journal, err := strconv.ParseBool(opt.value)
if err != nil {
return nil, errors.New("bad value for j: " + opt.value)
}
safe.J = journal
case "wtimeoutMS":
timeout, err := strconv.Atoi(opt.value)
if err != nil {
return nil, errors.New("bad value for wtimeoutMS: " + opt.value)
}
if timeout < 0 {
return nil, errors.New("bad value (negative) for wtimeoutMS: " + opt.value)
}
safe.WTimeout = timeout
case "maxPoolSize":
poolLimit, err = strconv.Atoi(opt.value)
if err != nil {
Expand Down Expand Up @@ -394,15 +412,15 @@ func ParseURL(url string) (*DialInfo, error) {
return nil, errors.New("bad value for minPoolSize: " + opt.value)
}
if minPoolSize < 0 {
return nil, errors.New("bad value (negtive) for minPoolSize: " + opt.value)
return nil, errors.New("bad value (negative) for minPoolSize: " + opt.value)
}
case "maxIdleTimeMS":
maxIdleTimeMS, err = strconv.Atoi(opt.value)
if err != nil {
return nil, errors.New("bad value for maxIdleTimeMS: " + opt.value)
}
if maxIdleTimeMS < 0 {
return nil, errors.New("bad value (negtive) for maxIdleTimeMS: " + opt.value)
return nil, errors.New("bad value (negative) for maxIdleTimeMS: " + opt.value)
}
case "connect":
if opt.value == "direct" {
Expand Down Expand Up @@ -437,6 +455,7 @@ func ParseURL(url string) (*DialInfo, error) {
Mode: readPreferenceMode,
TagSets: readPreferenceTagSets,
},
Safe: safe,
ReplicaSetName: setName,
MinPoolSize: minPoolSize,
MaxIdleTimeMS: maxIdleTimeMS,
Expand Down Expand Up @@ -529,6 +548,9 @@ type DialInfo struct {
// Session.SetMode and Session.SelectServers.
ReadPreference *ReadPreference

// Safe mostly defines write options, though there is RMode. See Session.SetSafe
Safe Safe

// FailFast will cause connection and query attempts to fail faster when
// the server is unavailable, instead of retrying until the configured
// timeout period. Note that an unavailable server may silently drop
Expand Down Expand Up @@ -715,6 +737,8 @@ func DialWithInfo(dialInfo *DialInfo) (*Session, error) {
return nil, err
}

session.SetSafe(&info.Safe)

if info.ReadPreference != nil {
session.SelectServers(info.ReadPreference.TagSets...)
session.SetMode(info.ReadPreference.Mode, true)
Expand Down
37 changes: 37 additions & 0 deletions session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,43 @@ func (s *S) TestURLInvalidReadPreference(c *C) {
}
}

func (s *S) TestURLSafe(c *C) {
type test struct {
url string
safe mgo.Safe
}

tests := []test{
{"localhost:40001?w=majority", mgo.Safe{WMode: "majority"}},
{"localhost:40001?j=true", mgo.Safe{J: true}},
{"localhost:40001?j=false", mgo.Safe{J: false}},
{"localhost:40001?wtimeoutMS=1", mgo.Safe{WTimeout: 1}},
{"localhost:40001?wtimeoutMS=1000", mgo.Safe{WTimeout: 1000}},
{"localhost:40001?w=1&j=true&wtimeoutMS=1000", mgo.Safe{WMode: "1", J: true, WTimeout: 1000}},
}

for _, test := range tests {
info, err := mgo.ParseURL(test.url)
c.Assert(err, IsNil)
c.Assert(info.Safe, NotNil)
c.Assert(info.Safe, Equals, test.safe)
}
}

func (s *S) TestURLInvalidSafe(c *C) {
urls := []string{
"localhost:40001?wtimeoutMS=abc",
"localhost:40001?wtimeoutMS=",
"localhost:40001?wtimeoutMS=-1",
"localhost:40001?j=12",
"localhost:40001?j=foo",
}
for _, url := range urls {
_, err := mgo.ParseURL(url)
c.Assert(err, NotNil)
}
}

func (s *S) TestMinPoolSize(c *C) {
tests := []struct {
url string
Expand Down

0 comments on commit 2e9fa92

Please sign in to comment.