sf6 0.2 → 0.3

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.
Files changed (2) hide show
  1. data/lib/sf6.rb +36 -28
  2. metadata +2 -2
data/lib/sf6.rb CHANGED
@@ -3,53 +3,61 @@ module SF6
3
3
  class Unavailable < StandardError
4
4
  end
5
5
 
6
- def self.stamp(data, attr, bucket)
7
- timestamp = Time.now.to_i / bucket
8
- data[:counts][attr] = 0 unless data[:timestamps][attr] == timestamp
9
- timestamp
10
- end
11
-
12
- def self.increment(data, attr, timestamp)
13
- data[:timestamps][attr] = timestamp
14
- data[:counts][attr] += 1
15
- end
6
+ module Base
16
7
 
17
- def self.test(data, attr, value, &blk)
18
- if value == 0 || data[:counts][attr] < value
19
- yield
20
- else
21
- raise(Unavailable, attr)
8
+ def reset!(attr)
9
+ data[:timestamps][attr] = 0
10
+ data[:counts][attr] = 0
22
11
  end
23
- end
24
12
 
25
- module Limiter
13
+ private
26
14
 
27
- def self.data
15
+ def data
28
16
  @data ||= {:counts => Hash.new(0), :timestamps => Hash.new(0)}
29
17
  end
30
18
 
31
- def self.check(attr, value, bucket, &blk)
32
- timestamp = SF6.stamp(data, attr, bucket)
33
- SF6.increment(data, attr, timestamp)
34
- SF6.test(data, attr, value) do
19
+ def stamp(attr, bucket)
20
+ timestamp = Time.now.to_i / bucket
21
+ data[:counts][attr] = 0 unless data[:timestamps][attr] == timestamp
22
+ timestamp
23
+ end
24
+
25
+ def increment(attr, timestamp)
26
+ data[:timestamps][attr] = timestamp
27
+ data[:counts][attr] += 1
28
+ end
29
+
30
+ def test(attr, value, &blk)
31
+ if value == 0 || data[:counts][attr] < value
35
32
  yield
33
+ else
34
+ raise(Unavailable, attr)
36
35
  end
37
36
  end
38
37
  end
39
38
 
40
- module Breaker
39
+ module Limiter
40
+ extend Base
41
41
 
42
- def self.data
43
- @data ||= {:counts => Hash.new(0), :timestamps => Hash.new(0)}
42
+ def self.check(attr, value, bucket, &blk)
43
+ timestamp = stamp(attr, bucket)
44
+ increment(attr, timestamp)
45
+ test(attr, value) do
46
+ yield
47
+ end
44
48
  end
49
+ end
50
+
51
+ module Breaker
52
+ extend Base
45
53
 
46
54
  def self.check(attr, value, bucket, &blk)
47
- timestamp = SF6.stamp(data, attr, bucket)
48
- SF6.test(data, attr, value) do
55
+ timestamp = stamp(attr, bucket)
56
+ test(attr, value) do
49
57
  begin
50
58
  yield
51
59
  rescue => e
52
- SF6.increment(data, attr, timestamp)
60
+ increment(attr, timestamp)
53
61
  raise
54
62
  end
55
63
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sf6
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: '0.3'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-08 00:00:00.000000000 Z
12
+ date: 2013-03-09 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Circuit breaker, Rate limiter.
15
15
  email: mark.fine@gmail.com