Class: Dialed::HTTP::Operator

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/dialed/http/operator.rb

Instance Method Summary collapse

Constructor Details

#initializeOperator



8
9
10
# File 'lib/dialed/http/operator.rb', line 8

def initialize
  @dialers = {}
end

Instance Method Details

#checkout_dialer(connection_builder, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dialed/http/operator.rb', line 28

def checkout_dialer(connection_builder, &block)
  full_connection_uri = connection_builder.full_connection_uri
  plex_type = connection_builder.plex_type
  case [full_connection_uri, plex_type, registry.keys]
  in [URI => uri, :h2, [*, ^uri, *]]
    block.call multiplex_dialers.fetch(uri)
  in [URI => uri, :h1 | :h11, [*, ^uri, *]]
    # not thread safe. Use async gem as it is not implemented with threads
    dialer = remove_dialer(uri)
    raise Dialed::Error, 'Dialer not found when it was expected to be. Is it possible you are using multiple threads?' unless dialer

    block.call dialer
    register_dialer dialer
  in [URI => uri, Symbol, Array]
    register_dialer dialer
    block.call dialer
  else
    raise Dialed::Error, "Unknown Dialer type: #{full_connection_uri}, #{plex_type}"
  end
end

#fetch_dialer(_uri) ⇒ Object



49
50
51
# File 'lib/dialed/http/operator.rb', line 49

def fetch_dialer(_uri, &)
  Dialer.new(connection_builder, &)
end

#multiplex_dialersObject



23
24
25
26
# File 'lib/dialed/http/operator.rb', line 23

def multiplex_dialers
  @dialers.reject { |_, dialer| dialer.disconnected? }
    .select { |_, dialer| dialer.multiplex? }
end

#register_dialer(dialer) ⇒ Object



57
58
59
# File 'lib/dialed/http/operator.rb', line 57

def register_dialer(dialer)
  @dialers[dialer.full_connection_uri] = dialer
end

#remove_dialer(dialer) ⇒ Object



53
54
55
# File 'lib/dialed/http/operator.rb', line 53

def remove_dialer(dialer)
  @dialers.delete(dialer.full_connection_uri)
end

#request_call(&block) ⇒ Object



12
13
14
15
# File 'lib/dialed/http/operator.rb', line 12

def request_call(&block)
  connection_builder = block.call(ConnectionBuilder.new) if block_given?
  connection_builder ||= ConnectionBuilder.apply_defaults
end

#singleplex_dialersObject



17
18
19
20
21
# File 'lib/dialed/http/operator.rb', line 17

def singleplex_dialers
  @dialers
    .reject { |_, dialer| dialer.disconnected? }
    .select { |_, dialer| dialer.singleplex? }
end