Skip to content

Commit 802b0ba

Browse files
author
Isabella Siu
committed
GODRIVER-211 revise connection pooling options on client/connstring
Change-Id: Ic9b1f10ef3646e535a19a20a53decc0444fe1fe1
1 parent 37b6cf6 commit 802b0ba

File tree

7 files changed

+25
-162
lines changed

7 files changed

+25
-162
lines changed

internal/testutil/helpers/helpers.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,9 @@ func VerifyConnStringOptions(t *testing.T, cs connstring.ConnString, options map
108108
require.Equal(t, value, cs.J)
109109
case "maxidletimems":
110110
require.Equal(t, value, cs.MaxConnIdleTime)
111-
case "maxconnlifetimems":
112-
require.Equal(t, value, cs.MaxConnLifeTime)
113-
case "maxconnsperhost":
114-
require.True(t, cs.MaxIdleConnsPerHostSet)
115-
require.Equal(t, value, cs.MaxIdleConnsPerHost)
116-
case "maxidleconnsperhost":
117-
require.True(t, cs.MaxIdleConnsPerHostSet)
118-
require.Equal(t, value, cs.MaxIdleConnsPerHost)
111+
case "maxpoolsize":
112+
require.True(t, cs.MaxPoolSizeSet)
113+
require.Equal(t, value, cs.MaxPoolSize)
119114
case "readpreference":
120115
require.Equal(t, value, cs.ReadPreference)
121116
case "readpreferencetags":

mongo/client_options_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ func TestClientOptions_chainAll(t *testing.T) {
9898
}).SetConnectTimeout(500 * time.Millisecond).SetHeartbeatInterval(15 * time.Second).SetHosts([]string{
9999
"mongodb://localhost:27018",
100100
"mongodb://localhost:27019",
101-
}).SetLocalThreshold(time.Second).SetMaxConnIdleTime(30 * time.Second).SetMaxConnsPerHost(150).
102-
SetMaxIdleConnsPerHost(20).SetReadConcern(rc).SetReadPreference(rp).SetReplicaSet("foo").
101+
}).SetLocalThreshold(time.Second).SetMaxConnIdleTime(30 * time.Second).SetMaxPoolSize(150).
102+
SetReadConcern(rc).SetReadPreference(rp).SetReplicaSet("foo").
103103
SetRetryWrites(retryWrites).SetServerSelectionTimeout(time.Second).
104104
SetSingle(false).SetSocketTimeout(2 * time.Second).SetSSL(&options.SSLOpt{
105105
Enabled: true,
@@ -130,10 +130,8 @@ func TestClientOptions_chainAll(t *testing.T) {
130130
LocalThreshold: time.Second,
131131
MaxConnIdleTime: 30 * time.Second,
132132
MaxConnIdleTimeSet: true,
133-
MaxConnsPerHost: 150,
134-
MaxConnsPerHostSet: true,
135-
MaxIdleConnsPerHost: 20,
136-
MaxIdleConnsPerHostSet: true,
133+
MaxPoolSize: 150,
134+
MaxPoolSizeSet: true,
137135
ReplicaSet: "foo",
138136
ServerSelectionTimeoutSet: true,
139137
ServerSelectionTimeout: time.Second,

mongo/options/clientoptions.go

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -194,19 +194,10 @@ func (c *ClientOptions) SetMaxConnIdleTime(d time.Duration) *ClientOptions {
194194
return c
195195
}
196196

197-
// SetMaxConnsPerHost specifies the max size of a server's connection pool.
198-
func (c *ClientOptions) SetMaxConnsPerHost(u uint16) *ClientOptions {
199-
c.ConnString.MaxConnsPerHost = u
200-
c.ConnString.MaxConnsPerHostSet = true
201-
202-
return c
203-
}
204-
205-
// SetMaxIdleConnsPerHost specifies the number of connections in a server's connection pool that can
206-
// be idle at any given time.
207-
func (c *ClientOptions) SetMaxIdleConnsPerHost(u uint16) *ClientOptions {
208-
c.ConnString.MaxIdleConnsPerHost = u
209-
c.ConnString.MaxIdleConnsPerHostSet = true
197+
// SetMaxPoolSize specifies the max size of a server's connection pool.
198+
func (c *ClientOptions) SetMaxPoolSize(u uint16) *ClientOptions {
199+
c.ConnString.MaxPoolSize = u
200+
c.ConnString.MaxPoolSizeSet = true
210201

211202
return c
212203
}
@@ -353,13 +344,9 @@ func MergeClientOptions(cs connstring.ConnString, opts ...*ClientOptions) *Clien
353344
c.ConnString.MaxConnIdleTimeSet = true
354345
c.ConnString.MaxConnIdleTime = opt.ConnString.MaxConnIdleTime
355346
}
356-
if opt.ConnString.MaxConnsPerHostSet {
357-
c.ConnString.MaxConnsPerHostSet = true
358-
c.ConnString.MaxConnsPerHost = opt.ConnString.MaxConnsPerHost
359-
}
360-
if opt.ConnString.MaxIdleConnsPerHostSet {
361-
c.ConnString.MaxIdleConnsPerHostSet = true
362-
c.ConnString.MaxIdleConnsPerHost = opt.ConnString.MaxIdleConnsPerHost
347+
if opt.ConnString.MaxPoolSizeSet {
348+
c.ConnString.MaxPoolSizeSet = true
349+
c.ConnString.MaxPoolSize = opt.ConnString.MaxPoolSize
363350
}
364351
if opt.ReadConcern != nil {
365352
c.ReadConcern = opt.ReadConcern

x/mongo/driver/topology/topology_options.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,9 @@ func WithConnString(fn func(connstring.ConnString) connstring.ConnString) Option
8989
connOpts = append(connOpts, connection.WithIdleTimeout(func(time.Duration) time.Duration { return cs.MaxConnIdleTime }))
9090
}
9191

92-
if cs.MaxConnLifeTime > 0 {
93-
connOpts = append(connOpts, connection.WithIdleTimeout(func(time.Duration) time.Duration { return cs.MaxConnLifeTime }))
94-
}
95-
96-
if cs.MaxConnsPerHostSet {
97-
c.serverOpts = append(c.serverOpts, WithMaxConnections(func(uint16) uint16 { return cs.MaxConnsPerHost }))
98-
}
99-
100-
if cs.MaxIdleConnsPerHostSet {
101-
c.serverOpts = append(c.serverOpts, WithMaxIdleConnections(func(uint16) uint16 { return cs.MaxIdleConnsPerHost }))
92+
if cs.MaxPoolSizeSet {
93+
c.serverOpts = append(c.serverOpts, WithMaxConnections(func(uint16) uint16 { return cs.MaxPoolSize }))
94+
c.serverOpts = append(c.serverOpts, WithMaxIdleConnections(func(uint16) uint16 { return cs.MaxPoolSize }))
10295
}
10396

10497
if cs.ReplicaSet != "" {

x/network/connstring/connstring.go

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,8 @@ type ConnString struct {
5353
LocalThresholdSet bool
5454
MaxConnIdleTime time.Duration
5555
MaxConnIdleTimeSet bool
56-
MaxConnLifeTime time.Duration
57-
MaxConnsPerHost uint16
58-
MaxConnsPerHostSet bool
59-
MaxIdleConnsPerHost uint16
60-
MaxIdleConnsPerHostSet bool
56+
MaxPoolSize uint16
57+
MaxPoolSizeSet bool
6158
Password string
6259
PasswordSet bool
6360
ReadConcernLevel string
@@ -529,42 +526,20 @@ func (p *parser) addOption(pair string) error {
529526
}
530527
p.LocalThreshold = time.Duration(n) * time.Millisecond
531528
p.LocalThresholdSet = true
532-
case "maxconnsperhost":
533-
n, err := strconv.Atoi(value)
534-
if err != nil || n < 0 {
535-
return fmt.Errorf("invalid value for %s: %s", key, value)
536-
}
537-
p.MaxConnsPerHost = uint16(n)
538-
p.MaxConnsPerHostSet = true
539-
case "maxidleconnsperhost":
540-
n, err := strconv.Atoi(value)
541-
if err != nil || n < 0 {
542-
return fmt.Errorf("invalid value for %s: %s", key, value)
543-
}
544-
p.MaxIdleConnsPerHost = uint16(n)
545-
p.MaxIdleConnsPerHostSet = true
546529
case "maxidletimems":
547530
n, err := strconv.Atoi(value)
548531
if err != nil || n < 0 {
549532
return fmt.Errorf("invalid value for %s: %s", key, value)
550533
}
551534
p.MaxConnIdleTime = time.Duration(n) * time.Millisecond
552535
p.MaxConnIdleTimeSet = true
553-
case "maxlifetimems":
554-
n, err := strconv.Atoi(value)
555-
if err != nil || n < 0 {
556-
return fmt.Errorf("invalid value for %s: %s", key, value)
557-
}
558-
p.MaxConnLifeTime = time.Duration(n) * time.Millisecond
559536
case "maxpoolsize":
560537
n, err := strconv.Atoi(value)
561538
if err != nil || n < 0 {
562539
return fmt.Errorf("invalid value for %s: %s", key, value)
563540
}
564-
p.MaxConnsPerHost = uint16(n)
565-
p.MaxConnsPerHostSet = true
566-
p.MaxIdleConnsPerHost = uint16(n)
567-
p.MaxIdleConnsPerHostSet = true
541+
p.MaxPoolSize = uint16(n)
542+
p.MaxPoolSizeSet = true
568543
case "readconcernlevel":
569544
p.ReadConcernLevel = value
570545
case "readpreference":

x/network/connstring/connstring_spec_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,8 @@ func runTest(t *testing.T, filename string, test *testCase) {
138138
// for options that are present.
139139
var ok bool
140140

141-
_, ok = test.Options["maxconnsperhost"]
142-
require.Equal(t, ok, cs.MaxConnsPerHostSet)
143-
144-
_, ok = test.Options["maxidleconnsperhost"]
145-
require.Equal(t, ok, cs.MaxIdleConnsPerHostSet)
141+
_, ok = test.Options["maxpoolsize"]
142+
require.Equal(t, ok, cs.MaxPoolSizeSet)
146143

147144
require.Equal(t, test.Auth != nil && test.Auth.Password != nil, cs.PasswordSet)
148145
})

x/network/connstring/connstring_test.go

Lines changed: 2 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -225,86 +225,6 @@ func TestMaxConnIdleTime(t *testing.T) {
225225
}
226226
}
227227

228-
func TestMaxConnLifeTime(t *testing.T) {
229-
tests := []struct {
230-
s string
231-
expected time.Duration
232-
err bool
233-
}{
234-
{s: "maxLifeTimeMS=10", expected: time.Duration(10) * time.Millisecond},
235-
{s: "maxLifeTimeMS=100", expected: time.Duration(100) * time.Millisecond},
236-
{s: "maxLifeTimeMS=-2", err: true},
237-
{s: "maxLifeTimeMS=gsdge", err: true},
238-
}
239-
240-
for _, test := range tests {
241-
s := fmt.Sprintf("mongodb://localhost/?%s", test.s)
242-
t.Run(s, func(t *testing.T) {
243-
cs, err := connstring.Parse(s)
244-
if test.err {
245-
require.Error(t, err)
246-
} else {
247-
require.NoError(t, err)
248-
require.Equal(t, test.expected, cs.MaxConnLifeTime)
249-
}
250-
})
251-
}
252-
}
253-
254-
func TestMaxConnsPerHost(t *testing.T) {
255-
tests := []struct {
256-
s string
257-
expected uint16
258-
err bool
259-
}{
260-
{s: "maxConnsPerHost=10", expected: 10},
261-
{s: "maxConnsPerHost=100", expected: 100},
262-
{s: "maxConnsPerHost=-2", err: true},
263-
{s: "maxConnsPerHost=gsdge", err: true},
264-
}
265-
266-
for _, test := range tests {
267-
s := fmt.Sprintf("mongodb://localhost/?%s", test.s)
268-
t.Run(s, func(t *testing.T) {
269-
cs, err := connstring.Parse(s)
270-
if test.err {
271-
require.Error(t, err)
272-
} else {
273-
require.NoError(t, err)
274-
require.True(t, cs.MaxConnsPerHostSet)
275-
require.Equal(t, test.expected, cs.MaxConnsPerHost)
276-
}
277-
})
278-
}
279-
}
280-
281-
func TestMaxIdleConnsPerHost(t *testing.T) {
282-
tests := []struct {
283-
s string
284-
expected uint16
285-
err bool
286-
}{
287-
{s: "maxIdleConnsPerHost=10", expected: 10},
288-
{s: "maxIdleConnsPerHost=100", expected: 100},
289-
{s: "maxIdleConnsPerHost=-2", err: true},
290-
{s: "maxIdleConnsPerHost=gsdge", err: true},
291-
}
292-
293-
for _, test := range tests {
294-
s := fmt.Sprintf("mongodb://localhost/?%s", test.s)
295-
t.Run(s, func(t *testing.T) {
296-
cs, err := connstring.Parse(s)
297-
if test.err {
298-
require.Error(t, err)
299-
} else {
300-
require.NoError(t, err)
301-
require.True(t, cs.MaxIdleConnsPerHostSet)
302-
require.Equal(t, test.expected, cs.MaxIdleConnsPerHost)
303-
}
304-
})
305-
}
306-
}
307-
308228
func TestMaxPoolSize(t *testing.T) {
309229
tests := []struct {
310230
s string
@@ -325,10 +245,8 @@ func TestMaxPoolSize(t *testing.T) {
325245
require.Error(t, err)
326246
} else {
327247
require.NoError(t, err)
328-
require.True(t, cs.MaxConnsPerHostSet)
329-
require.Equal(t, test.expected, cs.MaxConnsPerHost)
330-
require.True(t, cs.MaxIdleConnsPerHostSet)
331-
require.Equal(t, test.expected, cs.MaxIdleConnsPerHost)
248+
require.True(t, cs.MaxPoolSizeSet)
249+
require.Equal(t, test.expected, cs.MaxPoolSize)
332250
}
333251
})
334252
}

0 commit comments

Comments
 (0)