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 59     59   350 use strict;
  59         117  
  59         1488  
16 59     59   261 use warnings;
  59         111  
  59         1629  
17             package MongoDB::_ServerSession;
18              
19             # MongoDB Server Session object
20              
21 59     59   289 use version;
  59         106  
  59         286  
22             our $VERSION = 'v2.2.2';
23              
24 59     59   4018 use MongoDB::Error;
  59         155  
  59         6372  
25              
26 59     59   355 use Moo;
  59         132  
  59         301  
27 59     59   37886 use UUID::URandom;
  59         19843  
  59         2135  
28 59     59   374 use Math::BigInt;
  59         113  
  59         605  
29 59         472 use MongoDB::_Types qw(
30             Document
31 59     59   17850 );
  59         135  
32 59         376 use Types::Standard qw(
33             Maybe
34             InstanceOf
35             Int
36             Bool
37 59     59   54306 );
  59         123  
38 59     59   54419 use constant UUID_TYPE => 4;
  59         121  
  59         4023  
39              
40 59     59   340 use namespace::clean -except => 'meta';
  59         115  
  59         429  
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: