Skip to content

Commit 754c5d3

Browse files
author
Divjot Arora
authored
GODRIVER-1410 Add retryReads URI option (#228)
1 parent c2d06af commit 754c5d3

File tree

4 files changed

+94
-9
lines changed

4 files changed

+94
-9
lines changed

mongo/client_test.go

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,18 +179,59 @@ func TestClient(t *testing.T) {
179179
assert.Equal(t, rc, client.readConcern, "expected read concern %v, got %v", rc, client.readConcern)
180180
})
181181
t.Run("retry writes", func(t *testing.T) {
182+
retryWritesURI := "mongodb://localhost:27017/?retryWrites=false"
183+
retryWritesErrorURI := "mongodb://localhost:27017/?retryWrites=foobar"
184+
182185
testCases := []struct {
183186
name string
184187
opts *options.ClientOptions
188+
expectErr bool
185189
expectedRetry bool
186190
}{
187-
{"default", options.Client(), true},
188-
{"custom", options.Client().SetRetryWrites(false), false},
191+
{"default", options.Client(), false, true},
192+
{"custom options", options.Client().SetRetryWrites(false), false, false},
193+
{"custom URI", options.Client().ApplyURI(retryWritesURI), false, false},
194+
{"custom URI error", options.Client().ApplyURI(retryWritesErrorURI), true, false},
189195
}
190196
for _, tc := range testCases {
191-
client := setupClient(tc.opts)
192-
assert.Equal(t, tc.expectedRetry, client.retryWrites,
193-
"expected retryWrites %v, got %v", tc.expectedRetry, client.retryWrites)
197+
t.Run(tc.name, func(t *testing.T) {
198+
client, err := NewClient(tc.opts)
199+
if tc.expectErr {
200+
assert.NotNil(t, err, "expected error, got nil")
201+
return
202+
}
203+
assert.Nil(t, err, "configuration error: %v", err)
204+
assert.Equal(t, tc.expectedRetry, client.retryWrites, "expected retryWrites %v, got %v",
205+
tc.expectedRetry, client.retryWrites)
206+
})
207+
}
208+
})
209+
t.Run("retry reads", func(t *testing.T) {
210+
retryReadsURI := "mongodb://localhost:27017/?retryReads=false"
211+
retryReadsErrorURI := "mongodb://localhost:27017/?retryReads=foobar"
212+
213+
testCases := []struct {
214+
name string
215+
opts *options.ClientOptions
216+
expectErr bool
217+
expectedRetry bool
218+
}{
219+
{"default", options.Client(), false, true},
220+
{"custom options", options.Client().SetRetryReads(false), false, false},
221+
{"custom URI", options.Client().ApplyURI(retryReadsURI), false, false},
222+
{"custom URI error", options.Client().ApplyURI(retryReadsErrorURI), true, false},
223+
}
224+
for _, tc := range testCases {
225+
t.Run(tc.name, func(t *testing.T) {
226+
client, err := NewClient(tc.opts)
227+
if tc.expectErr {
228+
assert.NotNil(t, err, "expected error, got nil")
229+
return
230+
}
231+
assert.Nil(t, err, "configuration error: %v", err)
232+
assert.Equal(t, tc.expectedRetry, client.retryReads, "expected retryReads %v, got %v",
233+
tc.expectedRetry, client.retryReads)
234+
})
194235
}
195236
})
196237
t.Run("write concern", func(t *testing.T) {

mongo/options/clientoptions.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,10 @@ func (c *ClientOptions) ApplyURI(uri string) *ClientOptions {
219219
c.RetryWrites = &cs.RetryWrites
220220
}
221221

222+
if cs.RetryReadsSet {
223+
c.RetryReads = &cs.RetryReads
224+
}
225+
222226
if cs.ReplicaSet != "" {
223227
c.ReplicaSet = &cs.ReplicaSet
224228
}

x/mongo/driver/connstring/connstring.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ type ConnString struct {
6464
ReadPreferenceTagSets []map[string]string
6565
RetryWrites bool
6666
RetryWritesSet bool
67+
RetryReads bool
68+
RetryReadsSet bool
6769
MaxStaleness time.Duration
6870
MaxStalenessSet bool
6971
ReplicaSet string
@@ -553,6 +555,17 @@ func (p *parser) addOption(pair string) error {
553555
}
554556

555557
p.RetryWritesSet = true
558+
case "retryreads":
559+
switch value {
560+
case "true":
561+
p.RetryReads = true
562+
case "false":
563+
p.RetryReads = false
564+
default:
565+
return fmt.Errorf("invalid value for %s: %s", key, value)
566+
}
567+
568+
p.RetryReadsSet = true
556569
case "serverselectiontimeoutms":
557570
n, err := strconv.Atoi(value)
558571
if err != nil || n < 0 {

x/mongo/driver/connstring/connstring_test.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ func TestRetryWrites(t *testing.T) {
389389
}{
390390
{s: "retryWrites=true", expected: true},
391391
{s: "retryWrites=false", expected: false},
392+
{s: "retryWrites=foobar", expected: false, err: true},
392393
}
393394

394395
for _, test := range tests {
@@ -397,11 +398,37 @@ func TestRetryWrites(t *testing.T) {
397398
cs, err := connstring.Parse(s)
398399
if test.err {
399400
require.Error(t, err)
400-
} else {
401-
require.NoError(t, err)
402-
require.Equal(t, test.expected, cs.RetryWrites)
403-
require.Equal(t, true, cs.RetryWritesSet)
401+
return
402+
}
403+
require.NoError(t, err)
404+
require.Equal(t, test.expected, cs.RetryWrites)
405+
require.True(t, cs.RetryWritesSet)
406+
})
407+
}
408+
}
409+
410+
func TestRetryReads(t *testing.T) {
411+
tests := []struct {
412+
s string
413+
expected bool
414+
err bool
415+
}{
416+
{s: "retryReads=true", expected: true},
417+
{s: "retryReads=false", expected: false},
418+
{s: "retryReads=foobar", expected: false, err: true},
419+
}
420+
421+
for _, test := range tests {
422+
s := fmt.Sprintf("mongodb://localhost/?%s", test.s)
423+
t.Run(s, func(t *testing.T) {
424+
cs, err := connstring.Parse(s)
425+
if test.err {
426+
require.Error(t, err)
427+
return
404428
}
429+
require.NoError(t, err)
430+
require.Equal(t, test.expected, cs.RetryReads)
431+
require.True(t, cs.RetryReadsSet)
405432
})
406433
}
407434
}

0 commit comments

Comments
 (0)