class SiteConnectionManager

HTTP site connection pool

Constants

SECURE_OPTS

Public Class Methods

new(uri_key) click to toggle source
Calls superclass method
# File lib/polyphony/http/client/site_connection_manager.rb, line 11
def initialize(uri_key)
  @uri_key = uri_key
  super(limit: 4)
end

Public Instance Methods

acquire() click to toggle source

def method_missing(sym, *args)

raise "Invalid method #{sym}"

end

Calls superclass method
# File lib/polyphony/http/client/site_connection_manager.rb, line 20
def acquire
  Thread.current.agent.ref
  prepare_first_connection if @size.zero?
  super
ensure
  Thread.current.agent.unref
  # The size goes back to 0 only in case existing connections get into an
  # error state and then get discarded
  @state = nil if @size == 0
end
connect() click to toggle source
# File lib/polyphony/http/client/site_connection_manager.rb, line 76
def connect
  socket = create_socket
  protocol = socket_protocol(socket)
  case protocol
  when :http1
    HTTP1Adapter.new(socket)
  when :http2
    HTTP2Adapter.new(socket)
  else
    raise "Unknown protocol #{protocol.inspect}"
  end
end
create_first_connection() click to toggle source
# File lib/polyphony/http/client/site_connection_manager.rb, line 42
def create_first_connection
  @first_connection_queue = []
  # @first_connection_queue << Fiber.current

  adapter = connect
  @state = adapter.protocol
  send(:"setup_#{@state}_allocator", adapter)
  dequeue_first_connection_waiters
end
create_socket() click to toggle source
# File lib/polyphony/http/client/site_connection_manager.rb, line 99
def create_socket
  case @uri_key[:scheme]
  when 'http'
    Polyphony::Net.tcp_connect(@uri_key[:host], @uri_key[:port])
  when 'https'
    Polyphony::Net.tcp_connect(@uri_key[:host], @uri_key[:port], SECURE_OPTS)
  else
    raise "Invalid scheme #{@uri_key[:scheme].inspect}"
  end
end
dequeue_first_connection_waiters() click to toggle source
# File lib/polyphony/http/client/site_connection_manager.rb, line 69
def dequeue_first_connection_waiters
  return unless @first_connection_queue

  @first_connection_queue.each(&:schedule)
  @first_connection_queue = nil
end
prepare_first_connection() click to toggle source
# File lib/polyphony/http/client/site_connection_manager.rb, line 31
def prepare_first_connection
  case @state
  when nil
    @state = :first_connection
    create_first_connection
  when :first_connection
    @first_connection_queue << Fiber.current
    suspend
  end
end
setup_http1_allocator(adapter) click to toggle source
# File lib/polyphony/http/client/site_connection_manager.rb, line 52
def setup_http1_allocator(adapter)
  @size += 1
  adapter.extend ResourceExtensions
  @stock << adapter
  @allocator = proc { connect }
end
setup_http2_allocator(adapter) click to toggle source
# File lib/polyphony/http/client/site_connection_manager.rb, line 59
def setup_http2_allocator(adapter)
  @adapter = adapter
  @limit = 20
  @size += 1
  stream_adapter = adapter.allocate_stream_adapter
  stream_adapter.extend ResourceExtensions
  @stock << stream_adapter
  @allocator = proc { adapter.allocate_stream_adapter }
end
socket_protocol(socket) click to toggle source
# File lib/polyphony/http/client/site_connection_manager.rb, line 89
def socket_protocol(socket)
  if socket.is_a?(OpenSSL::SSL::SSLSocket) && socket.alpn_protocol == 'h2'
    :http2
  else
    :http1
  end
end