class Mongo::Session::SessionPool

A pool of server sessions.

@api private

@since 2.5.0

Public Class Methods

create(client) click to toggle source

Create a SessionPool.

@example

SessionPool.create(client)

@param [ Mongo::Client ] client The client that will be associated with this

session pool.

@since 2.5.0

# File lib/mongo/session/session_pool.rb, line 35
def self.create(client)
  pool = new(client)
  client.instance_variable_set(:@session_pool, pool)
end
new(client) click to toggle source

Initialize a SessionPool.

@example

SessionPool.new(client)

@param [ Mongo::Client ] client The client that will be associated with this

session pool.

@since 2.5.0

# File lib/mongo/session/session_pool.rb, line 49
def initialize(client)
  @queue = []
  @mutex = Mutex.new
  @client = client
end

Public Instance Methods

checkin(session) click to toggle source

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 116
def checkin(session)
  @mutex.synchronize do
    prune!
    unless about_to_expire?(session)
      @queue.unshift(session)
    end
  end
end
checkout() click to toggle source

Checkout a server session from the pool.

@example Checkout a session.

pool.checkout

@return [ ServerSession ] The server session.

@since 2.5.0

# File lib/mongo/session/session_pool.rb, line 93
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() click to toggle source

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 131
def end_sessions
  while !@queue.empty?
    server = ServerSelector.get(mode: :primary_preferred).select_server(@client.cluster)
    Operation::Commands::Command.new(
        :selector => {endSessions: @queue.shift(10_000).collect { |s| s.session_id }},
        :db_name => Database::ADMIN).execute(server)
  end
rescue
end
inspect() click to toggle source

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
with_session() { |server_session| ... } click to toggle source

Checkout a session to be used in the context of a block and return the session back to

the pool after the block completes.

@example Checkout, use a session, and return it back to the pool after the block.

pool.with_session do |session|
  ...
end

@yieldparam [ ServerSession ] The server session.

@since 2.5.0

# File lib/mongo/session/session_pool.rb, line 78
def with_session
  server_session = checkout
  yield(server_session)
ensure
  begin; checkin(server_session) if server_session; rescue; end
end

Private Instance Methods

about_to_expire?(session) click to toggle source
# File lib/mongo/session/session_pool.rb, line 143
def about_to_expire?(session)
  if @client.logical_session_timeout
    idle_time_minutes = (Time.now - session.last_use) / 60
    (idle_time_minutes + 1) >= @client.logical_session_timeout
  end
end
prune!() click to toggle source
# File lib/mongo/session/session_pool.rb, line 150
def prune!
  while !@queue.empty?
    if about_to_expire?(@queue[-1])
      @queue.pop
    else
      break
    end
  end
end