Class: Mongo::Session::SessionPool Private
Do not use. This class is for internal use only.
Relationships & Source Files | |
Inherits: | Object |
Defined in: | lib/mongo/session/session_pool.rb |
Overview
A pool of server sessions.
Class Method Summary
-
.new(cluster) ⇒ SessionPool
constructor
Internal use only
Initialize a
SessionPool
.
Instance Method Summary
-
#checkin(session)
Internal use only
Checkin a server session to the pool.
-
#checkout ⇒ ServerSession
Internal use only
Check out a server session from the pool.
-
#end_sessions
Internal use only
End all sessions in the pool by sending the endSessions command to the server.
-
#inspect ⇒ String
Internal use only
Get a formatted string for use in inspection.
- #about_to_expire?(session) ⇒ Boolean private Internal use only
- #prune! private Internal use only
-
#return_to_queue?(session) ⇒ true | false
private
Internal use only
Query whether the given session is okay to return to the pool’s queue.
Instance Method Details
#about_to_expire?(session) ⇒ Boolean
(private)
# File 'lib/mongo/session/session_pool.rb', line 133
def about_to_expire?(session) if session.nil? raise ArgumentError, 'session cannot be nil' end # Load balancers spec explicitly requires to ignore the logical session # timeout value. # No rationale is provided as of the time of this writing. if @cluster.load_balanced? return false end logical_session_timeout = @cluster.logical_session_timeout if logical_session_timeout idle_time_minutes = (Time.now - session.last_use) / 60 (idle_time_minutes + 1) >= logical_session_timeout end end
#checkin(session)
Checkin a server session to the pool.
# File 'lib/mongo/session/session_pool.rb', line 86
def checkin(session) if session.nil? raise ArgumentError, 'session cannot be nil' end @mutex.synchronize do prune! @queue.unshift(session) if return_to_queue?(session) end end
#checkout ⇒ ServerSession
Check out a server session from the pool.
# File 'lib/mongo/session/session_pool.rb', line 63
def checkout @mutex.synchronize do loop do if @queue.empty? return ServerSession.new else session = @queue.shift unless about_to_expire?(session) return session end end end end end
#end_sessions
End all sessions in the pool by sending the endSessions command to the server.
# File 'lib/mongo/session/session_pool.rb', line 103
def end_sessions while !@queue.empty? server = ServerSelector.get(mode: :primary_preferred).select_server(@cluster) op = Operation::Command.new( selector: { endSessions: @queue.shift(10_000).map(&:session_id), }, db_name: Database::ADMIN, ) context = Operation::Context.new(options: { server_api: server. [:server_api], }) op.execute(server, context: context) end rescue Mongo::Error, Error::AuthError end
#inspect ⇒ String
Get a formatted string for use in inspection.
# File 'lib/mongo/session/session_pool.rb', line 51
def inspect "#<Mongo::Session::SessionPool:0x#{object_id} current_size=#{@queue.size}>" end
#prune! (private)
# File 'lib/mongo/session/session_pool.rb', line 153
def prune! # Load balancers spec explicitly requires not to prune sessions. # No rationale is provided as of the time of this writing. return if @cluster.load_balanced? while !@queue.empty? if about_to_expire?(@queue[-1]) @queue.pop else break end end end
#return_to_queue?(session) ⇒ true
| false
(private)
Query whether the given session is okay to return to the pool’s queue.
# File 'lib/mongo/session/session_pool.rb', line 129
def return_to_queue?(session) !session.dirty? && !about_to_expire?(session) end