Skip to content

add URI options: "w", "j", "wtimeoutMS" #162

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

Merged
merged 2 commits into from
May 14, 2018
Merged
Show file tree
Hide file tree
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
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