pooled-curb 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Angel
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,28 @@
1
+ = pooled-curb
2
+
3
+ High-performance HTTP requests for a multi-threaded environment.
4
+
5
+ When consuming web services, HTTP performance can be greatly improved if you enable keep-alive
6
+ and the right HTTP library.
7
+
8
+ But reusing HTTP connections can be challenging in a multi-threaded environment, where you want
9
+ to reuse connections created from another thread, or on a single-threaded environment where several
10
+ code points connect to the same host.
11
+
12
+ Pooled-curb helps to solve this implementing a pool of Curb (libcurl) objects and providing a
13
+ pleasent API to do HTTP requests through the pool.
14
+
15
+
16
+ == Note on Patches/Pull Requests
17
+
18
+ * Fork the project.
19
+ * Make your feature addition or bug fix.
20
+ * Add tests for it. This is important so I don't break it in a
21
+ future version unintentionally.
22
+ * Commit, do not mess with rakefile, version, or history.
23
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
24
+ * Send me a pull request. Bonus points for topic branches.
25
+
26
+ == Copyright
27
+
28
+ Copyright (c) 2010 Angel Faus. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,57 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "pooled-curb"
8
+ gem.summary = %Q{High-performance HTTP requests for a multi-threaded environment}
9
+ gem.description = %Q{When consuming web services, HTTP performance can be greatly improved if you enable keep-alive
10
+ and the right HTTP library.\nPooled-curb helps to solve this implementing a pool of Curb (libcurl) objects and providing a
11
+ pleasent API to do HTTP requests through the pool.}
12
+ gem.email = "angel@vlex.com"
13
+ gem.homepage = "http://github.com/angelf/pooled-curb"
14
+ gem.authors = ["Angel Faus"]
15
+ gem.add_dependency "common-pool", ">= 0"
16
+ gem.add_dependency "curb", ">= 0"
17
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
18
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
19
+ end
20
+ Jeweler::GemcutterTasks.new
21
+ rescue LoadError
22
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
23
+ end
24
+
25
+ require 'rake/testtask'
26
+ Rake::TestTask.new(:test) do |test|
27
+ test.libs << 'lib' << 'test'
28
+ test.pattern = 'test/**/test_*.rb'
29
+ test.verbose = true
30
+ end
31
+
32
+ begin
33
+ require 'rcov/rcovtask'
34
+ Rcov::RcovTask.new do |test|
35
+ test.libs << 'test'
36
+ test.pattern = 'test/**/test_*.rb'
37
+ test.verbose = true
38
+ end
39
+ rescue LoadError
40
+ task :rcov do
41
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
42
+ end
43
+ end
44
+
45
+ task :test => :check_dependencies
46
+
47
+ task :default => :test
48
+
49
+ require 'rake/rdoctask'
50
+ Rake::RDocTask.new do |rdoc|
51
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
52
+
53
+ rdoc.rdoc_dir = 'rdoc'
54
+ rdoc.title = "pooled-curb #{version}"
55
+ rdoc.rdoc_files.include('README*')
56
+ rdoc.rdoc_files.include('lib/**/*.rb')
57
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,113 @@
1
+ require 'common_pool'
2
+ require 'curb'
3
+ require 'pooled_curb_datasource'
4
+
5
+ # wrapper on top of curb to ensure we reuse the Curl objects, and thus make sure we
6
+ # benefit from keep-alive
7
+ #
8
+ # Usage:
9
+ #
10
+ # PooledCurb.get(url)
11
+ #
12
+ # PooledCurb.post(url, {:hash => 'of', :several => 'parameters})
13
+ # PooledCurb.post(url, request_body)
14
+ #
15
+ # PooledCurb.put(url, request_body)
16
+
17
+ class PooledCurb
18
+
19
+ #NOTE: curb documentation lives in http://rubydoc.info/github/taf2/curb/
20
+
21
+ HTTP_CLIENT_POOL = CommonPool::ObjectPool.new(PooledCurbDataSource.new) {|config| config.max_active = 200 }
22
+
23
+ def self.with_client
24
+ curl = HTTP_CLIENT_POOL.borrow_object
25
+ begin
26
+ yield curl
27
+ rescue Curl::Err::MultiBadEasyHandle
28
+ # obtener una nueva
29
+ curl = HTTP_CLIENT_POOL.borrow_object
30
+ retry
31
+ ensure
32
+ HTTP_CLIENT_POOL.return_object(curl)
33
+ end
34
+ end
35
+
36
+ # get the URL, returns body_str, responde_code
37
+ def self.get(url, timeout = 20)
38
+ with_client do |curl|
39
+ curl.timeout = timeout
40
+ curl.url = url
41
+ curl.http("GET")
42
+ return curl.body_str, curl.response_code
43
+ end
44
+ end
45
+
46
+ # get the URL, retrying upto max_retries times if an error is found.
47
+ #
48
+ # When a code block is given, it will be used as a validation function
49
+ # for the response, otherwise and standard validation that tests that response code is 200
50
+ # and the body is not nil will be used
51
+ #
52
+ # for example
53
+ #
54
+ # PooledCurb.get_with_retry(url) {|body, code| body !~ /ERROR/}
55
+
56
+ def self.get_with_retry(url, timeout = 20, max_retries = 5)
57
+
58
+ body = nil
59
+
60
+ max_retries.times do |ix_try|
61
+
62
+ # If the target server is down for a very short time, a very short sleep after the third failure
63
+ # will help us recover the error
64
+ Kernel.sleep(0.2) if ix_try > 3
65
+
66
+ begin
67
+ body, response_code = get(url, timeout)
68
+
69
+ if block_given?
70
+ next unless yield body, response_code
71
+ else
72
+ next unless response_code == 200 && body.any?
73
+ end
74
+
75
+ return body
76
+ rescue Curl::Err::CurlError
77
+ # will retry
78
+ end
79
+ end
80
+
81
+ body
82
+ end
83
+
84
+ # post to the URL, returns body_str, responde_code
85
+ #
86
+ # can accept a hash of parameters or a string containing the request body
87
+
88
+ def self.post(url, params_or_body = {}, timeout = 20)
89
+ with_client do |curl|
90
+ curl.timeout = timeout
91
+ curl.url = url
92
+ if params_or_body.kind_of?(String)
93
+ curl.http_post url, params_or_body
94
+ else
95
+ params = params_or_body
96
+ fields = params.keys.map{|param_name| Curl::PostField.content(param_name, params[param_name]) }
97
+ curl.http_post url, *fields
98
+ end
99
+ return curl.body_str, curl.response_code
100
+ end
101
+ end
102
+
103
+ # put to the URL, returns body_str, responde_code
104
+ def self.put(url, request_body, timeout = 20)
105
+ with_client do |curl|
106
+ curl.timeout = timeout
107
+ curl.url = url
108
+ curl.http_put request_body
109
+ return curl.body_str, curl.response_code
110
+ end
111
+ end
112
+
113
+ end
@@ -0,0 +1,20 @@
1
+ # PooledCurbDataSource
2
+ #
3
+ # obtains http clients (Curl::Easy objects) through a pool.
4
+ #
5
+ # esto es importante porque si reutilizamos los clientes HTTP (curb) tenemos alguna
6
+ #
7
+ # usage:
8
+ # http_client_pool = CommonPool::ObjectPool.new(PooledCurbDataSource.new)
9
+ # curb_client = http_client_pool.borrow_object
10
+ # ..
11
+ # http_client_pool.return_object(curb_client)
12
+ #
13
+ # see:
14
+ # http://github.com/jugend/common-pool
15
+
16
+ class PooledCurbDataSource < CommonPool::PoolDataSource
17
+ def create_object
18
+ Curl::Easy.new
19
+ end
20
+ end
@@ -0,0 +1,64 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{pooled-curb}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Angel Faus"]
12
+ s.date = %q{2010-09-29}
13
+ s.description = %q{When consuming web services, HTTP performance can be greatly improved if you enable keep-alive
14
+ and the right HTTP library.
15
+ Pooled-curb helps to solve this implementing a pool of Curb (libcurl) objects and providing a
16
+ pleasent API to do HTTP requests through the pool.}
17
+ s.email = %q{angel@vlex.com}
18
+ s.extra_rdoc_files = [
19
+ "LICENSE",
20
+ "README.rdoc"
21
+ ]
22
+ s.files = [
23
+ ".document",
24
+ ".gitignore",
25
+ "LICENSE",
26
+ "README.rdoc",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "lib/pooled-curb.rb",
30
+ "lib/pooled_curb_datasource.rb",
31
+ "pooled-curb.gemspec",
32
+ "test/helper.rb",
33
+ "test/test_pooled-curb.rb"
34
+ ]
35
+ s.homepage = %q{http://github.com/angelf/pooled-curb}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.6}
39
+ s.summary = %q{High-performance HTTP requests for a multi-threaded environment}
40
+ s.test_files = [
41
+ "test/helper.rb",
42
+ "test/test_pooled-curb.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 3
48
+
49
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
50
+ s.add_runtime_dependency(%q<common-pool>, [">= 0"])
51
+ s.add_runtime_dependency(%q<curb>, [">= 0"])
52
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
53
+ else
54
+ s.add_dependency(%q<common-pool>, [">= 0"])
55
+ s.add_dependency(%q<curb>, [">= 0"])
56
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
57
+ end
58
+ else
59
+ s.add_dependency(%q<common-pool>, [">= 0"])
60
+ s.add_dependency(%q<curb>, [">= 0"])
61
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
62
+ end
63
+ end
64
+
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'pooled-curb'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestPooledCurb < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pooled-curb
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Angel Faus
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-09-29 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: common-pool
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ name: curb
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ segments:
40
+ - 0
41
+ version: "0"
42
+ type: :runtime
43
+ version_requirements: *id002
44
+ - !ruby/object:Gem::Dependency
45
+ name: thoughtbot-shoulda
46
+ prerelease: false
47
+ requirement: &id003 !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ segments:
52
+ - 0
53
+ version: "0"
54
+ type: :development
55
+ version_requirements: *id003
56
+ description: |-
57
+ When consuming web services, HTTP performance can be greatly improved if you enable keep-alive
58
+ and the right HTTP library.
59
+ Pooled-curb helps to solve this implementing a pool of Curb (libcurl) objects and providing a
60
+ pleasent API to do HTTP requests through the pool.
61
+ email: angel@vlex.com
62
+ executables: []
63
+
64
+ extensions: []
65
+
66
+ extra_rdoc_files:
67
+ - LICENSE
68
+ - README.rdoc
69
+ files:
70
+ - .document
71
+ - .gitignore
72
+ - LICENSE
73
+ - README.rdoc
74
+ - Rakefile
75
+ - VERSION
76
+ - lib/pooled-curb.rb
77
+ - lib/pooled_curb_datasource.rb
78
+ - pooled-curb.gemspec
79
+ - test/helper.rb
80
+ - test/test_pooled-curb.rb
81
+ has_rdoc: true
82
+ homepage: http://github.com/angelf/pooled-curb
83
+ licenses: []
84
+
85
+ post_install_message:
86
+ rdoc_options:
87
+ - --charset=UTF-8
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ segments:
95
+ - 0
96
+ version: "0"
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ segments:
102
+ - 0
103
+ version: "0"
104
+ requirements: []
105
+
106
+ rubyforge_project:
107
+ rubygems_version: 1.3.6
108
+ signing_key:
109
+ specification_version: 3
110
+ summary: High-performance HTTP requests for a multi-threaded environment
111
+ test_files:
112
+ - test/helper.rb
113
+ - test/test_pooled-curb.rb