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