net-http2 0.10.0 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 53d47a5179357c9eebc6e0c23e6eb0ba724d4ccc
4
- data.tar.gz: d82ec20911a804eef2183564580fbde8d03483e0
3
+ metadata.gz: 2f412e44e24ba9a2bdb452b616a6e0102fe8be71
4
+ data.tar.gz: 1f0149a8b2aace96f40bb6da2f56983ada7d2e3f
5
5
  SHA512:
6
- metadata.gz: 784a337baecb984bffb5b1594c40a46a4aab7bce5cc570d651953a49af0687658ccfeef4026cbbfa2e7b7bdf709e7990f8ffa13563de00fcde180ae9bbb6d7c0
7
- data.tar.gz: 784009bbae1c958ad17fb0efbb227c7291f4ec59d270e8c5aea4fbe902e39a141ebacb15abf52dd7a9dc1ef14b0308b034669cf8466bc881f7ecc81f87256c63
6
+ metadata.gz: f8427579c27e5ece7b87659fbc263ce96b243308ae6ff571ef94071804efdc59e604a8c46eb95ffbee57161bfaf9d85eba9d1556cb33b20f68b829a18d26fdd2
7
+ data.tar.gz: 3b79aa2279ab86dcd05bec6495fbb3f8bf28a218efee9af89e45031288dcb7f7332c9e8a36ee15b215bd2e3df22b70061a5a4bb27b6be9fea5a98bd939210eed
data/README.md CHANGED
@@ -76,7 +76,10 @@ client.close
76
76
  * **new(url, options={})** → **`NetHttp2::Client`**
77
77
 
78
78
  Returns a new client. `url` is a `string` such as `http://nghttp2.org`.
79
- The only current option is `:ssl_context`, in case the url has an https scheme and you want your SSL client to use a custom context.
79
+ The current options are:
80
+
81
+ * `:connect_timeout`, specifies the max connect timeout in seconds (defaults to 60).
82
+ * `:ssl_context`, in case the url has an https scheme and you want your SSL client to use a custom context.
80
83
 
81
84
  To create a new client:
82
85
  ```ruby
@@ -1,6 +1,7 @@
1
1
  require 'net-http2/client'
2
2
  require 'net-http2/response'
3
3
  require 'net-http2/request'
4
+ require 'net-http2/socket'
4
5
  require 'net-http2/stream'
5
6
  require 'net-http2/version'
6
7
 
@@ -11,8 +11,9 @@ module NetHttp2
11
11
  attr_reader :uri
12
12
 
13
13
  def initialize(url, options={})
14
- @uri = URI.parse(url)
15
- @ssl_context = add_npn_to_context(options[:ssl_context] || OpenSSL::SSL::SSLContext.new)
14
+ @uri = URI.parse(url)
15
+ @connect_timeout = options[:connect_timeout] || 60
16
+ @ssl_context = add_npn_to_context(options[:ssl_context] || OpenSSL::SSL::SSLContext.new)
16
17
 
17
18
  @is_ssl = (@uri.scheme == 'https')
18
19
 
@@ -99,24 +100,12 @@ module NetHttp2
99
100
  end
100
101
 
101
102
  def new_socket
102
- tcp = TCPSocket.new(@uri.host, @uri.port)
103
-
104
- if ssl?
105
- socket = OpenSSL::SSL::SSLSocket.new(tcp, @ssl_context)
106
- socket.sync_close = true
107
- socket.hostname = @uri.hostname
108
-
109
- socket.connect
110
-
111
- socket
112
- else
113
- tcp
114
- end
103
+ NetHttp2::Socket.create(@uri, ssl: ssl?, ssl_context: @ssl_context, connect_timeout: @connect_timeout)
115
104
  end
116
105
 
117
106
  def ensure_sent_before_receiving
118
107
  while !@first_data_sent
119
- sleep 0.1
108
+ sleep 0.01
120
109
  end
121
110
  end
122
111
 
@@ -0,0 +1,50 @@
1
+ module NetHttp2
2
+
3
+ module Socket
4
+
5
+ def self.create(uri, options)
6
+ options[:ssl] ? ssl_socket(uri, options) : tcp_socket(uri, options)
7
+ end
8
+
9
+ def self.ssl_socket(uri, options)
10
+ tcp = tcp_socket(uri, options)
11
+
12
+ socket = OpenSSL::SSL::SSLSocket.new(tcp, options[:ssl_context])
13
+ socket.sync_close = true
14
+ socket.hostname = uri.hostname
15
+
16
+ socket.connect
17
+
18
+ socket
19
+ end
20
+
21
+ def self.tcp_socket(uri, options)
22
+ family = ::Socket::AF_INET
23
+ address = ::Socket.getaddrinfo(uri.host, nil, family).first[3]
24
+ sockaddr = ::Socket.pack_sockaddr_in(uri.port, address)
25
+
26
+ socket = ::Socket.new(family, ::Socket::SOCK_STREAM, 0)
27
+ socket.setsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY, 1)
28
+
29
+ begin
30
+ socket.connect_nonblock(sockaddr)
31
+ rescue IO::WaitWritable
32
+ if IO.select(nil, [socket], nil, options[:connect_timeout])
33
+ begin
34
+ socket.connect_nonblock(sockaddr)
35
+ rescue Errno::EISCONN
36
+ # socket is connected
37
+ rescue
38
+ socket.close
39
+ raise
40
+ end
41
+ else
42
+ socket.close
43
+ raise Errno::ETIMEDOUT
44
+ end
45
+ end
46
+
47
+ socket
48
+ end
49
+ end
50
+ end
@@ -7,8 +7,11 @@ module NetHttp2
7
7
  @headers = {}
8
8
  @data = ''
9
9
  @request = nil
10
- @completed = false
11
10
  @async = false
11
+ @completed = false
12
+ @mutex = Mutex.new
13
+ @cv = ConditionVariable.new
14
+
12
15
 
13
16
  listen_for_headers
14
17
  listen_for_data
@@ -62,7 +65,12 @@ module NetHttp2
62
65
  def listen_for_close
63
66
  @h2_stream.on(:close) do |data|
64
67
  @completed = true
65
- @request.emit(:close, data) if async?
68
+
69
+ if async?
70
+ @request.emit(:close, data)
71
+ else
72
+ @mutex.synchronize { @cv.signal }
73
+ end
66
74
  end
67
75
  end
68
76
 
@@ -85,11 +93,7 @@ module NetHttp2
85
93
  end
86
94
 
87
95
  def wait_for_completed
88
- cutoff_time = Time.now + @request.timeout
89
-
90
- while !@completed && Time.now < cutoff_time
91
- sleep 0.1
92
- end
96
+ @mutex.synchronize { @cv.wait(@mutex, @request.timeout) }
93
97
  end
94
98
  end
95
99
  end
@@ -1,3 +1,3 @@
1
1
  module NetHttp2
2
- VERSION = "0.10.0"
2
+ VERSION = "0.11.0"
3
3
  end
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.10.0
4
+ version: 0.11.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-05-08 00:00:00.000000000 Z
11
+ date: 2016-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http-2
@@ -88,6 +88,7 @@ files:
88
88
  - lib/net-http2/client.rb
89
89
  - lib/net-http2/request.rb
90
90
  - lib/net-http2/response.rb
91
+ - lib/net-http2/socket.rb
91
92
  - lib/net-http2/stream.rb
92
93
  - lib/net-http2/version.rb
93
94
  - net-http2.gemspec