| 1 | package Salvation::MongoMgr::Connection; | ||
| 2 | |||
| 3 | use utf8; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use feature 'state'; | ||
| 7 | |||
| 8 | use JSON 'from_json'; | ||
| 9 | use File::Slurp 'read_file'; | ||
| 10 | use Salvation::TC (); | ||
| 11 | use MongoDB (); | ||
| 12 | |||
| 13 | |||
| 14 | sub new { | ||
| 15 | |||
| 16 | my ( $proto, %args ) = @_; | ||
| 17 | |||
| 18 | Salvation::TC -> assert( \%args, 'HashRef( | ||
| 19 | Str :db! | ||
| 20 | Str :config_file!, | ||
| 21 | Maybe[Str] :auth_config_file, | ||
| 22 | Int :timeout, | ||
| 23 | Int :query_timeout, | ||
| 24 | Int :wtimeout, | ||
| 25 | Str :auth_db_name, | ||
| 26 | Str :host, | ||
| 27 | Str :use_auth_for | ||
| 28 | )' ); | ||
| 29 | |||
| 30 | unless( exists $args{ 'use_auth_for' } ) { | ||
| 31 | |||
| 32 | $args{ 'use_auth_for' } = $args{ 'db' }; | ||
| 33 | } | ||
| 34 | |||
| 35 | my $self = bless( \%args, ( ref( $proto ) || $proto ) ); | ||
| 36 | |||
| 37 | if( defined( my $auth_config_file = $self -> auth_config_file() ) ) { | ||
| 38 | |||
| 39 | my $rv = from_json( scalar( read_file( $auth_config_file ) ) ); | ||
| 40 | |||
| 41 | Salvation::TC -> assert( $rv, sprintf( 'HashRef( | ||
| 42 | HashRef( | ||
| 43 | HashRef( | ||
| 44 | Str :login!, | ||
| 45 | Str :password! | ||
| 46 | ) :%s! | ||
| 47 | ) :db_auth! | ||
| 48 | |||
| 49 | )', $self -> { 'use_auth_for' } ) ); | ||
| 50 | |||
| 51 | $self -> { '_auth_info' } = $rv; | ||
| 52 | |||
| 53 | } else { | ||
| 54 | |||
| 55 | $self -> { '_auth_info' } = { db_auth => { $self -> { 'use_auth_for' } => {} } }; | ||
| 56 | } | ||
| 57 | |||
| 58 | $self -> { '_mongo_servers_list' } = join( ',', @{ $self -> servers_list() } ); | ||
| 59 | |||
| 60 | return $self; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub _connect { | ||
| 64 | |||
| 65 | my ( $self ) = @_; | ||
| 66 | my $mongo_servers_list = $self -> { '_mongo_servers_list' }; | ||
| 67 | state $HMONGO = {}; | ||
| 68 | |||
| 69 | if( | ||
| 70 | exists $HMONGO -> { $mongo_servers_list } | ||
| 71 | && ( $HMONGO -> { $mongo_servers_list } -> { 'pid' } == $$ ) | ||
| 72 | ) { | ||
| 73 | |||
| 74 | $self -> { '_connection' } = $HMONGO -> { $mongo_servers_list } -> { 'handle' }; | ||
| 75 | |||
| 76 | } else { | ||
| 77 | |||
| 78 | my ( $login, $password ) = @{ $self | ||
| 79 | -> { '_auth_info' } | ||
| 80 | -> { 'db_auth' } | ||
| 81 | -> { $self -> { 'use_auth_for' } } | ||
| 82 | }{ 'login', 'password' }; | ||
| 83 | |||
| 84 | $self -> { '_connection' } = MongoDB::MongoClient -> new( | ||
| 85 | host => "mongodb://${mongo_servers_list}", | ||
| 86 | timeout => ( $self -> { 'timeout' } // 20_000 ), | ||
| 87 | query_timeout => ( $self -> { 'query_timeout' } // 600_000 ), # in ms | ||
| 88 | wtimeout => ( $self -> { 'wtimeout' } // 30_000 ), | ||
| 89 | find_master => 1, | ||
| 90 | auto_connect => 1, | ||
| 91 | w => 1, | ||
| 92 | dt_type => undef, | ||
| 93 | db_name => ( $self -> { 'auth_db_name' } // 'admin' ), | ||
| 94 | ( defined $login ? ( username => $login ) : () ), | ||
| 95 | ( defined $password ? ( password => $password ) : () ), | ||
| 96 | ); | ||
| 97 | |||
| 98 | $HMONGO -> { $mongo_servers_list } = { | ||
| 99 | handle => $self -> { '_connection' }, | ||
| 100 | pid => $$, | ||
| 101 | }; | ||
| 102 | } | ||
| 103 | |||
| 104 | return $self -> { '_connection' }; | ||
| 105 | } | ||
| 106 | |||
| 107 | sub config_file { | ||
| 108 | |||
| 109 | my ( $self ) = @_; | ||
| 110 | |||
| 111 | return $self -> { 'config_file' }; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub auth_config_file { | ||
| 115 | |||
| 116 | my ( $self ) = @_; | ||
| 117 | |||
| 118 | unless( exists $self -> { 'auth_config_file' } ) { | ||
| 119 | |||
| 120 | $self -> { 'auth_config_file' } = $self -> { 'config_file' }; | ||
| 121 | } | ||
| 122 | |||
| 123 | return $self -> { 'auth_config_file' }; | ||
| 124 | } | ||
| 125 | |||
| 126 | sub servers_list { | ||
| 127 | |||
| 128 | my ( $self ) = @_; | ||
| 129 | |||
| 130 | unless( exists $self -> { 'servers_list' } ) { | ||
| 131 | |||
| 132 | if( exists $self -> { 'host' } ) { | ||
| 133 | |||
| 134 | $self -> { 'servers_list' } = [ $self -> { 'host' } ]; | ||
| 135 | |||
| 136 | } else { | ||
| 137 | |||
| 138 | my $config = from_json( scalar( read_file( $self -> config_file() ) ) ); | ||
| 139 | |||
| 140 | Salvation::TC -> assert( $config, 'HashRef( ArrayRef[Str] :servers_list! )' ); | ||
| 141 | |||
| 142 | $self -> { 'servers_list' } = $config -> { 'servers_list' }; | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | return $self -> { 'servers_list' }; | ||
| 147 | } | ||
| 148 | |||
| 149 | sub get_connection { | ||
| 150 | |||
| 151 | my ( $self ) = @_; | ||
| 152 | |||
| 153 | return $self -> _connect(); | ||
| 154 | } | ||
| 155 | |||
| 156 | sub get_database { | ||
| 157 | |||
| 158 | my ( $self, $db ) = @_; | ||
| 159 | |||
| 160 | return $self -> get_connection() -> get_database( $db // $self -> { 'db' } ); | ||
| 161 | } | ||
| 162 | |||
| 163 | sub get_collection { | ||
| 164 | |||
| 165 | my ( $self, $collection ) = @_; | ||
| 166 | |||
| 167 | return $self -> get_database() -> get_collection( $collection ); | ||
| 168 | } | ||
| 169 | |||
| 170 | |||
| 171 | 1; | ||
| 172 | |||
| 173 | __END__ |