1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
# Copyright 2018 - present MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
use strict;
use warnings;
package MongoDB::_ServerSession;
# MongoDB Server Session object
use version;
our $VERSION = 'v2.2.2';
use MongoDB::Error;
use Moo;
use UUID::URandom;
use Math::BigInt;
use MongoDB::_Types qw(
Document
);
use Types::Standard qw(
Maybe
InstanceOf
Int
Bool
);
use constant UUID_TYPE => 4;
use namespace::clean -except => 'meta';
# session_id
#
# $server_session->session_id;
#
# Returns the session id for this server session as a L<BSON::Bytes> object
# containing a binary UUID V4. For lower network usage, if not provided on
# initialisation this class will generate a new UUID instead of consulting the
# server for a new session id.
has session_id => (
is => 'lazy',
isa => Document,
builder => '_build_session_id',
);
sub _build_session_id {
my ( $self ) = @_;
my $uuid = BSON::Bytes->new(
data => UUID::URandom::create_uuid(),
subtype => UUID_TYPE,
);
return { id => $uuid };
}
# last_use
#
# $server_session->last_use;
#
# Returns the unix time that this server session was last used. Used for checking
# expiry of a server session. If undefined, then the session has (probably) not
# been used on the server.
has last_use => (
is => 'rwp',
init_arg => undef,
isa => Maybe[Int],
);
# transaction_id
#
# $server_session->transaction_id
#
# Returns the current transaction id for this server session. This is a ratcheted
# incrementing ID number, which when combined with the session id allows for
# retrying transactions in the correct order.
has transaction_id => (
is => 'rwp',
init_arg => undef,
default => sub { Math::BigInt->new('0') },
);
# pool_epoch
#
# tracks which pool the session came from; sessions won't be checked into
# a newer pool
has pool_epoch => (
is => 'ro',
default => -1,
);
#
# Mark this session as dirty.
# A server session is marked dirty when a command fails with a network
# error. Dirty sessions are later discarded from the server session pool.
#
has dirty => (
is => 'rwp',
isa => Bool,
default => 0,
);
sub mark_dirty {
my $self = shift;
$self->_set_dirty(1);
}
# update_last_use
#
# $server_session->update_last_use;
#
# Updates the value of L</last_use> to the current unix time.
sub update_last_use {
my ( $self ) = @_;
$self->_set_last_use( time() );
}
sub _is_expiring {
my ( $self, $session_timeout ) = @_;
# if session_timeout is undef, then sessions arent actually supported (this
# value should be from logical_session_timeout_minutes).
return 1 unless defined $session_timeout;
my $timeout = time() - ( ( $session_timeout - 1 ) * 60 );
# Undefined last_use means its never actually been used on the server
return 1 if defined $self->last_use && $self->last_use < $timeout;
return;
}
1;
# vim: set ts=4 sts=4 sw=4 et tw=75:
|