net-http-pool 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +5 -0
- data/README.md +15 -1
- data/SPEC.md +2 -1
- data/lib/net/http/pool.rb +9 -29
- data/net-http-pool.gemspec +1 -1
- data/test/pool.rb +19 -6
- metadata +12 -11
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# Net::HTTP::Pool
|
2
2
|
|
3
|
+
[![Build Status](https://secure.travis-ci.org/elcuervo/net-http-pool.png?branch=master)](http://travis-ci.org/elcuervo/net-http-pool)
|
4
|
+
|
3
5
|
![Pool](http://www.beijingboyce.com/wp-content/uploads/2008/03/pool-table.JPG)
|
4
6
|
|
5
7
|
Act like a database pool but for HTTP.
|
@@ -12,13 +14,25 @@ verb block it's async.
|
|
12
14
|
The target of the lib it's to provide DB-like pool to handle information
|
13
15
|
exchange.
|
14
16
|
|
17
|
+
## Motivation
|
18
|
+
|
19
|
+
The main goal it's to provide a easy to use HTTP pool exploiting HTTP 1.1
|
20
|
+
persistent connections in a non-blocking way.
|
21
|
+
|
15
22
|
## Example
|
16
23
|
|
17
24
|
```ruby
|
18
25
|
require 'net/http/pool'
|
19
26
|
|
20
27
|
pool = Net::HTTP::Pool.new("http://elcuervo.co")
|
21
|
-
|
28
|
+
request = Net::HTTP::Get.new("/humans.txt")
|
29
|
+
|
30
|
+
pool.request(request) do |response|
|
22
31
|
File.open('nevermore.txt', 'w') { |f| f << response.body } if response.code == "200"
|
23
32
|
end
|
24
33
|
```
|
34
|
+
|
35
|
+
## Thanks to
|
36
|
+
|
37
|
+
* [@foca](http://github.com/foca) and [@dcadenas](http://github.com/dcadenas)
|
38
|
+
for code-review and corrections.
|
data/SPEC.md
CHANGED
data/lib/net/http/pool.rb
CHANGED
@@ -7,13 +7,6 @@ require 'net/http/persistent'
|
|
7
7
|
class Net::HTTP::Pool
|
8
8
|
include Celluloid
|
9
9
|
|
10
|
-
# Private: Custom Error for a missing block in the call.
|
11
|
-
class MissingBlock < StandardError
|
12
|
-
def message
|
13
|
-
"You must pass a block"
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
10
|
# Public: The Connection Pool.
|
18
11
|
#
|
19
12
|
# host - The string of the host.
|
@@ -37,9 +30,10 @@ class Net::HTTP::Pool
|
|
37
30
|
#
|
38
31
|
# &block - The block to be called passing the current connection
|
39
32
|
def round_robin(&block)
|
40
|
-
raise
|
33
|
+
raise LocalJumpError unless block
|
34
|
+
|
41
35
|
@current_index = @current_index > @pool.size ? 0 : @current_index + 1
|
42
|
-
yield @pool[@current_index]
|
36
|
+
yield @pool[@current_index]
|
43
37
|
end
|
44
38
|
|
45
39
|
# Public: Helper to access the connection asynchronous
|
@@ -55,30 +49,16 @@ class Net::HTTP::Pool
|
|
55
49
|
@uri = URI(host)
|
56
50
|
end
|
57
51
|
|
58
|
-
def
|
59
|
-
|
60
|
-
end
|
61
|
-
|
62
|
-
def post(path, body = nil, headers = {}, &block)
|
63
|
-
request!(path, Net::HTTP::Post, body, headers, &block)
|
52
|
+
def request(request, &block)
|
53
|
+
async_request!(request, &block)
|
64
54
|
end
|
65
55
|
|
66
|
-
def
|
67
|
-
|
68
|
-
end
|
56
|
+
def async_request(request, &block)
|
57
|
+
raise LocalJumpError unless block
|
69
58
|
|
70
|
-
def delete(path, headers = {}, &block)
|
71
|
-
request!(path, Net::HTTP::Delete, headers, &block)
|
72
|
-
end
|
73
|
-
|
74
|
-
def request(path, type, body = nil, headers = {}, &block)
|
75
|
-
raise MissingBlock unless block
|
76
59
|
@pool.with do |connection|
|
77
|
-
|
78
|
-
request.body = body if body
|
79
|
-
headers.each { |key, value| request.add_field(key, value) }
|
80
|
-
request['Content-Length'] = body && body.length || 0
|
81
|
-
yield connection.request(@uri, request) if block
|
60
|
+
yield connection.request(@uri, request)
|
82
61
|
end
|
83
62
|
end
|
63
|
+
|
84
64
|
end
|
data/net-http-pool.gemspec
CHANGED
data/test/pool.rb
CHANGED
@@ -35,34 +35,47 @@ scope do
|
|
35
35
|
end
|
36
36
|
|
37
37
|
test "check that the HTTP verbs do work" do
|
38
|
-
|
38
|
+
request = Net::HTTP::Get.new("/")
|
39
|
+
|
40
|
+
@pool.request(request) do |res|
|
39
41
|
assert_equal "200", res.code
|
40
42
|
assert_equal 'What is up dog!', res.body
|
41
43
|
end
|
42
44
|
|
43
|
-
|
45
|
+
request = Net::HTTP::Get.new("/marco")
|
46
|
+
|
47
|
+
@pool.request(request) do |res|
|
44
48
|
assert_equal "200", res.code
|
45
49
|
assert_equal 'polo', res.body
|
46
50
|
end
|
47
51
|
|
48
|
-
|
52
|
+
request = Net::HTTP::Post.new("/post")
|
53
|
+
request.body = 'test=test'
|
54
|
+
request['X-Fancy-Header'] = 'Sometimes'
|
55
|
+
|
56
|
+
@pool.request(request) do |res|
|
49
57
|
assert_equal "200", res.code
|
50
58
|
assert_equal 'this is a test', res.body
|
51
59
|
end
|
52
60
|
|
53
|
-
|
61
|
+
request = Net::HTTP::Put.new("/put")
|
62
|
+
request.body = 'run=fast'
|
63
|
+
|
64
|
+
@pool.request(request) do |res|
|
54
65
|
assert_equal "200", res.code
|
55
66
|
assert_equal 'fast', res.body
|
56
67
|
end
|
57
68
|
|
58
|
-
|
69
|
+
request = Net::HTTP::Delete.new("/delete")
|
70
|
+
@pool.request(request) do |res|
|
59
71
|
assert_equal "200", res.code
|
60
72
|
end
|
61
73
|
end
|
62
74
|
|
63
75
|
test "do not block main thread when resource is slow" do
|
64
76
|
start = Time.now
|
65
|
-
|
77
|
+
request = Net::HTTP::Get.new("/wait")
|
78
|
+
5.times { @pool.request(request) {} }
|
66
79
|
assert Time.now - start < 20
|
67
80
|
end
|
68
81
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: net-http-pool
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-04-12 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: net-http-persistent
|
16
|
-
requirement: &
|
16
|
+
requirement: &70350927480060 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '2.6'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70350927480060
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: celluloid
|
27
|
-
requirement: &
|
27
|
+
requirement: &70350927479400 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 0.9.0
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70350927479400
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: cutest
|
38
|
-
requirement: &
|
38
|
+
requirement: &70350927478640 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 1.1.3
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70350927478640
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: capybara
|
49
|
-
requirement: &
|
49
|
+
requirement: &70350927478040 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: 1.1.2
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70350927478040
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: mock-server
|
60
|
-
requirement: &
|
60
|
+
requirement: &70350927477380 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ~>
|
@@ -65,7 +65,7 @@ dependencies:
|
|
65
65
|
version: 0.1.2
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70350927477380
|
69
69
|
description:
|
70
70
|
email:
|
71
71
|
- yo@brunoaguirre.com
|
@@ -73,6 +73,7 @@ executables: []
|
|
73
73
|
extensions: []
|
74
74
|
extra_rdoc_files: []
|
75
75
|
files:
|
76
|
+
- .travis.yml
|
76
77
|
- Gemfile
|
77
78
|
- README.md
|
78
79
|
- Rakefile
|