Class: Monga::Connection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/monga/connection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Connection

Simple connection wrapper. Accpets

  • host/port or server

  • connection type

  • timeout



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/monga/connection.rb', line 14

def initialize(opts)
  @type = opts[:type]

  host, port = if server = opts[:server]
    h, p = server.split(":")
    [h, p.to_i]
  else
    h = opts[:host] || Monga::DEFAULT_HOST
    p = opts[:port] || Monga::DEFAULT_PORT
    [h, p]
  end
  timeout = opts[:timeout]

  @connection = case @type
  when :em
    require File.expand_path("../connections/em_connection", __FILE__)
    Monga::Connections::EMConnection.connect(host, port, timeout)
  when :sync
    require File.expand_path("../connections/em_connection", __FILE__)
    require File.expand_path("../connections/fibered_connection", __FILE__)
    Monga::Connections::FiberedConnection.connect(host, port, timeout)
  when :block
    require File.expand_path("../connections/kgio_connection", __FILE__)
    Monga::Connections::KGIOConnection.connect(host, port, timeout)
  else
    raise Monga::Exceptions::WrongConnectionType, "Connection type `#{opts[:type]}` is non valid, choose one of: :em, :sync, or :block" unless conn_type
  end
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/monga/connection.rb', line 7

def type
  @type
end

Class Method Details

.proxy_connection_class(type, client) ⇒ Object

Returns name of proxy_connection class



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/monga/connection.rb', line 44

def self.proxy_connection_class(type, client)
  case type
  when :em
    require File.expand_path("../connections/em_proxy_connection", __FILE__)
    Monga::Connections::EMProxyConnection.new(client)
  when :sync
    require File.expand_path("../connections/em_proxy_connection", __FILE__)
    require File.expand_path("../connections/fibered_proxy_connection", __FILE__)
    Monga::Connections::FiberedProxyConnection.new(client)
  when :block
    require File.expand_path("../connections/proxy_connection", __FILE__)
    Monga::Connections::ProxyConnection.new(client)
  end
end