源码 链表 连接池


vendor/go.mongodb.org/mongo-driver/x/mongo/driver/session/session_pool.go:17


// Node represents a server session in a linked list
type Node struct {
*Server
next *Node
prev *Node
}

// Pool is a pool of server sessions that can be reused.
type Pool struct {
descChan <-chan description.Topology
head *Node
tail *Node
timeout uint32
mutex sync.Mutex // mutex to protect list and sessionTimeout

checkedOut int // number of sessions checked out of pool
}



vendor/go.mongodb.org/mongo-driver/x/mongo/driver/session/session_pool.go:103



// ReturnSession returns a session to the pool if it has not expired.
func (p *Pool) ReturnSession(ss *Server) {
if ss == nil {
return
}

p.mutex.Lock()
defer p.mutex.Unlock()

p.checkedOut--
p.updateTimeout()
// check sessions at end of queue for expired
// stop checking after hitting the first valid session
for p.tail != nil && p.tail.expired(p.timeout) {
if p.tail.prev != nil {
p.tail.prev.next = nil
}
p.tail = p.tail.prev
}

// session expired
if ss.expired(p.timeout) {
return
}

// session is dirty
if ss.Dirty {
return
}

newNode := &Node{
Server: ss,
next: nil,
prev: nil,
}

// empty list
if p.tail == nil {
p.head = newNode
p.tail = newNode
return
}

// at least 1 valid session in list
newNode.next = p.head
p.head.prev = newNode
p.head = newNode
}
















posted @ 2020-11-05 10:44  papering  阅读(117)  评论(0编辑  收藏  举报