net-http2 0.11.1 → 0.12.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/README.md +22 -21
- data/lib/net-http2/client.rb +19 -3
- data/lib/net-http2/stream.rb +8 -1
- data/lib/net-http2/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e3f05adbbbbe99f8eb15097186a59db80eb66042
|
4
|
+
data.tar.gz: e22193008420db5d3dffffab3a39ba73eff062df
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8ede872c9f2ed793b385354507dd0bc0dc4bb66fef127afa673eddb005e19125342faf94b977cd1994803b239b36425ebbe40bee044bdf1e82d1343d9051b21a
|
7
|
+
data.tar.gz: 2e4e48b32df8d4860590d155b15d4bfd363a8f10d27897ab2ff3643c7a93827ff10fd40b7bdfe2be0a9c3bd7ffb11df45981053e34140f894e0c9273294f6303
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
ruby-2.3.
|
1
|
+
ruby-2.3.1
|
data/README.md
CHANGED
@@ -58,10 +58,8 @@ request.on(:close) { puts "request completed!" }
|
|
58
58
|
# send
|
59
59
|
client.call_async(request)
|
60
60
|
|
61
|
-
#
|
62
|
-
|
63
|
-
# your program alive.
|
64
|
-
sleep 5
|
61
|
+
# Wait for all outgoing stream to be closed
|
62
|
+
client.join
|
65
63
|
|
66
64
|
# close the connection
|
67
65
|
client.close
|
@@ -115,7 +113,6 @@ These behave similarly to HTTP/1 calls.
|
|
115
113
|
response_3 = client.call(:post, '/path3', body: "the request body", timeout: 1)
|
116
114
|
```
|
117
115
|
|
118
|
-
|
119
116
|
##### Non-blocking calls
|
120
117
|
The real benefit of HTTP/2 is being able to receive body and header streams. Instead of buffering the whole response, you might want to react immediately upon receiving those streams. This is what non-blocking calls are for.
|
121
118
|
|
@@ -127,26 +124,14 @@ The real benefit of HTTP/2 is being able to receive body and header streams. Ins
|
|
127
124
|
request = client.prepare_request(:get, '/path', headers: { 'x-custom-header' => 'custom' })
|
128
125
|
```
|
129
126
|
|
130
|
-
* **on(event, &block)**
|
131
|
-
|
132
|
-
Allows to set a callback for the request. Available events are:
|
133
|
-
|
134
|
-
* `:headers`: triggered when headers are received (called once).
|
135
|
-
* `:body_chunk`: triggered when body chunks are received (may be called multiple times).
|
136
|
-
* `:close`: triggered when the request has been completed (called once).
|
137
|
-
|
138
|
-
Even if NetHttp2 is thread-safe, the async callbacks will be executed in a different thread, so ensure that your code in the callbacks is thread-safe.
|
139
|
-
|
140
|
-
```ruby
|
141
|
-
request.on(:headers) { |headers| p headers }
|
142
|
-
request.on(:body_chunk) { |chunk| p chunk }
|
143
|
-
request.on(:close) { puts "request completed!" }
|
144
|
-
```
|
145
|
-
|
146
127
|
* **call_async(request)**
|
147
128
|
|
148
129
|
Calls the server with the async request.
|
149
130
|
|
131
|
+
* **join**
|
132
|
+
|
133
|
+
Wait for all outstanding requests to be completed.
|
134
|
+
|
150
135
|
|
151
136
|
### `NetHttp2::Request`
|
152
137
|
|
@@ -172,6 +157,22 @@ The real benefit of HTTP/2 is being able to receive body and header streams. Ins
|
|
172
157
|
|
173
158
|
The request's timeout.
|
174
159
|
|
160
|
+
* **on(event, &block)**
|
161
|
+
|
162
|
+
Allows to set a callback for the request. Available events are:
|
163
|
+
|
164
|
+
* `:headers`: triggered when headers are received (called once).
|
165
|
+
* `:body_chunk`: triggered when body chunks are received (may be called multiple times).
|
166
|
+
* `:close`: triggered when the request has been completed (called once).
|
167
|
+
|
168
|
+
Even if NetHttp2 is thread-safe, the async callbacks will be executed in a different thread, so ensure that your code in the callbacks is thread-safe.
|
169
|
+
|
170
|
+
```ruby
|
171
|
+
request.on(:headers) { |headers| p headers }
|
172
|
+
request.on(:body_chunk) { |chunk| p chunk }
|
173
|
+
request.on(:close) { puts "request completed!" }
|
174
|
+
```
|
175
|
+
|
175
176
|
|
176
177
|
### `NetHttp2::Response`
|
177
178
|
|
data/lib/net-http2/client.rb
CHANGED
@@ -44,6 +44,20 @@ module NetHttp2
|
|
44
44
|
init_vars
|
45
45
|
end
|
46
46
|
|
47
|
+
def join
|
48
|
+
while !@streams.empty? do
|
49
|
+
sleep 0.05
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def add_stream_to_monitor(stream)
|
54
|
+
@streams[stream.id] = true
|
55
|
+
end
|
56
|
+
|
57
|
+
def mark_stream_as_closed(stream)
|
58
|
+
@streams.delete(stream.id)
|
59
|
+
end
|
60
|
+
|
47
61
|
private
|
48
62
|
|
49
63
|
def init_vars
|
@@ -52,10 +66,11 @@ module NetHttp2
|
|
52
66
|
@socket_thread = nil
|
53
67
|
@first_data_sent = false
|
54
68
|
@mutex = Mutex.new
|
69
|
+
@streams = {}
|
55
70
|
end
|
56
71
|
|
57
72
|
def new_stream
|
58
|
-
NetHttp2::Stream.new(
|
73
|
+
NetHttp2::Stream.new(client: self, h2_stream: h2.new_stream)
|
59
74
|
end
|
60
75
|
|
61
76
|
def ensure_open
|
@@ -69,14 +84,15 @@ module NetHttp2
|
|
69
84
|
|
70
85
|
begin
|
71
86
|
socket_loop
|
72
|
-
rescue
|
87
|
+
rescue Exception => e
|
73
88
|
# socket closed
|
89
|
+
Thread.main.raise e
|
74
90
|
ensure
|
75
91
|
@socket.close unless @socket.closed?
|
76
92
|
@socket = nil
|
77
93
|
@socket_thread = nil
|
78
94
|
end
|
79
|
-
end
|
95
|
+
end
|
80
96
|
end
|
81
97
|
end
|
82
98
|
|
data/lib/net-http2/stream.rb
CHANGED
@@ -3,6 +3,7 @@ module NetHttp2
|
|
3
3
|
class Stream
|
4
4
|
|
5
5
|
def initialize(options={})
|
6
|
+
@client = options[:client]
|
6
7
|
@h2_stream = options[:h2_stream]
|
7
8
|
@headers = {}
|
8
9
|
@data = ''
|
@@ -12,12 +13,15 @@ module NetHttp2
|
|
12
13
|
@mutex = Mutex.new
|
13
14
|
@cv = ConditionVariable.new
|
14
15
|
|
15
|
-
|
16
16
|
listen_for_headers
|
17
17
|
listen_for_data
|
18
18
|
listen_for_close
|
19
19
|
end
|
20
20
|
|
21
|
+
def id
|
22
|
+
@h2_stream.id
|
23
|
+
end
|
24
|
+
|
21
25
|
def call_with(request)
|
22
26
|
@request = request
|
23
27
|
send_request_data
|
@@ -27,6 +31,8 @@ module NetHttp2
|
|
27
31
|
def async_call_with(request)
|
28
32
|
@request = request
|
29
33
|
@async = true
|
34
|
+
@client.add_stream_to_monitor(self)
|
35
|
+
|
30
36
|
send_request_data
|
31
37
|
end
|
32
38
|
|
@@ -68,6 +74,7 @@ module NetHttp2
|
|
68
74
|
|
69
75
|
if async?
|
70
76
|
@request.emit(:close, data)
|
77
|
+
@client.mark_stream_as_closed(self)
|
71
78
|
else
|
72
79
|
@mutex.synchronize { @cv.signal }
|
73
80
|
end
|
data/lib/net-http2/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: net-http2
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.12.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Roberto Ostinelli
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-06-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: http-2
|