Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make the releaser simpler (rfc) #1

Open
wants to merge 2 commits into
base: v2-unstable
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 42 additions & 38 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ var defaultServerInfo mongoServerInfo

func newServer(addr string, tcpaddr *net.TCPAddr, sync chan bool, dial dialer, minPoolSize int, maxIdleTimeMS int) *mongoServer {
server := &mongoServer{
Addr: addr,
ResolvedAddr: tcpaddr.String(),
tcpaddr: tcpaddr,
sync: sync,
dial: dial,
info: &defaultServerInfo,
pingValue: time.Hour, // Push it back before an actual ping.
minPoolSize: minPoolSize,
Addr: addr,
ResolvedAddr: tcpaddr.String(),
tcpaddr: tcpaddr,
sync: sync,
dial: dial,
info: &defaultServerInfo,
pingValue: time.Hour, // Push it back before an actual ping.
minPoolSize: minPoolSize,
maxIdleTimeMS: maxIdleTimeMS,
}
go server.pinger(true)
Expand Down Expand Up @@ -217,8 +217,8 @@ func (server *mongoServer) RecycleSocket(socket *mongoSocket) {
if !server.closed {
now := time.Now()
server.unusedSockets = append(server.unusedSockets, &timedMongoSocket{
lastTimeUsed:&now,
soc:socket,
lastTimeUsed: &now,
soc: socket,
})
}
server.Unlock()
Expand Down Expand Up @@ -358,44 +358,48 @@ func (server *mongoServer) pinger(loop bool) {
}

func (server *mongoServer) releaser() {
for {

time.Sleep(1 * time.Minute)
ticker := time.NewTicker(1 * time.Minute)
for _ = range ticker.C {
if server.closed {
ticker.Stop()
return
}
server.RLock()
if len(server.unusedSockets) < server.minPoolSize {
server.RUnlock()
server.Lock()
unused := len(server.unusedSockets)
if unused < server.minPoolSize {
server.Unlock()
continue
}
tmpSlice := make([]*timedMongoSocket, 0, len(server.unusedSockets) - server.minPoolSize)
for _, s := range server.unusedSockets {
if len(tmpSlice) == cap(tmpSlice) {
now := time.Now()
end := 0
reclaimMap := map[*mongoSocket]bool{}
// Because the acquirision and recycle are done at the tail of array,
// the head is always the oldest unused socket.
for _, s := range server.unusedSockets[:unused-server.minPoolSize] {
if s.lastTimeUsed.Add(time.Duration(server.maxIdleTimeMS) * time.Millisecond).After(now) {
break
}
if time.Since(*(s.lastTimeUsed)) > time.Duration(server.maxIdleTimeMS) * time.Millisecond {
tmpSlice = append(tmpSlice, s)
}
end++
reclaimMap[s.soc] = true
}
server.RUnlock()

if len(tmpSlice) > 0 {
server.Lock()
for _, s := range tmpSlice {
for i, unused := range server.unusedSockets {
if s.soc == unused.soc {
copy(server.unusedSockets[i:], server.unusedSockets[i+1:])
n := len(server.unusedSockets) - 1
server.unusedSockets[n] = nil
server.unusedSockets = server.unusedSockets[:n]
stats.conn(-1, server.info.Master)
s.soc.Close()
break
}
tbr := server.unusedSockets[:end]
if end > 0 {
next := make([]*timedMongoSocket, unused-end)
copy(next, server.unusedSockets[end:])
server.unusedSockets = next
remainSockets := []*mongoSocket{}
for _, s := range server.liveSockets {
if !reclaimMap[s] {
remainSockets = append(remainSockets, s)
}
}
server.Unlock()
server.liveSockets = remainSockets
stats.conn(-1*end, server.info.Master)
}
server.Unlock()

for _, s := range tbr {
s.soc.Close()
}
}
}
Expand Down