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