mongo-retry 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cfe67d1e2dd3025dab5b0747359076ca339d69ad
4
+ data.tar.gz: 46b973d9d74c40714c486cb1eabf8cd391b45655
5
+ SHA512:
6
+ metadata.gz: 75ebd5a95c9741b9b02efaaa732bea9bc96f10a21827a08708e61ac43bcbfd1656c4e5c93a457326a08d4e50c4c166de8ea05cfc207db8e580628da152c63819
7
+ data.tar.gz: bb03c3e50de804ef8259fcf06ce6d12d692cd8bb2dea947bee1b4c95c21f50c34b30e20eb484264a0c437ad7f12fa6267013696202b3a5badc621be2260da254
data/README.md ADDED
@@ -0,0 +1,84 @@
1
+ # Simple retry error handling on connection errors in for the [Mongo ruby driver](https://github.com/mongodb/mongo-ruby-driver)
2
+
3
+ [![Build Status](https://travis-ci.org/burtcorp/mongo-retry.png?branch=master)](https://travis-ci.org/burtcorp/mongo-retry)
4
+ [![Coverage Status](https://coveralls.io/repos/burtcorp/mongo-retry/badge.png)](https://coveralls.io/r/burtcorp/mongo-retry)
5
+
6
+ In its simplest form
7
+ ```ruby
8
+ require 'mongo'
9
+ require 'mongo-retry'
10
+
11
+ client = Mongo::MongoClient.new
12
+ retryer = Mongo::Retry.new(client)
13
+
14
+ # Will retry after 1, 5, 10 seconds and eventually fail
15
+ # by throwing the exception thrown on the 4th attempt
16
+ doc = retryer.connection_guard { client.db('foo')['bar'].find_one() }
17
+
18
+ ```
19
+
20
+ You may also specify the following optional parameters here with the default values in place.
21
+
22
+ ```ruby
23
+
24
+ retryer = Mongo::Retry.new(client, delayer: Kernel.method(:sleep),
25
+ retries: DEFAULT_RETRY_SLEEPS, # [1, 5, 10]
26
+ exceptions: DEFAULT_RETRYABLE_EXCEPTIONS, # [::Mongo::ConnectionError,::Mongo::ConnectionTimeoutError,::Mongo::ConnectionFailure,::Mongo::OperationTimeout]
27
+ retry_exceptions: DEFAULT_RETRYABLE_EXCEPTIONS, # [::Mongo::ConnectionError,::Mongo::ConnectionTimeoutError,::Mongo::ConnectionFailure,::Mongo::OperationTimeout]
28
+ logger: nil)
29
+
30
+ ```
31
+
32
+ ## A note on logger
33
+
34
+ logger should be a lambda with two arguments
35
+
36
+ ```ruby
37
+ logger = lambda { |reason, e|
38
+ # Where reason may be :retry, :fail, :reconnect_fail
39
+ p reason
40
+ p e
41
+ }
42
+ ```
43
+
44
+ In the default case where mongo would be down the following is expected to be received by the logger
45
+
46
+ ```ruby
47
+
48
+ doc = retryer.connection_guard {
49
+ client.db('foo')['bar'].find_one()
50
+ }
51
+ # Would be identical have the same effect in respect to the logger as
52
+ logger.call(:retry, exception)
53
+ logger.call(:reconnect_fail, exception)
54
+ logger.call(:retry, exception)
55
+ logger.call(:reconnect_fail, exception)
56
+ logger.call(:retry, exception)
57
+ logger.call(:reconnect_fail, exception)
58
+ logger.call(:fail, exception)
59
+
60
+ ```
61
+
62
+
63
+ (The MIT License)
64
+
65
+ Copyright (c) 2011 Burt
66
+
67
+ Permission is hereby granted, free of charge, to any person obtaining
68
+ a copy of this software and associated documentation files (the
69
+ "Software"), to deal in the Software without restriction, including
70
+ without limitation the rights to use, copy, modify, merge, publish,
71
+ distribute, sublicense, and/or sell copies of the Software, and to
72
+ permit persons to whom the Software is furnished to do so, subject to
73
+ the following conditions:
74
+
75
+ The above copyright notice and this permission notice shall be
76
+ included in all copies or substantial portions of the Software.
77
+
78
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
79
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
80
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
81
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
82
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
83
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
84
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,54 @@
1
+ require 'mongo'
2
+
3
+ module Mongo
4
+ class Retry
5
+ DEFAULT_RETRY_SLEEPS = [1, 5, 10].freeze
6
+ DEFAULT_RETRYABLE_EXCEPTIONS = [
7
+ ::Mongo::ConnectionError,
8
+ ::Mongo::ConnectionTimeoutError,
9
+ ::Mongo::ConnectionFailure,
10
+ ::Mongo::OperationTimeout
11
+ ]
12
+
13
+ DEFAULT_OPTIONS = {
14
+ delayer: Kernel.method(:sleep),
15
+ retries: DEFAULT_RETRY_SLEEPS,
16
+ exceptions: DEFAULT_RETRYABLE_EXCEPTIONS,
17
+ retry_exceptions: DEFAULT_RETRYABLE_EXCEPTIONS,
18
+ logger: nil
19
+ }
20
+
21
+ def initialize(connection, options = {})
22
+ @options = DEFAULT_OPTIONS.merge(options)
23
+ @connection = connection
24
+ end
25
+
26
+ def connection_guard(retries = @options[:retries].dup)
27
+ yield
28
+ rescue *@options[:exceptions] => e
29
+ if retry_timeout = retries.pop
30
+ log(:retry, e)
31
+ @options[:delayer].call(retry_timeout)
32
+ reconnect!
33
+ retry
34
+ else
35
+ log(:fail, e)
36
+ raise e
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def log(reason, exception)
43
+ if @options[:logger]
44
+ @options[:logger].call(reason, exception)
45
+ end
46
+ end
47
+
48
+ def reconnect!
49
+ @connection.reconnect
50
+ rescue *@options[:retry_exceptions] => e
51
+ log(:reconnect_fail, e)
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,8 @@
1
+ require 'bundler/setup'
2
+ require 'mongo'
3
+ require_relative '../lib/mongo/retry'
4
+ RSpec.configure do |config|
5
+ config.mock_with :rspec do |c|
6
+ c.syntax = [:should, :expect]
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongo-retry
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Erik Fonselius
8
+ - Sofia Larsson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-09-12 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: mongo-retry helper
15
+ email:
16
+ - fonsan@burtcorp.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - README.md
22
+ - lib/mongo/retry.rb
23
+ - spec/spec_helper.rb
24
+ homepage: http://github.com/burtcorp/mongo-retry
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 1.9.3
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.2.2
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: mongo-retry helper
48
+ test_files:
49
+ - spec/spec_helper.rb