Class: Dagger::ConnectionManager

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

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ ConnectionManager



7
8
9
10
11
# File 'lib/dagger/connection_manager.rb', line 7

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.



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

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

#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.



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

def 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



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

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