Skip to content

Commit 2e9fa92

Browse files
DaytonGdomodwyer
authored andcommitted
add URI options: "w", "j", "wtimeoutMS" (#162)
* add URI options: "w", "j", "wtimeoutMS" * change "w" to "j"
1 parent 72d0ac2 commit 2e9fa92

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

session.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ func ParseURL(url string) (*DialInfo, error) {
342342
var readPreferenceTagSets []bson.D
343343
minPoolSize := 0
344344
maxIdleTimeMS := 0
345+
safe := Safe{}
345346
for _, opt := range uinfo.options {
346347
switch opt.key {
347348
case "authSource":
@@ -352,6 +353,23 @@ func ParseURL(url string) (*DialInfo, error) {
352353
service = opt.value
353354
case "replicaSet":
354355
setName = opt.value
356+
case "w":
357+
safe.WMode = opt.value
358+
case "j":
359+
journal, err := strconv.ParseBool(opt.value)
360+
if err != nil {
361+
return nil, errors.New("bad value for j: " + opt.value)
362+
}
363+
safe.J = journal
364+
case "wtimeoutMS":
365+
timeout, err := strconv.Atoi(opt.value)
366+
if err != nil {
367+
return nil, errors.New("bad value for wtimeoutMS: " + opt.value)
368+
}
369+
if timeout < 0 {
370+
return nil, errors.New("bad value (negative) for wtimeoutMS: " + opt.value)
371+
}
372+
safe.WTimeout = timeout
355373
case "maxPoolSize":
356374
poolLimit, err = strconv.Atoi(opt.value)
357375
if err != nil {
@@ -394,15 +412,15 @@ func ParseURL(url string) (*DialInfo, error) {
394412
return nil, errors.New("bad value for minPoolSize: " + opt.value)
395413
}
396414
if minPoolSize < 0 {
397-
return nil, errors.New("bad value (negtive) for minPoolSize: " + opt.value)
415+
return nil, errors.New("bad value (negative) for minPoolSize: " + opt.value)
398416
}
399417
case "maxIdleTimeMS":
400418
maxIdleTimeMS, err = strconv.Atoi(opt.value)
401419
if err != nil {
402420
return nil, errors.New("bad value for maxIdleTimeMS: " + opt.value)
403421
}
404422
if maxIdleTimeMS < 0 {
405-
return nil, errors.New("bad value (negtive) for maxIdleTimeMS: " + opt.value)
423+
return nil, errors.New("bad value (negative) for maxIdleTimeMS: " + opt.value)
406424
}
407425
case "connect":
408426
if opt.value == "direct" {
@@ -437,6 +455,7 @@ func ParseURL(url string) (*DialInfo, error) {
437455
Mode: readPreferenceMode,
438456
TagSets: readPreferenceTagSets,
439457
},
458+
Safe: safe,
440459
ReplicaSetName: setName,
441460
MinPoolSize: minPoolSize,
442461
MaxIdleTimeMS: maxIdleTimeMS,
@@ -529,6 +548,9 @@ type DialInfo struct {
529548
// Session.SetMode and Session.SelectServers.
530549
ReadPreference *ReadPreference
531550

551+
// Safe mostly defines write options, though there is RMode. See Session.SetSafe
552+
Safe Safe
553+
532554
// FailFast will cause connection and query attempts to fail faster when
533555
// the server is unavailable, instead of retrying until the configured
534556
// timeout period. Note that an unavailable server may silently drop
@@ -715,6 +737,8 @@ func DialWithInfo(dialInfo *DialInfo) (*Session, error) {
715737
return nil, err
716738
}
717739

740+
session.SetSafe(&info.Safe)
741+
718742
if info.ReadPreference != nil {
719743
session.SelectServers(info.ReadPreference.TagSets...)
720744
session.SetMode(info.ReadPreference.Mode, true)

session_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,43 @@ func (s *S) TestURLInvalidReadPreference(c *C) {
168168
}
169169
}
170170

171+
func (s *S) TestURLSafe(c *C) {
172+
type test struct {
173+
url string
174+
safe mgo.Safe
175+
}
176+
177+
tests := []test{
178+
{"localhost:40001?w=majority", mgo.Safe{WMode: "majority"}},
179+
{"localhost:40001?j=true", mgo.Safe{J: true}},
180+
{"localhost:40001?j=false", mgo.Safe{J: false}},
181+
{"localhost:40001?wtimeoutMS=1", mgo.Safe{WTimeout: 1}},
182+
{"localhost:40001?wtimeoutMS=1000", mgo.Safe{WTimeout: 1000}},
183+
{"localhost:40001?w=1&j=true&wtimeoutMS=1000", mgo.Safe{WMode: "1", J: true, WTimeout: 1000}},
184+
}
185+
186+
for _, test := range tests {
187+
info, err := mgo.ParseURL(test.url)
188+
c.Assert(err, IsNil)
189+
c.Assert(info.Safe, NotNil)
190+
c.Assert(info.Safe, Equals, test.safe)
191+
}
192+
}
193+
194+
func (s *S) TestURLInvalidSafe(c *C) {
195+
urls := []string{
196+
"localhost:40001?wtimeoutMS=abc",
197+
"localhost:40001?wtimeoutMS=",
198+
"localhost:40001?wtimeoutMS=-1",
199+
"localhost:40001?j=12",
200+
"localhost:40001?j=foo",
201+
}
202+
for _, url := range urls {
203+
_, err := mgo.ParseURL(url)
204+
c.Assert(err, NotNil)
205+
}
206+
}
207+
171208
func (s *S) TestMinPoolSize(c *C) {
172209
tests := []struct {
173210
url string

0 commit comments

Comments
 (0)