Class: Plum::ClientSession

Inherits:
Object
  • Object
show all
Defined in:
lib/plum/client/client_session.rb

Overview

HTTP/2 client session.

Constant Summary collapse

HTTP2_DEFAULT_SETTINGS =
{
  enable_push: 0, # TODO: api?
  initial_window_size: 2 ** 30, # TODO
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket, config) ⇒ ClientSession

Returns a new instance of ClientSession.


12
13
14
15
16
17
18
19
# File 'lib/plum/client/client_session.rb', line 12

def initialize(socket, config)
  @socket = socket
  @config = config
  @http2_settings = HTTP2_DEFAULT_SETTINGS.merge(@config[:http2_settings])

  @plum = setup_plum
  @responses = Set.new
end

Instance Attribute Details

#plumObject (readonly)

Returns the value of attribute plum


10
11
12
# File 'lib/plum/client/client_session.rb', line 10

def plum
  @plum
end

Instance Method Details

#closeObject


31
32
33
34
35
36
# File 'lib/plum/client/client_session.rb', line 31

def close
  @closed = true
  @responses.each(&:_fail)
  @responses.clear
  @plum.close
end

#empty?Boolean

Returns:

  • (Boolean)

27
28
29
# File 'lib/plum/client/client_session.rb', line 27

def empty?
  @responses.empty?
end

#request(headers, body, options, &headers_cb) ⇒ Object


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/plum/client/client_session.rb', line 38

def request(headers, body, options, &headers_cb)
  headers = { ":method" => nil,
              ":path" => nil,
              ":authority" => @config[:hostname],
              ":scheme" => @config[:scheme]
  }.merge(headers)

  response = Response.new
  @responses << response
  stream = @plum.open_stream
  stream.send_headers(headers, end_stream: !body)
  stream.send_data(body, end_stream: true) if body

  stream.on(:headers) { |resp_headers_raw|
    response._headers(resp_headers_raw)
    headers_cb.call(response) if headers_cb
  }
  stream.on(:data) { |chunk|
    response._chunk(chunk)
    check_window(stream)
  }
  stream.on(:end_stream) {
    response._finish
    @responses.delete(response)
  }
  stream.on(:stream_error) { |ex|
    response._fail
    raise ex
  }
  response
end

#succObject


21
22
23
24
25
# File 'lib/plum/client/client_session.rb', line 21

def succ
  @plum << @socket.readpartial(16384)
rescue => e
  fail(e)
end