class Mongo::Session::SessionPool
A pool of server sessions.
@api private
@since 2.5.0
Public Class Methods
Create a SessionPool
.
@example
SessionPool.create(cluster)
@param [ Mongo::Cluster
] cluster The cluster that will be associated with this
session pool.
@since 2.5.0
# File lib/mongo/session/session_pool.rb, line 35 def self.create(cluster) pool = new(cluster) cluster.instance_variable_set(:@session_pool, pool) end
Initialize a SessionPool
.
@example
SessionPool.new(cluster)
@param [ Mongo::Cluster
] cluster The cluster that will be associated with this
session pool.
@since 2.5.0
# File lib/mongo/session/session_pool.rb, line 49 def initialize(cluster) @queue = [] @mutex = Mutex.new @cluster = cluster end
Public Instance Methods
Checkin a server session to the pool.
@example Checkin a session.
pool.checkin(session)
@param [ Session::ServerSession
] session The session to checkin.
@since 2.5.0
# File lib/mongo/session/session_pool.rb, line 98 def checkin(session) @mutex.synchronize do prune! unless about_to_expire?(session) @queue.unshift(session) end end end
Check out a server session from the pool.
@example Check out a session.
pool.checkout
@return [ ServerSession
] The server session.
@since 2.5.0
# File lib/mongo/session/session_pool.rb, line 75 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 all sessions in the pool by sending the endSessions command to the server.
@example End all sessions.
pool.end_sessions
@since 2.5.0
# File lib/mongo/session/session_pool.rb, line 113 def end_sessions while !@queue.empty? server = ServerSelector.get(mode: :primary_preferred).select_server(@cluster) Operation::Command.new( :selector => {endSessions: @queue.shift(10_000).collect { |s| s.session_id }}, :db_name => Database::ADMIN).execute(server) end rescue Mongo::Error, Error::AuthError end
Get a formatted string for use in inspection.
@example Inspect the session pool object.
session_pool.inspect
@return [ String ] The session pool inspection.
@since 2.5.0
# File lib/mongo/session/session_pool.rb, line 63 def inspect "#<Mongo::Session::SessionPool:0x#{object_id} current_size=#{@queue.size}>" end
Private Instance Methods
# File lib/mongo/session/session_pool.rb, line 125 def about_to_expire?(session) 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
# File lib/mongo/session/session_pool.rb, line 134 def prune! while !@queue.empty? if about_to_expire?(@queue[-1]) @queue.pop else break end end end