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)

Since:

  • 2.5.0

[ GitHub ]

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

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

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

  logical_session_timeout = @cluster.logical_session_timeout

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

#checkin(session)

Checkin a server session to the pool.

Examples:

Checkin a session.

pool.checkin(session)

Parameters:

Since:

  • 2.5.0

[ GitHub ]

  
# 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

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 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_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 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

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 51

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 153

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?

  while !@queue.empty?
    if about_to_expire?(@queue[-1])
      @queue.pop
    else
      break
    end
  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 129

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