/usr/share/perl5/MongoDBx/Class/ConnectionPool.pm is in libmongodbx-class-perl 1.02-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | package MongoDBx::Class::ConnectionPool;
# ABSTARCT: A simple connection pool for MongoDBx::Class
our $VERSION = "1.02";
$VERSION = eval $VERSION;
use Moose::Role;
use namespace::autoclean;
use Carp;
=head1 NAME
MongoDBx::Class::ConnectionPool - A simple connection pool for MongoDBx::Class
=head1 VERSION
version 1.02
=head1 SYNOPSIS
# create a MongoDBx::Class object normally:
use MongoDBx::Class;
my $dbx = MongoDBx::Class->new(namespace => 'MyApp::Model::DB');
# instead of connection, create a rotated pool
my $pool = $dbx->pool(max_conns => 200, type => 'rotated'); # max_conns defaults to 100
# or, if you need to pass attributes to MongoDB::Connection->new():
my $pool = $dbx->pool(max_conns => 200, type => 'rotated', params => {
host => $host,
username => $username,
password => $password,
...
});
# get a connection from the pool on a per-request basis
my $conn = $pool->get_conn;
# ... do stuff with $conn and return it when done ...
$pool->return_conn($conn); # only relevant on backup pools but a good practice anyway
=head1 DESCRIPTION
WARNING: connection pooling via MongoDBx::Class is experimental. It is a
quick, simple implementation that may or may not work as expected.
MongoDBx::Class::ConnectionPool is a very simple interface for creating
MongoDB connection pools. The basic idea is: create a pool with a maximum
number of connections as a setting. Give connections from the pool on a per-request
basis. The pool is empty at first, and connections are created for each
request, until the maximum is reached. The behaviour of the pool when this
maximum is reached is dependent on the implementation. There are currently
two implementations:
=over
=item * Rotated pools (L<MongoDBx::Class::ConnectionPool::Rotated>) - these
pools hold at most the number of maximum connections defined. An index is
held, initially starting at zero. When a request for a connection is made,
the connection located at the current index is returned (if exists, otherwise
a new one is created), and the index is incremented. When the index reaches the
end of the pool, it returns to the beginning (i.e. zero), and the next
request will receive the first connection in the pool, and so on. This means
that every connection in the pool can be shared by an unlimited number of
requesters.
=item * Backup pools (L<MongoDBx::Class::ConnectionPool::Backup>) - these
pools expect the receiver of a connection to return it when they're done
using it. If no connections are available when a request is made (i.e.
all connections are being used), a backup connection is returned (there
can be only one backup connection). This means that every connection in
the pool can be used by one requester, except for the backup connection
which can be shared.
=back
The rotated pool makes more sense for pools with a relatively low number
of connections, while the backup pool is more fit for a larger number of
connections. The selection should be based, among other factors, on your
application's metrics: how many end-users (e.g. website visitors) use your
application concurrently? does your application experience larger loads
and usage numbers at certain points of the day/week? does it make more
sense for you to balance work between a predefined number of connections
(rotated pool) or do you prefer each end-user to get their own connection
(backup pool)?
At any rate, every end-user will receive a connection, shared or not.
=head1 ATTRIBUTES
=head2 max_conns
An integer defining the maximum number of connections a pool can hold.
Defaults to 100.
=head2 pool
An array-reference of L<MongoDBx::Class::Connection> objects, this is the
actual pool. Mostly used and populated internally.
=head2 num_used
For backup pools, this will be an integer indicating the number of connections
from the pool currently being used. For rotated pools, this will be the index
of the connection to be given to the next end-user.
=head2 params
A hash-ref of parameters to pass to C<< MongoDB::Connection->new() >> when
creating a new connection. See L<MongoDB::Connection/"ATTRIBUTES"> for
more information.
=cut
has 'max_conns' => (
is => 'ro',
isa => 'Int',
default => 100,
);
has 'pool' => (
is => 'ro',
isa => 'ArrayRef[MongoDBx::Class::Connection]',
writer => '_set_pool',
default => sub { [] },
);
has 'num_used' => (
is => 'ro',
isa => 'Int',
writer => '_set_used',
default => 0,
);
has 'params' => (
is => 'ro',
isa => 'HashRef',
required => 1,
);
=head1 REQUIRED METHODS
This L<Moose role|Moose::Role> requires consuming classes to implement
the following methods:
=over
=item * get_conn()
Returns a connection from the pool to a requester, possibly creating a
new one in the process if no connections are available and the maximum
has not been reached yet.
=item * return_conn( $conn )
Returns a connection (receievd via C<get_conn()>) to the pool, meant to
be called by the end-user after being done with the connection. Only relevant
when C<get_conn()> actually takes the connection out of the pool (so it
is not shared), like with backup pools. Otherwise this method may do nothing.
=back
=cut
requires 'get_conn';
requires 'return_conn';
=head1 PROVIDED METHODS
Meant to be used by consuming classes:
=head2 _get_new_conn()
Creates a new L<MongoDBx::Class::Connection> object, increments the C<num_used>
attribute, and returns the new connection. Should be used by C<get_conn()>.
=cut
sub _get_new_conn {
my $self = shift;
my $conn = MongoDBx::Class::Connection->new(%{$self->params});
$self->_inc_used;
return $conn;
}
=head2 _inc_used( [ $int ] )
Increases the C<num_used> attribute by C<$int> (which can be negative),
or by 1 if C<$int> is not supplied.
=cut
sub _inc_used {
my ($self, $int) = @_;
$int ||= 1;
$self->_set_used($self->num_used + $int);
}
=head2 _add_to_pool( $conn )
Adds a connection object to the end of the pool (the C<pool> attribute).
=cut
sub _add_to_pool {
my ($self, $conn) = @_;
my $pool = $self->pool;
push(@$pool, $conn);
$self->_set_pool($pool);
}
=head1 AUTHOR
Ido Perlmuter, C<< <ido at ido50.net> >>
=head1 BUGS
Please report any bugs or feature requests to C<bug-mongodbx-class at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MongoDBx-Class>. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc MongoDBx::Class::ConnectionPool
You can also look for information at:
=over 4
=item * RT: CPAN's request tracker
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=MongoDBx::Class>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/MongoDBx::Class>
=item * CPAN Ratings
L<http://cpanratings.perl.org/d/MongoDBx::Class>
=item * Search CPAN
L<http://search.cpan.org/dist/MongoDBx::Class/>
=back
=head1 SEE ALSO
L<MongoDBx::Class>, L<MongoDB::Connection>.
=head1 LICENSE AND COPYRIGHT
Copyright 2010-2012 Ido Perlmuter.
This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
=cut
1;
|