backoff 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: e93372cda8a2116f0dace4167a3131fbad40d698
4
- data.tar.gz: f50dce4eaf961a130c13b8f2722d767130c3bf3c
2
+ SHA256:
3
+ metadata.gz: 45f9a9163265dfffac8ccf2c29570c753ac8957dcb1b936f14ea371ab4ef40e8
4
+ data.tar.gz: 0f21d4ef04ccd63b38d69bc873a7f0aba8c3786351bb7fd895477ff2534a11fe
5
5
  SHA512:
6
- metadata.gz: 45c4bbdc4e4119712a3c1ce297a9c0bfc94127976f5c34664464dbb8c5d0a085fd549bdc65ccf0a090180a5c6af27d65aecd5a50d805ddde2c2e48ac2501dc5d
7
- data.tar.gz: 1a4476bc07c93795841ee23a717be2e30ec1735ec1003fadade3a768fef9a0274d367366c856702000fe31a40f0c00982e05b98e3e11d573ee16eda30204014a
6
+ metadata.gz: 35722d025e81e3c098d2e45c82e6089dce30aaab4395c6f5b884c38b186a5131faa6a89ce86d0c49de479dba0bc03309dd3cf6824343839cff728f867eec766a
7
+ data.tar.gz: c51554f49e3e90821a8992ccf74eb8e53cc8f3f92fffedc859d0a24bde72d9ff1ea29e576549d5df3c0bfb5d2ab090d6847e4d93ac792e002665302f61fe81cb
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- backoff (0.1.1)
4
+ backoff (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # Backoff
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/backoff`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ Add exponential full jitter backoff to any object by proxy
6
4
 
7
5
  ## Installation
8
6
 
@@ -29,21 +27,27 @@ logger = Logger.new($stderr)
29
27
  class Foo
30
28
  class MyError < StandardError; end
31
29
  class MyErrorTwo < StandardError; end
30
+
31
+ def initialize
32
+ @i = 0
33
+ end
34
+
32
35
  def bar
33
- raise MyError if rand < 0.3
34
- raise MyErrorTwo if rand < 0.1
36
+ @i += 1
37
+ raise MyError if @i < 2
38
+ raise MyErrorTwo if @i < 4
35
39
  true
36
40
  end
37
41
  end
38
42
  # The second argument may be a exception class or a list of exception classes
39
43
  foo_with_backoff = Backoff.wrap(Foo.new, [Foo::MyError, Foo::MyErrorTwo], logger, initial_backoff: 1, multiplier: 2)
40
44
  p foo_with_backoff.bar
41
- # E, [2018-06-09T14:37:53.943201 #23986] ERROR -- : Got Foo::MyErrorTwo, sleeping 1
42
- # I, [2018-06-09T14:37:54.943928 #23986] INFO -- : Woke up after Foo::MyErrorTwo retrying again
43
- # E, [2018-06-09T14:37:54.944066 #23986] ERROR -- : Got Foo::MyError, sleeping 2
44
- # I, [2018-06-09T14:37:56.944181 #23986] INFO -- : Woke up after Foo::MyError retrying again
45
- # E, [2018-06-09T14:37:56.944346 #23986] ERROR -- : Got Foo::MyError, sleeping 4
46
- # I, [2018-06-09T14:38:00.947363 #23986] INFO -- : Woke up after Foo::MyError retrying again
45
+ # E, [2018-11-08T10:17:16.199572 #33699] ERROR -- : Got Foo::MyError, sleeping 0.5672605987514756
46
+ # I, [2018-11-08T10:17:17.204727 #33699] INFO -- : Woke up after Foo::MyError retrying again
47
+ # E, [2018-11-08T10:17:17.204903 #33699] ERROR -- : Got Foo::MyErrorTwo, sleeping 1.6799593245880358
48
+ # I, [2018-11-08T10:17:19.205967 #33699] INFO -- : Woke up after Foo::MyErrorTwo retrying again
49
+ # E, [2018-11-08T10:17:19.206074 #33699] ERROR -- : Got Foo::MyErrorTwo, sleeping 3.732148747396218
50
+ # I, [2018-11-08T10:17:23.206237 #33699] INFO -- : Woke up after Foo::MyErrorTwo retrying again
47
51
  # => true
48
52
  ```
49
53
 
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "backoff"
7
- spec.version = "0.1.1"
7
+ spec.version = "0.2.0"
8
8
  spec.authors = ["Fonsan"]
9
9
  spec.email = ["fonsan@gmail.com"]
10
10
 
@@ -12,6 +12,8 @@ class Backoff < Delegator
12
12
  @sleeper = options[:sleeper] || Kernel.method(:sleep)
13
13
  @initial_backoff = options[:initial_backoff] || 1
14
14
  @multiplier = options[:multiplier] || 2
15
+ @random = options[:random] || Random.new
16
+ @jitter = options[:jitter] || lambda {|delay| @random.rand(0..delay.to_f) }
15
17
  end
16
18
 
17
19
  def __getobj__
@@ -25,7 +27,8 @@ class Backoff < Delegator
25
27
  def _with_backoff(backoff = @initial_backoff)
26
28
  yield
27
29
  rescue *@exception_classes => e
28
- @logger.error "Got #{e.class}, sleeping #{backoff}"
30
+ jittered_backoff = @jitter.call(backoff)
31
+ @logger.error "Got #{e.class}, sleeping #{jittered_backoff}"
29
32
  @sleeper.call(backoff)
30
33
  @logger.info "Woke up after #{e.class} retrying again"
31
34
  backoff *= @multiplier
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: backoff
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fonsan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-06-09 00:00:00.000000000 Z
11
+ date: 2018-11-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -92,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
92
  version: '0'
93
93
  requirements: []
94
94
  rubyforge_project:
95
- rubygems_version: 2.6.14
95
+ rubygems_version: 2.7.7
96
96
  signing_key:
97
97
  specification_version: 4
98
98
  summary: Implements exponential backoff when matching exception is caught