Class: Mongo::Session::SessionPool Private

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo/session/session_pool.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

A pool of server sessions.

Since:

  • 2.5.0

Instance Method Summary collapse

Constructor Details

#initialize(cluster) ⇒ SessionPool

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a SessionPool.

Examples:

SessionPool.new(cluster)

Parameters:

  • cluster (Mongo::Cluster)

    The cluster that will be associated with this session pool.

Since:

  • 2.5.0



34
35
36
37
38
# File 'lib/mongo/session/session_pool.rb', line 34

def initialize(cluster)
  @queue = []
  @mutex = Mutex.new
  @cluster = cluster
end

Instance Method Details

#checkin(session) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Checkin a server session to the pool.

Examples:

Checkin a session.

pool.checkin(session)

Parameters:

Raises:

  • (ArgumentError)

Since:

  • 2.5.0



79
80
81
82
83
84
85
86
# 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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check out a server session from the pool.

Examples:

Check out a session.

pool.checkout

Returns:

Since:

  • 2.5.0



60
61
62
63
64
65
66
67
68
69
# 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_sessionsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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



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

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