| 1 | package Mail::MtPolicyd::ConnectionPool; | ||||||||||
| 2 | |||||||||||
| 3 | 8 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 8 | ▶ | use MooseX::Singleton; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 8 | our $VERSION = '2.01'; # VERSION | |||||||||
| 7 | # ABSTRACT: a singleton to hold all configure connections | ||||||||||
| 8 | |||||||||||
| 9 | has 'pool' => ( | ||||||||||
| 10 | is => 'ro', | ||||||||||
| 11 | isa => 'HashRef[Mail::MtPolicyd::Connection]', | ||||||||||
| 12 | lazy => 1, | ||||||||||
| 13 | 5 | ▶ | default => sub { {} }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 8 | traits => [ 'Hash' ], | |||||||||
| 15 | handles => { | ||||||||||
| 16 | 'get_connection' => 'get', | ||||||||||
| 17 | 'add_connection' => 'set', | ||||||||||
| 18 | } | ||||||||||
| 19 | ); | ||||||||||
| 20 | |||||||||||
| 21 | sub get_handle { | ||||||||||
| 22 | 27 | ▶ | my ( $self, $name ) = @_; | ||||||||
|
Subroutine
get_handle: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||
| 23 | 27 | ▶ | if( defined $self->pool->{$name} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 27 | return $self->pool->{$name}->handle; | |||||||||
| 25 | } | ||||||||||
| 26 | 0 | return; | |||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | 8 | has 'plugin_prefix' => ( is => 'ro', isa => 'Str', | |||||||||
| 30 | default => 'Mail::MtPolicyd::Connection::'); | ||||||||||
| 31 | |||||||||||
| 32 | sub load_config { | ||||||||||
| 33 | 0 | ▶ | my ( $self, $config ) = @_; | ||||||||
|
Subroutine
load_config: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 34 | 0 | foreach my $name ( keys %$config ) { | |||||||||
| 35 | 0 | $self->load_connection( $name, $config->{$name} ); | |||||||||
| 36 | } | ||||||||||
| 37 | 0 | return; | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | sub load_connection { | ||||||||||
| 41 | 5 | ▶ | my ( $self, $name, $params ) = @_; | ||||||||
|
Subroutine
load_connection: called CC=4 SCAR=14.5
Pod
undocumented
| |||||||||||
| 42 | 5 | ▶ | if( ! defined $params->{'module'} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 0 | die('no module defined for connection '.$name.'!'); | |||||||||
| 44 | } | ||||||||||
| 45 | 5 | my $module = $params->{'module'}; | |||||||||
| 46 | 5 | my $class = $self->plugin_prefix.$module; | |||||||||
| 47 | 5 | my $conn; | |||||||||
| 48 | |||||||||||
| 49 | 5 | my $code = "require ".$class.";"; | |||||||||
| 50 | 5 | eval $code; ## no critic (ProhibitStringyEval) | |||||||||
| 51 | 5 | ▶ | if($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 0 | die('could not load connection '.$name.': '.$@); | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 5 | eval { | |||||||||
| 56 | 5 | $conn = $class->new( | |||||||||
| 57 | name => $name, | ||||||||||
| 58 | %$params, | ||||||||||
| 59 | ); | ||||||||||
| 60 | 5 | $conn->init(); | |||||||||
| 61 | }; | ||||||||||
| 62 | 5 | ▶ | if($@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 0 | die('could not initialize connection '.$name.': '.$@); | |||||||||
| 64 | } | ||||||||||
| 65 | 5 | $self->add_connection( $name => $conn ); | |||||||||
| 66 | 5 | return; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | sub shutdown { | ||||||||||
| 70 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
shutdown: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 71 | |||||||||||
| 72 | 0 | foreach my $conn ( values %{$self->pool} ) { | |||||||||
| 73 | 0 | $conn->shutdown(@_); # cascade | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | 0 | return; | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | sub reconnect { | ||||||||||
| 80 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
reconnect: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 81 | |||||||||||
| 82 | 0 | foreach my $conn ( values %{$self->pool} ) { | |||||||||
| 83 | 0 | $conn->reconnect(@_); # cascade | |||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | 0 | return; | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | 8 | 1; | |||||||||
| 90 | |||||||||||
| 91 | __END__ | ||||||||||
| 92 | |||||||||||
| 93 | =pod | ||||||||||
| 94 | |||||||||||
| 95 | =encoding UTF-8 | ||||||||||
| 96 | |||||||||||
| 97 | =head1 NAME | ||||||||||
| 98 | |||||||||||
| 99 | Mail::MtPolicyd::ConnectionPool - a singleton to hold all configure connections | ||||||||||
| 100 | |||||||||||
| 101 | =head1 VERSION | ||||||||||
| 102 | |||||||||||
| 103 | version 2.01 | ||||||||||
| 104 | |||||||||||
| 105 | =head1 AUTHOR | ||||||||||
| 106 | |||||||||||
| 107 | Markus Benning <ich@markusbenning.de> | ||||||||||
| 108 | |||||||||||
| 109 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 110 | |||||||||||
| 111 | This software is Copyright (c) 2014 by Markus Benning <ich@markusbenning.de>. | ||||||||||
| 112 | |||||||||||
| 113 | This is free software, licensed under: | ||||||||||
| 114 | |||||||||||
| 115 | The GNU General Public License, Version 2, June 1991 | ||||||||||
| 116 | |||||||||||
| 117 | =cut | ||||||||||