Skip to content

Commit

Permalink
implement 'maxIdleTimeMS' to release idle connections
Browse files Browse the repository at this point in the history
  • Loading branch information
JodeZer committed Apr 14, 2017
1 parent 8f11f72 commit 6f7ed6c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
3 changes: 2 additions & 1 deletion cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type mongoCluster struct {
sync chan bool
dial dialer
minPoolSize int
maxIdleTimeMS int
}

func newCluster(userSeeds []string, direct, failFast bool, dial dialer, setName string) *mongoCluster {
Expand Down Expand Up @@ -407,7 +408,7 @@ func (cluster *mongoCluster) server(addr string, tcpaddr *net.TCPAddr) *mongoSer
if server != nil {
return server
}
return newServer(addr, tcpaddr, cluster.sync, cluster.dial, cluster.minPoolSize)
return newServer(addr, tcpaddr, cluster.sync, cluster.dial, cluster.minPoolSize, cluster.maxIdleTimeMS)
}

func resolveAddr(addr string) (*net.TCPAddr, error) {
Expand Down
12 changes: 8 additions & 4 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type mongoServer struct {
pingWindow [6]time.Duration
info *mongoServerInfo
minPoolSize int
maxIdleTimeMS int
}

type dialer struct {
Expand All @@ -77,7 +78,7 @@ type mongoServerInfo struct {

var defaultServerInfo mongoServerInfo

func newServer(addr string, tcpaddr *net.TCPAddr, sync chan bool, dial dialer, minPoolSize int) *mongoServer {
func newServer(addr string, tcpaddr *net.TCPAddr, sync chan bool, dial dialer, minPoolSize int, maxIdleTimeMS int) *mongoServer {
server := &mongoServer{
Addr: addr,
ResolvedAddr: tcpaddr.String(),
Expand All @@ -87,9 +88,12 @@ func newServer(addr string, tcpaddr *net.TCPAddr, sync chan bool, dial dialer, m
info: &defaultServerInfo,
pingValue: time.Hour, // Push it back before an actual ping.
minPoolSize: minPoolSize,
maxIdleTimeMS: maxIdleTimeMS,
}
go server.pinger(true)
go server.releaser()
if maxIdleTimeMS != 0 {
go server.releaser()
}
return server
}

Expand Down Expand Up @@ -370,7 +374,7 @@ func (server *mongoServer) releaser() {
if len(tmpSlice) == cap(tmpSlice) {
break
}
if time.Since(*(s.lastTimeUsed)) > 5 * time.Minute {
if time.Since(*(s.lastTimeUsed)) > time.Duration(server.maxIdleTimeMS) * time.Millisecond {
tmpSlice = append(tmpSlice, s)
}
}
Expand All @@ -385,11 +389,11 @@ func (server *mongoServer) releaser() {
n := len(server.unusedSockets) - 1
server.unusedSockets[n] = nil
server.unusedSockets = server.unusedSockets[:n]
stats.conn(-1, server.info.Master)
s.soc.Close()
break
}
}
s.soc.Close()
}
server.Unlock()
}
Expand Down
17 changes: 16 additions & 1 deletion session.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ func ParseURL(url string) (*DialInfo, error) {
setName := ""
poolLimit := 0
minPoolSize := 0
maxIdleTimeMS := 0
for k, v := range uinfo.options {
switch k {
case "authSource":
Expand All @@ -297,10 +298,15 @@ func ParseURL(url string) (*DialInfo, error) {
return nil, errors.New("bad value for maxPoolSize: " + v)
}
case "minPoolSize":
minPoolSize ,err = strconv.Atoi(v)
minPoolSize, err = strconv.Atoi(v)
if err != nil {
return nil, errors.New("bad value for minPoolSize: " + v)
}
case "maxIdleTimeMS":
maxIdleTimeMS, err = strconv.Atoi(v)
if err != nil {
return nil, errors.New("bad value for maxIdleTimeMS: " + v)
}
case "connect":
if v == "direct" {
direct = true
Expand All @@ -326,6 +332,7 @@ func ParseURL(url string) (*DialInfo, error) {
PoolLimit: poolLimit,
ReplicaSetName: setName,
MinPoolSize: minPoolSize,
maxIdleTimeMS: maxIdleTimeMS,
}
return &info, nil
}
Expand Down Expand Up @@ -396,6 +403,10 @@ type DialInfo struct {
// Defaults to 0.
MinPoolSize int

//The maximum number of milliseconds that a connection can remain idle in the pool
// before being removed and closed.
maxIdleTimeMS int

// DialServer optionally specifies the dial function for establishing
// connections with the MongoDB servers.
DialServer func(addr *ServerAddr) (net.Conn, error)
Expand Down Expand Up @@ -471,6 +482,10 @@ func DialWithInfo(info *DialInfo) (*Session, error) {
cluster.minPoolSize = info.MinPoolSize
}

if info.maxIdleTimeMS > 0 {
cluster.maxIdleTimeMS = info.maxIdleTimeMS
}

cluster.Release()

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

0 comments on commit 6f7ed6c

Please sign in to comment.