Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit 3116f72

Browse files
committed
Support standard maxPoolSize URL option.
1 parent 114d7e7 commit 3116f72

File tree

2 files changed

+50
-32
lines changed

2 files changed

+50
-32
lines changed

cluster_test.go

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,41 +1152,40 @@ func (s *S) TestRemovalOfClusterMember(c *C) {
11521152
}
11531153

11541154
func (s *S) TestPoolLimitSimple(c *C) {
1155-
session, err := mgo.Dial("localhost:40001")
1156-
c.Assert(err, IsNil)
1157-
defer session.Close()
1158-
1159-
stats := mgo.GetStats()
1160-
for stats.MasterConns+stats.SlaveConns != 1 {
1161-
stats = mgo.GetStats()
1162-
c.Log("Waiting for connection to be established...")
1163-
time.Sleep(100 * time.Millisecond)
1164-
}
1165-
1166-
c.Assert(stats.SocketsAlive, Equals, 1)
1167-
c.Assert(stats.SocketsInUse, Equals, 0)
1155+
for test := 0; test < 2; test++ {
1156+
var session *mgo.Session
1157+
var err error
1158+
if test == 0 {
1159+
session, err = mgo.Dial("localhost:40001")
1160+
c.Assert(err, IsNil)
1161+
session.SetPoolLimit(1)
1162+
} else {
1163+
session, err = mgo.Dial("localhost:40001?maxPoolSize=1")
1164+
c.Assert(err, IsNil)
1165+
}
1166+
defer session.Close()
11681167

1169-
// Put one socket in use.
1170-
c.Assert(session.Ping(), IsNil)
1168+
// Put one socket in use.
1169+
c.Assert(session.Ping(), IsNil)
11711170

1172-
done := make(chan time.Duration)
1171+
done := make(chan time.Duration)
11731172

1174-
// Now block trying to get another one due to the pool limit.
1175-
go func() {
1176-
copy := session.Copy()
1177-
defer copy.Close()
1178-
copy.SetPoolLimit(1)
1179-
started := time.Now()
1180-
c.Check(copy.Ping(), IsNil)
1181-
done <- time.Now().Sub(started)
1182-
}()
1173+
// Now block trying to get another one due to the pool limit.
1174+
go func() {
1175+
copy := session.Copy()
1176+
defer copy.Close()
1177+
started := time.Now()
1178+
c.Check(copy.Ping(), IsNil)
1179+
done <- time.Now().Sub(started)
1180+
}()
11831181

1184-
time.Sleep(500 * time.Millisecond)
1182+
time.Sleep(300 * time.Millisecond)
11851183

1186-
// Put the one socket back in the pool, freeing it for the copy.
1187-
session.Refresh()
1188-
delay := <-done
1189-
c.Assert(delay > 500 * time.Millisecond, Equals, true, Commentf("Delay: %s", delay))
1184+
// Put the one socket back in the pool, freeing it for the copy.
1185+
session.Refresh()
1186+
delay := <-done
1187+
c.Assert(delay > 300 * time.Millisecond, Equals, true, Commentf("Delay: %s", delay))
1188+
}
11901189
}
11911190

11921191
func (s *S) TestPoolLimitMany(c *C) {

session.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,13 @@ const defaultPrefetch = 0.25
186186
//
187187
// gssapiServiceName=<name>
188188
//
189-
// Defines the service name to use when authenticating with the GSSAPI
190-
// mechanism. Defaults to "mongodb".
189+
// Defines the service name to use when authenticating with the GSSAPI
190+
// mechanism. Defaults to "mongodb".
191+
//
192+
// maxPoolSize=<limit>
193+
//
194+
// Defines the per-server socket pool limit. Defaults to 4096.
195+
// See Session.SetPoolLimit for details.
191196
//
192197
//
193198
// Relevant documentation:
@@ -218,6 +223,7 @@ func DialWithTimeout(url string, timeout time.Duration) (*Session, error) {
218223
mechanism := ""
219224
service := ""
220225
source := ""
226+
poolLimit := 0
221227
for k, v := range uinfo.options {
222228
switch k {
223229
case "authSource":
@@ -226,6 +232,11 @@ func DialWithTimeout(url string, timeout time.Duration) (*Session, error) {
226232
mechanism = v
227233
case "gssapiServiceName":
228234
service = v
235+
case "maxPoolSize":
236+
poolLimit, err = strconv.Atoi(v)
237+
if err != nil {
238+
return nil, errors.New("bad value for maxPoolSize: " + v)
239+
}
229240
case "connect":
230241
if v == "direct" {
231242
direct = true
@@ -249,6 +260,7 @@ func DialWithTimeout(url string, timeout time.Duration) (*Session, error) {
249260
Mechanism: mechanism,
250261
Service: service,
251262
Source: source,
263+
PoolLimit: poolLimit,
252264
}
253265
return DialWithInfo(&info)
254266
}
@@ -300,6 +312,10 @@ type DialInfo struct {
300312
Username string
301313
Password string
302314

315+
// PoolLimit defines the per-server socket pool limit. Defaults to 4096.
316+
// See Session.SetPoolLimit for details.
317+
PoolLimit int
318+
303319
// DialServer optionally specifies the dial function for establishing
304320
// connections with the MongoDB servers.
305321
DialServer func(addr *ServerAddr) (net.Conn, error)
@@ -363,6 +379,9 @@ func DialWithInfo(info *DialInfo) (*Session, error) {
363379
}
364380
session.creds = []Credential{*session.dialCred}
365381
}
382+
if info.PoolLimit > 0 {
383+
session.poolLimit = info.PoolLimit
384+
}
366385
cluster.Release()
367386

368387
// People get confused when we return a session that is not actually

0 commit comments

Comments
 (0)