sf6 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/Gemfile +3 -0
  2. data/Gemfile.lock +14 -0
  3. data/lib/sf6.rb +58 -0
  4. metadata +47 -0
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,14 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ sf6 (0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+
10
+ PLATFORMS
11
+ ruby
12
+
13
+ DEPENDENCIES
14
+ sf6!
@@ -0,0 +1,58 @@
1
+ module SF6
2
+
3
+ class Unavailable < StandardError
4
+ end
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
16
+
17
+ def self.test(data, attr, value, &blk)
18
+ if value == 0 || data[:counts][attr] < value
19
+ yield
20
+ else
21
+ raise(Unavailable, attr)
22
+ end
23
+ end
24
+
25
+ module Limiter
26
+
27
+ def self.data
28
+ @data ||= {counts: Hash.new(0), timestamps: Hash.new(0)}
29
+ end
30
+
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
35
+ yield
36
+ end
37
+ end
38
+ end
39
+
40
+ module Breaker
41
+
42
+ def self.data
43
+ @data ||= {counts: Hash.new(0), timestamps: Hash.new(0)}
44
+ end
45
+
46
+ def self.check(attr, value, bucket, &blk)
47
+ timestamp = SF6.stamp(data, attr, bucket)
48
+ SF6.test(data, attr, value) do
49
+ begin
50
+ yield
51
+ rescue => e
52
+ SF6.increment(data, attr, timestamp)
53
+ raise
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sf6
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mark Fine
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-10 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Circuit breaker, Rate limiter.
15
+ email: mark.fine@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/sf6.rb
21
+ - Gemfile
22
+ - Gemfile.lock
23
+ homepage: http://github.com/mfine/sf6
24
+ licenses: []
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.24
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: breaker
47
+ test_files: []