Class: Mongo::Session::SessionPool Private

Inherits:
Object
  • Object
show all
Defined in:
build/ruby-driver-v2.19/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

Class Method Summary collapse

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



52
53
54
55
56
# File 'build/ruby-driver-v2.19/lib/mongo/session/session_pool.rb', line 52

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

Class Method Details

.create(cluster) ⇒ 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.

Create a SessionPool.

Examples:

SessionPool.create(cluster)

Parameters:

  • cluster (Mongo::Cluster)

    The cluster that will be associated with this session pool.

Since:

  • 2.5.0



38
39
40
41
# File 'build/ruby-driver-v2.19/lib/mongo/session/session_pool.rb', line 38

def self.create(cluster)
  pool = new(cluster)
  cluster.instance_variable_set(:@session_pool, pool)
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:

Since:

  • 2.5.0



101
102
103
104
105
106
107
108
109
110
111
112
# File 'build/ruby-driver-v2.19/lib/mongo/session/session_pool.rb', line 101

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

  @mutex.synchronize do
    prune!
    unless about_to_expire?(session)
      @queue.unshift(session)
    end
  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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'build/ruby-driver-v2.19/lib/mongo/session/session_pool.rb', line 78

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



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'build/ruby-driver-v2.19/lib/mongo/session/session_pool.rb', line 120

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:

  • (String)

    The session pool inspection.

Since:

  • 2.5.0



66
67
68
# File 'build/ruby-driver-v2.19/lib/mongo/session/session_pool.rb', line 66

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