Class: Mongo::Session::ServerSession Private

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo/session/server_session.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.

An object representing the server-side session.

Since:

  • 2.5.0

Constant Summary collapse

DASH_REGEX =

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

Regex for removing dashes from the UUID string.

Since:

  • 2.5.0

/\-/.freeze
UUID_PACK =

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

Pack directive for the UUID.

Since:

  • 2.5.0

'H*'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeServerSession

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 ServerSession.

Examples:

ServerSession.new

Since:

  • 2.5.0



69
70
71
72
73
# File 'lib/mongo/session/server_session.rb', line 69

def initialize
  set_last_use!
  session_id
  @txn_num = 0
end

Instance Attribute Details

#last_useObject (readonly)

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.

The last time the server session was used.

Since:

  • 2.5.0



42
43
44
# File 'lib/mongo/session/server_session.rb', line 42

def last_use
  @last_use
end

#txn_numObject (readonly)

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.

The current transaction number.

When a transaction is active, all operations in that transaction use the same transaction number. If the entire transaction is restarted (for example, by Session#with_transaction, in which case it would also invoke the block provided to it again), each transaction attempt has its own transaction number.

Transaction number is also used outside of transactions for retryable writes. In this case, each write operation has its own transaction number, but retries of a write operation use the same transaction number as the first write (which is how the server knows that subsequent writes are retries and should be ignored if the first write succeeded on the server but was not read by the client, for example).

Since:

  • 2.5.0



61
62
63
# File 'lib/mongo/session/server_session.rb', line 61

def txn_num
  @txn_num
end

Instance Method Details

#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 object.

session.inspect

Returns:

  • (String)

    The session inspection.

Since:

  • 2.5.0



117
118
119
# File 'lib/mongo/session/server_session.rb', line 117

def inspect
  "#<Mongo::Session::ServerSession:0x#{object_id} session_id=#{session_id} last_use=#{@last_use}>"
end

#next_txn_numInteger

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.

Increment the current transaction number and return the new value.

Returns:

  • (Integer)

    The updated transaction number.

Since:

  • 2.5.0



105
106
107
# File 'lib/mongo/session/server_session.rb', line 105

def next_txn_num
  @txn_num += 1
end

#session_idBSON::Document

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.

The session id of this server session.

Examples:

Get the session id.

server_session.session_id

Returns:

  • (BSON::Document)

    The session id.

Since:

  • 2.5.0



95
96
97
98
# File 'lib/mongo/session/server_session.rb', line 95

def session_id
  @session_id ||= (bytes = [SecureRandom.uuid.gsub(DASH_REGEX, '')].pack(UUID_PACK)
                    BSON::Document.new(id: BSON::Binary.new(bytes, :uuid)))
end

#set_last_use!Time

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.

Update the last_use attribute of the server session to now.

Examples:

Set the last use field to now.

server_session.set_last_use!

Returns:

  • (Time)

    The last time the session was used.

Since:

  • 2.5.0



83
84
85
# File 'lib/mongo/session/server_session.rb', line 83

def set_last_use!
  @last_use = Time.now
end