Class: Lowdown::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/lowdown/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, ssl_context) ⇒ Connection



27
28
29
# File 'lib/lowdown/connection.rb', line 27

def initialize(uri, ssl_context)
  @uri, @ssl_context = URI(uri), ssl_context
end

Instance Attribute Details

#ssl_contextObject (readonly)

Returns the value of attribute ssl_context.



25
26
27
# File 'lib/lowdown/connection.rb', line 25

def ssl_context
  @ssl_context
end

#uriObject (readonly)

Returns the value of attribute uri.



25
26
27
# File 'lib/lowdown/connection.rb', line 25

def uri
  @uri
end

Instance Method Details

#closeObject

Terminates the worker thread and closes the socket. Finally it peforms one more check for pending jobs dispatched onto the main thread.



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/lowdown/connection.rb', line 59

def close
  flush

  @worker_thread[:should_exit] = true
  @worker_thread.join

  @ssl.close

  sleep 0.1
  @main_queue.drain!

  @socket = @ssl = @http = @main_queue = @work_queue = @requests = @exceptions = @worker_thread = nil
end

#flushObject



73
74
75
76
77
78
# File 'lib/lowdown/connection.rb', line 73

def flush
  until @work_queue.empty? && @requests.zero?
    @main_queue.drain!
    sleep 0.1
  end
end

#openObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/lowdown/connection.rb', line 31

def open
  @socket = TCPSocket.new(@uri.host, @uri.port)

  @ssl = OpenSSL::SSL::SSLSocket.new(@socket, @ssl_context)
  @ssl.sync_close = true
  @ssl.hostname = @uri.hostname
  @ssl.connect

  @http = HTTP2::Client.new
  @http.on(:frame) do |bytes|
    @ssl.print(bytes)
    @ssl.flush
  end

  @main_queue    = Threading::DispatchQueue.new
  @work_queue    = Threading::DispatchQueue.new
  @requests      = Threading::Counter.new
  @exceptions    = Queue.new
  @worker_thread = start_worker_thread!
end

#open?Boolean



52
53
54
# File 'lib/lowdown/connection.rb', line 52

def open?
  !@ssl.nil? && !@ssl.closed?
end

#post(path, headers, body, &callback) ⇒ Object



80
81
82
# File 'lib/lowdown/connection.rb', line 80

def post(path, headers, body, &callback)
  request('POST', path, headers, body, &callback)
end