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

API:

  • private

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:

  • The cluster that will be associated with this session pool.

Since:

  • 2.5.0

API:

  • private



37
38
39
40
41
# File 'lib/mongo/session/session_pool.rb', line 37

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:

  • The session to checkin.

Since:

  • 2.5.0

API:

  • private



86
87
88
89
90
91
92
93
94
95
# 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

#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:

  • The server session.

Since:

  • 2.5.0

API:

  • private



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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_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

API:

  • private



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# 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.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:

  • The session pool inspection.

Since:

  • 2.5.0

API:

  • private



51
52
53
# File 'lib/mongo/session/session_pool.rb', line 51

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