gateway 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +14 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +29 -0
- data/Rakefile +42 -0
- data/VERSION +1 -0
- data/lib/gateway/base.rb +70 -0
- data/lib/gateway/connection/pool.rb +27 -0
- data/lib/gateway/connection/single.rb +20 -0
- data/lib/gateway/connection/thread_local.rb +24 -0
- data/lib/gateway/connection.rb +34 -0
- data/lib/gateway/errors.rb +39 -0
- data/lib/gateway/feature/categorize_error.rb +82 -0
- data/lib/gateway/feature/error_handle.rb +39 -0
- data/lib/gateway/feature/new_relic.rb +21 -0
- data/lib/gateway/feature/performance.rb +53 -0
- data/lib/gateway/feature/retry.rb +41 -0
- data/lib/gateway/feature/timeout.rb +18 -0
- data/lib/gateway.rb +21 -0
- data/spec/gateway_spec.rb +5 -0
- data/spec/spec_helper.rb +12 -0
- metadata +149 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
|
3
|
+
gem "activesupport", "~> 3.0"
|
4
|
+
gem "resource_pool", ">= 0.2.0"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "rspec", "~> 2.8.0"
|
10
|
+
gem "yard", "~> 0.7"
|
11
|
+
gem "rdoc", "~> 3.12"
|
12
|
+
gem "bundler", "~> 1.0.0"
|
13
|
+
gem "jeweler", "~> 1.8.3"
|
14
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Aaron Qian
|
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,29 @@
|
|
1
|
+
= gateway
|
2
|
+
|
3
|
+
Gateway is an opinionated generic IO connection manager. It is created to be a manager / wrapper that can provide a uniform behavior across various IO connection types such as HTTP, Memcache, database, or even generic TCP / UDP protocols.
|
4
|
+
|
5
|
+
This gem provides a `Gateway::Base` class for other connection types to be built upon. It provides the following features:
|
6
|
+
|
7
|
+
* Connection Management. Choose from single, connection pooling, or thread local.
|
8
|
+
* Error Transform / Handling.
|
9
|
+
* Timeouts
|
10
|
+
* Automatic retry
|
11
|
+
* performance logging
|
12
|
+
* Newrelic instrumentation. ( If newrelic is loaded. )
|
13
|
+
|
14
|
+
|
15
|
+
== Contributing to gateway
|
16
|
+
|
17
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
18
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
19
|
+
* Fork the project.
|
20
|
+
* Start a feature/bugfix branch.
|
21
|
+
* Commit and push until you are happy with your contribution.
|
22
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
23
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
24
|
+
|
25
|
+
== Copyright
|
26
|
+
|
27
|
+
Copyright (c) 2012 Aaron Qian. See LICENSE.txt for
|
28
|
+
further details.
|
29
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "gateway"
|
18
|
+
gem.homepage = "http://github.com/aq1018/gateway"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{ Opinionated Generic IO Connection Manager }
|
21
|
+
gem.description = %Q{ Opinionated Generic IO Connection Manager }
|
22
|
+
gem.email = "aq1018@gmail.com"
|
23
|
+
gem.authors = ["Aaron Qian"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rspec/core'
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
+
end
|
33
|
+
|
34
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
35
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
36
|
+
spec.rcov = true
|
37
|
+
end
|
38
|
+
|
39
|
+
task :default => :spec
|
40
|
+
|
41
|
+
require 'yard'
|
42
|
+
YARD::Rake::YardocTask.new
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/gateway/base.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
module Gateway
|
2
|
+
class Base
|
3
|
+
include Gateway::Feature::NewRelic
|
4
|
+
include Gateway::Feature::Performance
|
5
|
+
include Gateway::Feature::Timeout
|
6
|
+
include Gateway::Feature::Retry
|
7
|
+
include Gateway::Feature::ErrorHandle
|
8
|
+
include Gateway::Feature::CategorizeError
|
9
|
+
include Gateway::Connection
|
10
|
+
|
11
|
+
categorize_error SystemCallError, SocketError, IOError,
|
12
|
+
:as => :bad_gateway
|
13
|
+
|
14
|
+
categorize_error Timeout::Error,
|
15
|
+
:as => :timeout
|
16
|
+
|
17
|
+
categorize_error SystemCallError, SocketError, IOError,
|
18
|
+
:as => :retry
|
19
|
+
|
20
|
+
attr_reader :options, :name
|
21
|
+
|
22
|
+
def initialize(name, opts)
|
23
|
+
@name = name
|
24
|
+
@options = opts
|
25
|
+
end
|
26
|
+
|
27
|
+
protected
|
28
|
+
|
29
|
+
def bad_gateway_errors(action)
|
30
|
+
errors_for(action, :bad_gateway)
|
31
|
+
end
|
32
|
+
|
33
|
+
def timeout_errors(action)
|
34
|
+
errors_for(action, :timeout)
|
35
|
+
end
|
36
|
+
|
37
|
+
def retry_errors(action)
|
38
|
+
errors_for(action, :retry)
|
39
|
+
end
|
40
|
+
|
41
|
+
def run_bad_gateway_callbacks(action)
|
42
|
+
run_callbacks_for(action, :bad_gateway)
|
43
|
+
end
|
44
|
+
|
45
|
+
def run_timeout_callbacks(action)
|
46
|
+
run_callbacks_for(action, :timeout)
|
47
|
+
end
|
48
|
+
|
49
|
+
def run_retry_callbacks(action)
|
50
|
+
run_callbacks_for(action, :retry)
|
51
|
+
end
|
52
|
+
|
53
|
+
def execute(action, req, opts={}, &block)
|
54
|
+
with_connection(opts) do |conn|
|
55
|
+
with_retry(action, opts) do
|
56
|
+
with_perf(action, req, opts) do
|
57
|
+
with_error_handle(action, conn, opts) do
|
58
|
+
with_new_relic(opts) do
|
59
|
+
with_timeout(opts) do
|
60
|
+
block.call(conn)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Gateway
|
2
|
+
module Connection
|
3
|
+
module Pool
|
4
|
+
protected
|
5
|
+
|
6
|
+
def with_pool(&block)
|
7
|
+
pool.hold do |conn|
|
8
|
+
block.call(conn)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def purge_current_connection_from_pool!
|
13
|
+
pool.trash_current!
|
14
|
+
end
|
15
|
+
|
16
|
+
def pool
|
17
|
+
@pool ||= ResourcePool.new(pool_options) { connect }
|
18
|
+
end
|
19
|
+
|
20
|
+
def pool_options
|
21
|
+
@pool_options ||= {
|
22
|
+
:delete_proc => lambda{ |conn| disconnect conn }
|
23
|
+
}.merge(options[:pool])
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Gateway
|
2
|
+
module Connection
|
3
|
+
module Single
|
4
|
+
protected
|
5
|
+
def with_single(&block)
|
6
|
+
conn = connect
|
7
|
+
begin
|
8
|
+
block.call conn
|
9
|
+
ensure
|
10
|
+
disconnect(conn)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def purge_current_connection_from_single!
|
15
|
+
# This is intentionally empty.
|
16
|
+
# Do not remove this method.
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Gateway
|
2
|
+
module Connection
|
3
|
+
module ThreadLocal
|
4
|
+
protected
|
5
|
+
|
6
|
+
def with_thread_local(&block)
|
7
|
+
conn = thread_local_connection
|
8
|
+
block.call conn
|
9
|
+
end
|
10
|
+
|
11
|
+
def purge_current_connection_from_thread_local!
|
12
|
+
Thread.current[thread_local_connection_name] = nil
|
13
|
+
end
|
14
|
+
|
15
|
+
def thread_local_connection_name
|
16
|
+
"#{self.class.name}:#{self.name}"
|
17
|
+
end
|
18
|
+
|
19
|
+
def thread_local_connection
|
20
|
+
Thread.current[thread_local_connection_name] ||= connect
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Gateway
|
2
|
+
module Connection
|
3
|
+
include Single
|
4
|
+
include ThreadLocal
|
5
|
+
include Pool
|
6
|
+
|
7
|
+
protected
|
8
|
+
|
9
|
+
def connect
|
10
|
+
raise "Abstract Method"
|
11
|
+
end
|
12
|
+
|
13
|
+
def disconnect(conn)
|
14
|
+
raise "Abstract Method"
|
15
|
+
end
|
16
|
+
|
17
|
+
def reconnect(conn)
|
18
|
+
raise "Abstract Method"
|
19
|
+
end
|
20
|
+
|
21
|
+
def purge_current_connection!
|
22
|
+
send "purge_current_connection_from_#{connection_type}!"
|
23
|
+
end
|
24
|
+
|
25
|
+
def connection_type
|
26
|
+
@connection_type ||= (options[:connection_type] || :thread_local)
|
27
|
+
end
|
28
|
+
|
29
|
+
def with_connection(opts={}, &block)
|
30
|
+
return with_single(&block) if opts[:persistent] == false
|
31
|
+
send "with_#{connection_type}", &block
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Gateway
|
2
|
+
class Error < RuntimeError
|
3
|
+
attr_accessor :status, :inner_error
|
4
|
+
def self.wrap(error)
|
5
|
+
e = self.new("#{self.name} - #{error.class.name}: #{error.message}")
|
6
|
+
e.inner_error = error
|
7
|
+
e
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
# When backend stops responding within given time.
|
12
|
+
class GatewayTimeout < Error
|
13
|
+
def initialize(msg)
|
14
|
+
super
|
15
|
+
self.status = "504"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# When backend did not send back an (processible) response.
|
20
|
+
# Usually when connection is dropped.
|
21
|
+
class BadGateway < Error
|
22
|
+
def initialize(msg)
|
23
|
+
super
|
24
|
+
self.status = "502"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# When a response is returned, but is considered unsuccessful.
|
29
|
+
# This is application specific.
|
30
|
+
# For HTTP responses: 4xx, 5xx can be considered bad response
|
31
|
+
class BadResponse < BadGateway
|
32
|
+
attr_accessor :info
|
33
|
+
def initialize(msg, info=nil)
|
34
|
+
msg = "#{msg}\nInfo:\n#{info.inspect}" if info
|
35
|
+
super msg
|
36
|
+
self.info = info
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module Gateway
|
2
|
+
module Feature
|
3
|
+
module CategorizeError
|
4
|
+
def self.included(klass)
|
5
|
+
klass.class_eval do
|
6
|
+
class_attribute :error_catalog, :error_categories
|
7
|
+
self.error_catalog = {}
|
8
|
+
self.error_categories = [:bad_gateway, :timeout, :retry]
|
9
|
+
|
10
|
+
extend ClassMethods
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
protected
|
15
|
+
|
16
|
+
def errors_for(action, error_category)
|
17
|
+
cat = self.error_catalog[error_category]
|
18
|
+
return [] if cat.blank?
|
19
|
+
|
20
|
+
errors = cat[action] ? cat[action][:errors] : []
|
21
|
+
default_errors = cat[:all][:errors] || []
|
22
|
+
errors + default_errors
|
23
|
+
end
|
24
|
+
|
25
|
+
def error_callbacks_for(action, error_category)
|
26
|
+
cat = self.error_catalog[error_category]
|
27
|
+
return [] if cat.blank?
|
28
|
+
|
29
|
+
lambdas = cat[action] ? cat[action][:lambdas] : []
|
30
|
+
default_lambdas = cat[:all][:lambdas] || []
|
31
|
+
lambdas + default_lambdas
|
32
|
+
end
|
33
|
+
|
34
|
+
def run_callbacks_for(action, error_category)
|
35
|
+
error_callbacks_for(action, error_category).each do |cb|
|
36
|
+
cb.call self
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
module ClassMethods
|
41
|
+
protected
|
42
|
+
|
43
|
+
def categorize_error(*params, &block)
|
44
|
+
errors, actions, categories = extract_params(params)
|
45
|
+
|
46
|
+
self.error_catalog ||= {}
|
47
|
+
|
48
|
+
categories.each do |error_category|
|
49
|
+
self.error_catalog[error_category] ||= {}
|
50
|
+
actions.each do |action|
|
51
|
+
self.error_catalog[error_category][action] ||= {}
|
52
|
+
self.error_catalog[error_category][action][:errors] ||= []
|
53
|
+
self.error_catalog[error_category][action][:errors] += errors
|
54
|
+
self.error_catalog[error_category][action][:lambdas] ||= []
|
55
|
+
self.error_catalog[error_category][action][:lambdas] << block if block
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def extract_params(params)
|
61
|
+
opt = params.pop
|
62
|
+
opt ||= {}
|
63
|
+
errors = params.flatten
|
64
|
+
|
65
|
+
actions = [opt[:for]].flatten.compact
|
66
|
+
actions = [:all] if actions.blank?
|
67
|
+
|
68
|
+
categories = [opt[:as]].flatten.compact
|
69
|
+
categories = error_categories if categories.blank?
|
70
|
+
|
71
|
+
categories.each do |error_category|
|
72
|
+
unless error_categories.include?(error_category.to_sym)
|
73
|
+
raise "Error Category: #{error_category} must be one of #{error_categories}."
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
[errors, actions, categories]
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Gateway
|
2
|
+
module Feature
|
3
|
+
module ErrorHandle
|
4
|
+
protected
|
5
|
+
|
6
|
+
def with_error_handle(action, conn, opts={}, &block)
|
7
|
+
return block.call if opts[:handle_error] == false
|
8
|
+
|
9
|
+
begin
|
10
|
+
block.call
|
11
|
+
rescue *bad_gateway_errors(action) => e
|
12
|
+
run_bad_gateway_callbacks(action)
|
13
|
+
reconnect(conn)
|
14
|
+
raise Gateway::BadGateway.wrap(e)
|
15
|
+
rescue *timeout_errors(action) => e
|
16
|
+
run_timeout_callbacks(action)
|
17
|
+
reconnect(conn)
|
18
|
+
raise Gateway::GatewayTimeout.wrap(e)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def bad_gateway_errors(action)
|
23
|
+
raise "Abstract Method"
|
24
|
+
end
|
25
|
+
|
26
|
+
def timeout_errors(action)
|
27
|
+
raise "Abstract Method"
|
28
|
+
end
|
29
|
+
|
30
|
+
def run_bad_gateway_callbacks(action)
|
31
|
+
raise "Abstract Method"
|
32
|
+
end
|
33
|
+
|
34
|
+
def run_timeout_callbacks(action)
|
35
|
+
raise "Abstract Method"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Gateway
|
2
|
+
module Feature
|
3
|
+
module NewRelic
|
4
|
+
protected
|
5
|
+
|
6
|
+
def with_new_relic(opts={}, &block)
|
7
|
+
return block.call unless opts.fetch(:record_newrelic, record_newrelic)
|
8
|
+
|
9
|
+
# per newrelic support: ['External/servicename/all', 'External/allWeb']
|
10
|
+
metric_names = ["External/#{name}/all", 'External/allWeb']
|
11
|
+
::NewRelic::Agent::MethodTracer.trace_execution_scoped metric_names do
|
12
|
+
block.call
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def record_newrelic
|
17
|
+
defined?(::NewRelic) && options.fetch(:record_newrelic, false)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Gateway
|
2
|
+
module Feature
|
3
|
+
module Performance
|
4
|
+
def self.included(klass)
|
5
|
+
klass.class_eval do
|
6
|
+
class_attribute :profiler
|
7
|
+
end if klass.is_a?(Class)
|
8
|
+
end
|
9
|
+
|
10
|
+
def profiler
|
11
|
+
@profiler ||= (options[:profiler] || self.class.profiler)
|
12
|
+
end
|
13
|
+
|
14
|
+
protected
|
15
|
+
|
16
|
+
def with_perf(action, req, opts={}, &block)
|
17
|
+
return block.call if opts[:perf] == false
|
18
|
+
|
19
|
+
status = success_status
|
20
|
+
desc = success_message
|
21
|
+
start_time = Time.now
|
22
|
+
|
23
|
+
begin
|
24
|
+
block.call
|
25
|
+
rescue => e
|
26
|
+
status = error_status(e)
|
27
|
+
desc = error_message(e)
|
28
|
+
raise
|
29
|
+
ensure
|
30
|
+
duration = Time.now - start_time
|
31
|
+
req = "#{action.to_s.upcase} #{req}"
|
32
|
+
profiler.performance(name.to_s, duration, status, desc, req)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def success_status
|
37
|
+
200
|
38
|
+
end
|
39
|
+
|
40
|
+
def success_message
|
41
|
+
"OK"
|
42
|
+
end
|
43
|
+
|
44
|
+
def error_status(error)
|
45
|
+
error.respond_to?(:status) ? error.status : 500
|
46
|
+
end
|
47
|
+
|
48
|
+
def error_message(error)
|
49
|
+
"#{error.class.name} - #{error.message}"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Gateway
|
2
|
+
module Feature
|
3
|
+
module Retry
|
4
|
+
protected
|
5
|
+
|
6
|
+
def with_retry(action, opts={}, &block)
|
7
|
+
return block.call unless opts.fetch(:retry, retry?)
|
8
|
+
|
9
|
+
retried = false
|
10
|
+
begin
|
11
|
+
block.call
|
12
|
+
rescue => e
|
13
|
+
if !retried && retry_error?(action, e)
|
14
|
+
run_retry_callbacks(action)
|
15
|
+
retried = true
|
16
|
+
retry
|
17
|
+
end
|
18
|
+
raise
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def retry_error?(action, error)
|
23
|
+
error = error.inner_error if error.is_a?(Gateway::Error)
|
24
|
+
errors = retry_errors(action)
|
25
|
+
errors.any?{ |klass| error.is_a?(klass) }
|
26
|
+
end
|
27
|
+
|
28
|
+
def retry_errors(action)
|
29
|
+
raise "Abstract Method"
|
30
|
+
end
|
31
|
+
|
32
|
+
def run_retry_callbacks(action)
|
33
|
+
raise "Abstract Method"
|
34
|
+
end
|
35
|
+
|
36
|
+
def retry?
|
37
|
+
@retry ||= options.fetch(:retry, true)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Gateway
|
2
|
+
module Feature
|
3
|
+
module Timeout
|
4
|
+
DEFAULT_TIMEOUT = 2
|
5
|
+
|
6
|
+
def timeout
|
7
|
+
@timeout ||= (options[:timeout] || Gateway::Feature::Timeout::DEFAULT_TIMEOUT)
|
8
|
+
end
|
9
|
+
|
10
|
+
protected
|
11
|
+
|
12
|
+
def with_timeout(opts={}, &block)
|
13
|
+
return block.call if opts[:timeout] == false
|
14
|
+
::Timeout.timeout(opts[:timeout] || timeout){ block.call }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/gateway.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'active_support/core_ext/object/blank'
|
2
|
+
require 'active_support/core_ext/class/attribute'
|
3
|
+
require 'timeout'
|
4
|
+
require 'net/http'
|
5
|
+
|
6
|
+
require 'gateway/errors'
|
7
|
+
|
8
|
+
require 'gateway/feature/retry'
|
9
|
+
require 'gateway/feature/new_relic'
|
10
|
+
require 'gateway/feature/timeout'
|
11
|
+
require 'gateway/feature/error_handle'
|
12
|
+
require 'gateway/feature/categorize_error'
|
13
|
+
require 'gateway/feature/performance'
|
14
|
+
|
15
|
+
require 'gateway/connection/pool'
|
16
|
+
require 'gateway/connection/thread_local'
|
17
|
+
require 'gateway/connection/single'
|
18
|
+
require 'gateway/connection'
|
19
|
+
|
20
|
+
require 'gateway/base'
|
21
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'gateway'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gateway
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Aaron Qian
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-02-10 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: &20040260 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *20040260
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: resource_pool
|
27
|
+
requirement: &20039500 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 0.2.0
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *20039500
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rspec
|
38
|
+
requirement: &20038340 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 2.8.0
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *20038340
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: yard
|
49
|
+
requirement: &20037640 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.7'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *20037640
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: rdoc
|
60
|
+
requirement: &20037020 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ~>
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '3.12'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *20037020
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: bundler
|
71
|
+
requirement: &20036340 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ~>
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: 1.0.0
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *20036340
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: jeweler
|
82
|
+
requirement: &20035680 !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ~>
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: 1.8.3
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: *20035680
|
91
|
+
description: ! ' Opinionated Generic IO Connection Manager '
|
92
|
+
email: aq1018@gmail.com
|
93
|
+
executables: []
|
94
|
+
extensions: []
|
95
|
+
extra_rdoc_files:
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.rdoc
|
98
|
+
files:
|
99
|
+
- .document
|
100
|
+
- .rspec
|
101
|
+
- Gemfile
|
102
|
+
- LICENSE.txt
|
103
|
+
- README.rdoc
|
104
|
+
- Rakefile
|
105
|
+
- VERSION
|
106
|
+
- lib/gateway.rb
|
107
|
+
- lib/gateway/base.rb
|
108
|
+
- lib/gateway/connection.rb
|
109
|
+
- lib/gateway/connection/pool.rb
|
110
|
+
- lib/gateway/connection/single.rb
|
111
|
+
- lib/gateway/connection/thread_local.rb
|
112
|
+
- lib/gateway/errors.rb
|
113
|
+
- lib/gateway/feature/categorize_error.rb
|
114
|
+
- lib/gateway/feature/error_handle.rb
|
115
|
+
- lib/gateway/feature/new_relic.rb
|
116
|
+
- lib/gateway/feature/performance.rb
|
117
|
+
- lib/gateway/feature/retry.rb
|
118
|
+
- lib/gateway/feature/timeout.rb
|
119
|
+
- spec/gateway_spec.rb
|
120
|
+
- spec/spec_helper.rb
|
121
|
+
homepage: http://github.com/aq1018/gateway
|
122
|
+
licenses:
|
123
|
+
- MIT
|
124
|
+
post_install_message:
|
125
|
+
rdoc_options: []
|
126
|
+
require_paths:
|
127
|
+
- lib
|
128
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
130
|
+
requirements:
|
131
|
+
- - ! '>='
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
segments:
|
135
|
+
- 0
|
136
|
+
hash: -1877491257505437583
|
137
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
138
|
+
none: false
|
139
|
+
requirements:
|
140
|
+
- - ! '>='
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: '0'
|
143
|
+
requirements: []
|
144
|
+
rubyforge_project:
|
145
|
+
rubygems_version: 1.8.10
|
146
|
+
signing_key:
|
147
|
+
specification_version: 3
|
148
|
+
summary: Opinionated Generic IO Connection Manager
|
149
|
+
test_files: []
|