123456789_123456789_123456789_123456789_123456789_

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.

Since:

  • 2.5.0

Class Method Summary

Instance Method Summary

Instance Method Details

#about_to_expire?(session) ⇒ Boolean (private)

Raises:

  • (ArgumentError)

Since:

  • 2.5.0

[ GitHub ]

  
# File 'lib/mongo/session/session_pool.rb', line 124

def about_to_expire?(session)
  raise ArgumentError, 'session cannot be nil' if session.nil?

  # Load balancers spec explicitly requires to ignore the logical session
  # timeout value.
  # No rationale is provided as of the time of this writing.
  return false if @cluster.load_balanced?

  logical_session_timeout = @cluster.logical_session_timeout

  return unless logical_session_timeout

  idle_time_minutes = (Time.now - session.last_use) / 60
  (idle_time_minutes + 1) >= logical_session_timeout
end

#checkin(session)

Checkin a server session to the pool.

Examples:

Checkin a session.

pool.checkin(session)

Parameters:

Raises:

  • (ArgumentError)

Since:

  • 2.5.0

[ GitHub ]

  
# File 'lib/mongo/session/session_pool.rb', line 79

def checkin(session)
  raise ArgumentError, 'session cannot be nil' if session.nil?

  @mutex.synchronize do
    prune!
    @queue.unshift(session) if return_to_queue?(session)
  end
end

#checkoutServerSession

Check out a server session from the pool.

Examples:

Check out a session.

pool.checkout

Returns:

Since:

  • 2.5.0

[ GitHub ]

  
# File 'lib/mongo/session/session_pool.rb', line 60

def checkout
  @mutex.synchronize do
    loop do
      return ServerSession.new if @queue.empty?

      session = @queue.shift
      return session unless about_to_expire?(session)
    end
  end
end

#end_sessions

End all sessions in the pool by sending the endSessions command to the server.

Examples:

End all sessions.

pool.end_sessions

Since:

  • 2.5.0

[ GitHub ]

  
# File 'lib/mongo/session/session_pool.rb', line 94

def end_sessions
  until @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.options[:server_api],
                                     })
    op.execute(server, context: context)
  end
rescue Mongo::Error, Error::AuthError
end

#inspectString

Get a formatted string for use in inspection.

Examples:

Inspect the session pool object.

session_pool.inspect

Returns:

  • (String)

    The session pool inspection.

Since:

  • 2.5.0

[ GitHub ]

  
# File 'lib/mongo/session/session_pool.rb', line 48

def inspect
  "#<Mongo::Session::SessionPool:0x#{object_id} current_size=#{@queue.size}>"
end

#prune! (private)

Since:

  • 2.5.0

[ GitHub ]

  
# File 'lib/mongo/session/session_pool.rb', line 140

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?

  until @queue.empty?
    break unless about_to_expire?(@queue[-1])

    @queue.pop

  end
end

#return_to_queue?(session) ⇒ true | false (private)

Query whether the given session is okay to return to the pool’s queue.

Parameters:

Returns:

  • (true | false)

    whether to return the session to the queue.

Since:

  • 2.5.0

[ GitHub ]

  
# File 'lib/mongo/session/session_pool.rb', line 120

def return_to_queue?(session)
  !session.dirty? && !about_to_expire?(session)
end