1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package session
import (
"sync"
"go.mongodb.org/mongo-driver/mongo/description"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
)
// Node represents a server session in a linked list
type Node struct {
*Server
next *Node
prev *Node
}
// topologyDescription is used to track a subset of the fields present in a description.Topology instance that are
// relevant for determining session expiration.
type topologyDescription struct {
kind description.TopologyKind
timeoutMinutes uint32
}
// Pool is a pool of server sessions that can be reused.
type Pool struct {
descChan <-chan description.Topology
head *Node
tail *Node
latestTopology topologyDescription
mutex sync.Mutex // mutex to protect list and sessionTimeout
checkedOut int // number of sessions checked out of pool
}
func (p *Pool) createServerSession() (*Server, error) {
s, err := newServerSession()
if err != nil {
return nil, err
}
p.checkedOut++
return s, nil
}
// NewPool creates a new server session pool
func NewPool(descChan <-chan description.Topology) *Pool {
p := &Pool{
descChan: descChan,
}
return p
}
// assumes caller has mutex to protect the pool
func (p *Pool) updateTimeout() {
select {
case newDesc := <-p.descChan:
p.latestTopology = topologyDescription{
kind: newDesc.Kind,
timeoutMinutes: newDesc.SessionTimeoutMinutes,
}
default:
// no new description waiting
}
}
// GetSession retrieves an unexpired session from the pool.
func (p *Pool) GetSession() (*Server, error) {
p.mutex.Lock() // prevent changing the linked list while seeing if sessions have expired
defer p.mutex.Unlock()
// empty pool
if p.head == nil && p.tail == nil {
return p.createServerSession()
}
p.updateTimeout()
for p.head != nil {
// pull session from head of queue and return if it is valid for at least 1 more minute
if p.head.expired(p.latestTopology) {
p.head = p.head.next
continue
}
// found unexpired session
session := p.head.Server
if p.head.next != nil {
p.head.next.prev = nil
}
if p.tail == p.head {
p.tail = nil
p.head = nil
} else {
p.head = p.head.next
}
p.checkedOut++
return session, nil
}
// no valid session found
p.tail = nil // empty list
return p.createServerSession()
}
// 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.latestTopology) {
if p.tail.prev != nil {
p.tail.prev.next = nil
}
p.tail = p.tail.prev
}
// session expired
if ss.expired(p.latestTopology) {
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
}
// IDSlice returns a slice of session IDs for each session in the pool
func (p *Pool) IDSlice() []bsoncore.Document {
p.mutex.Lock()
defer p.mutex.Unlock()
var ids []bsoncore.Document
for node := p.head; node != nil; node = node.next {
ids = append(ids, node.SessionID)
}
return ids
}
// String implements the Stringer interface
func (p *Pool) String() string {
p.mutex.Lock()
defer p.mutex.Unlock()
s := ""
for head := p.head; head != nil; head = head.next {
s += head.SessionID.String() + "\n"
}
return s
}
// CheckedOut returns number of sessions checked out from pool.
func (p *Pool) CheckedOut() int {
return p.checkedOut
}
|