File Coverage

blib/lib/MongoDB/_ServerSession.pm
Criterion Covered Total %
statement 33 45 73.3
branch 0 4 0.0
condition 0 3 0.0
subroutine 11 15 73.3
pod 0 2 0.0
total 44 69 63.7


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