Class: Dagger::ConnectionManager

Inherits:
Object
  • Object
show all
Defined in:
lib/dagger/connection_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ ConnectionManager

Returns a new instance of ConnectionManager.



5
6
7
8
9
# File 'lib/dagger/connection_manager.rb', line 5

def initialize(opts = {})
  @opts = opts
  @active_connections = {}
  @mutex = Mutex.new
end

Instance Method Details

#connection_for(uri) ⇒ Object

Gets a connection for a given URI. This is for internal use only as it’s subject to change (we’ve moved between HTTP client schemes in the past and may do it again).

uri is expected to be a string.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dagger/connection_manager.rb', line 26

def connection_for(uri)
  @mutex.synchronize do
    connection = @active_connections[[uri.host, uri.port]]

    if connection.nil?
      connection = Dagger::Client.init_connection(uri, @opts)
      connection.start

      @active_connections[[uri.host, uri.port]] = connection
      # puts "#{@active_connections.count} connections"
    end

    connection
  end
end

#send_request(uri, request) ⇒ Object

Executes an HTTP request to the given URI with the given method. Also allows a request body, headers, and query string to be specified.



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dagger/connection_manager.rb', line 44

def send_request(uri, request)
  connection = connection_for(uri)
  @mutex.synchronize do
    begin
      connection.request(request)
    rescue StandardError => err
      err
    end
  end.tap do |result|
    raise(result) if result.is_a?(StandardError)
  end
end

#shutdownObject



11
12
13
14
15
16
17
18
19
# File 'lib/dagger/connection_manager.rb', line 11

def shutdown
  @mutex.synchronize do
    # puts "Shutting down connections: #{@active_connections.count}"
    @active_connections.each do |_, connection|
      connection.finish
    end
    @active_connections = {}
  end
end