Class: Monga::Clients::SingleInstanceClient
- Inherits:
-
Object
- Object
- Monga::Clients::SingleInstanceClient
- Defined in:
- lib/monga/clients/single_instance_client.rb
Instance Attribute Summary collapse
-
#status ⇒ Object
readonly
Status will inform Replica Set client of current client’s status primary/secondary/nil.
Instance Method Summary collapse
-
#aquire_connection ⇒ Object
If single connection then return it.
- #connected? ⇒ Boolean
-
#force_status! ⇒ Object
Check status of connection.
-
#initialize(opts) ⇒ SingleInstanceClient
constructor
A new instance of SingleInstanceClient.
- #primary? ⇒ Boolean
- #secondary? ⇒ Boolean
Constructor Details
#initialize(opts) ⇒ SingleInstanceClient
Returns a new instance of SingleInstanceClient.
7 8 9 10 11 12 13 14 |
# File 'lib/monga/clients/single_instance_client.rb', line 7 def initialize(opts) pool_size = opts[:pool_size] if pool_size && pool_size > 1 @connection_pool = Monga::ConnectionPool.new(opts) else @connection = Monga::Connection.new(opts) end end |
Instance Attribute Details
#status ⇒ Object (readonly)
Status will inform Replica Set client of current client’s status primary/secondary/nil
5 6 7 |
# File 'lib/monga/clients/single_instance_client.rb', line 5 def status @status end |
Instance Method Details
#aquire_connection ⇒ Object
If single connection then return it. If connection pool then aquire connection from pool. If connection is not connected, then status will be setted to nil.
19 20 21 22 23 24 25 26 27 |
# File 'lib/monga/clients/single_instance_client.rb', line 19 def aquire_connection conn = if @connection_pool @connection_pool.aquire_connection else @connection end @status = nil unless conn.connected? conn end |
#connected? ⇒ Boolean
29 30 31 |
# File 'lib/monga/clients/single_instance_client.rb', line 29 def connected? aquire_connection.connected? end |
#force_status! ⇒ Object
Check status of connection. If ReplicaSetClient can’t find connection with read_pref status it will send foce_status! to all clients while timout happend or while preferred status will be returned
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/monga/clients/single_instance_client.rb', line 37 def force_status! if connected? conn = aquire_connection conn.is_master? do |status| @status = status yield(@status) if block_given? end else yield(@status) if block_given? end end |
#primary? ⇒ Boolean
49 50 51 |
# File 'lib/monga/clients/single_instance_client.rb', line 49 def primary? @status == :primary end |
#secondary? ⇒ Boolean
53 54 55 |
# File 'lib/monga/clients/single_instance_client.rb', line 53 def secondary? @status == :secondary end |