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

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



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

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



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

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



98
99
100
101
102
103
104
105
# File 'lib/mongo/session/session_pool.rb', line 98

def checkin(session)
  @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.

Checkout a server session from the pool.

Examples:

Checkout a session.

pool.checkout

Returns:

Since:

  • 2.5.0



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/mongo/session/session_pool.rb', line 75

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



113
114
115
116
117
118
119
120
121
# File 'lib/mongo/session/session_pool.rb', line 113

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



63
64
65
# File 'lib/mongo/session/session_pool.rb', line 63

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