ihasa 0.0.2 → 0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9c2f1647dc36aad4a17f01d49748bf40116784c8
4
- data.tar.gz: 03c3f6c7bd66c3a33ee7b2fde4c9bd579ded3fc3
3
+ metadata.gz: 90ccebca3bb8a2ff3b1bce72077560703cca3946
4
+ data.tar.gz: 14f0db80a27e810b1e9fdd62d9f90538dff9f138
5
5
  SHA512:
6
- metadata.gz: 1cfc9af05559770453ce7e4c8d2d5cacdb35851eebdfaa247477f77839cbf9a137e8a844a3baf6d0606eed7d3491b76cc32ec865e4e7d27f27e1f8803c6d957a
7
- data.tar.gz: 61ba1b7ef42a2d06a7d15d3ec252fc7042741b01194edfd63b0e5aea2b74e50a787b520f87d42d8028f87ed7e10a99c83f78e6e65f82f9c062188f6db3d75575
6
+ metadata.gz: e628c3b17377252702081c5e892f517ace37ce2bbf1df37a16b5c7eac549535c996279afd89f5aa5f29ec09fdc9b31f7e216f8b5474ac11af72c910f163149c4
7
+ data.tar.gz: eb82b4c0dac6315b81e253b088b3549de4d3deda3bd0b74662aef13476ddc8aed62f62a5c2a6527891b059006bb5c022b2c892d1940b29494e11937026fc24a9
data/README.md CHANGED
@@ -28,7 +28,7 @@ $ gem install ihasa
28
28
  You can as well include it in you `Gemfile`:
29
29
 
30
30
  ```
31
- gem 'rubocop', require: false
31
+ gem 'ihasa', require: false
32
32
  ```
33
33
 
34
34
  ## Usage
@@ -69,8 +69,60 @@ Please note that there is also a `Ihasa::Bucket#accept?!` method that raise a
69
69
 
70
70
  ### Advanced
71
71
 
72
- Here is some details on the available configuration options of the Ihasa::Bucket
73
- class.
72
+ In this section, you will find some details on the available configuration options of the Ihasa::Bucket
73
+ class, as well a some advices to run lots of Bucket simultaneously.
74
+
75
+ #### Using a lots of different buckets
76
+
77
+ If you want to enforce rate limits by customer, you have to create as many buckets as customers you have.
78
+ Which can be a lot if you are successful ;).
79
+
80
+ To have a lots of buckets in parallel and to avoid resetting your redis namespaces too often, I suggest you
81
+ no longer use the `Ihasa.bucket` method. Instead, you should back-up your buckets with activerecord models
82
+ (for example) and initialize them in a callback runned after the models creation.
83
+
84
+ Example:
85
+
86
+ ```ruby
87
+ class Bucket < ActiveRecord::Base
88
+ attr_accessible :rate, :burst, :prefix
89
+
90
+ def implementation
91
+ @implementation ||= Ihasa::Bucket.new(rate, burst, prefix, $redis)
92
+ end
93
+
94
+ # The Ihasa::Bucket#initialize_redis_namespace set the relevant
95
+ # keys in your redis instance to have a working bucket. Do it
96
+ # only when you create or update your bucket's configuration.
97
+ after_save { implementation.initialize_redis_namespace }
98
+
99
+ delegate :accept?, to: :implementation
100
+ end
101
+
102
+ # Usage:
103
+
104
+ Bucket.create(rate: 10, burst: 50, prefix: 'CustomerIdentifier42')
105
+
106
+ # ...
107
+
108
+ # Later, in a controller:
109
+ DEFAULT_BUCKET = Ihasa.bucket(rate: 5, burst: 20, prefix: 'default')
110
+
111
+ def process_request
112
+ bucket = Bucket.find_by_prefix(params[:customer_identifier])
113
+ unless bucket
114
+ Rails.logger.warn("No bucket for customer #{params[:customer_identifier]}.")
115
+ Rails.logger.warn('Using default config.')
116
+ bucket = DEFAULT_BUCKET
117
+ end
118
+ unless bucket.accept?
119
+ Rails.logger.error("Customer #{params[:customer_identifier]} violated its rate limit.")
120
+ return head 403
121
+ end
122
+ # other actions not executed if rate limit violated
123
+ # ...
124
+ end
125
+ ```
74
126
 
75
127
  #### Configuring rate and burst limit
76
128
 
@@ -2,8 +2,10 @@ require 'redis'
2
2
  require 'ihasa/version'
3
3
  require 'ihasa/bucket'
4
4
 
5
+ # Ihasa module. Root of the Ihasa::Bucket class
5
6
  module Ihasa
6
7
  module_function
8
+
7
9
  def default_redis
8
10
  @redis ||= if ENV['REDIS_URL']
9
11
  Redis.new url: ENV['REDIS_URL']
@@ -12,8 +14,8 @@ module Ihasa
12
14
  end
13
15
  end
14
16
 
15
- DEFAULT_REDIS_PREFIX = 'IHAB'
16
- def bucket(rate: 5, burst: 10, prefix: DEFAULT_REDIS_PREFIX, redis: default_redis)
17
- @implementation = Bucket.new(rate, burst, prefix, redis)
17
+ DEFAULT_PREFIX = 'IHAB'.freeze
18
+ def bucket(rate: 5, burst: 10, prefix: DEFAULT_PREFIX, redis: default_redis)
19
+ Bucket.new(rate, burst, prefix, redis).tap(&:initialize_redis_namespace)
18
20
  end
19
21
  end
@@ -15,7 +15,6 @@ module Ihasa
15
15
  @redis = redis
16
16
  @rate = Float rate
17
17
  @burst = Float burst
18
- redis_init
19
18
  end
20
19
 
21
20
  def accept?
@@ -32,9 +31,7 @@ module Ihasa
32
31
  result
33
32
  end
34
33
 
35
- protected
36
-
37
- def redis_init
34
+ def initialize_redis_namespace
38
35
  redis_eval <<-LUA
39
36
  #{INTRO_STATEMENT}
40
37
  #{redis_set rate, @rate}
@@ -44,6 +41,8 @@ module Ihasa
44
41
  LUA
45
42
  end
46
43
 
44
+ protected
45
+
47
46
  require 'forwardable'
48
47
  extend Forwardable
49
48
 
@@ -3,7 +3,7 @@
3
3
  module Ihasa
4
4
  # This module holds the Ihasa version information.
5
5
  module Version
6
- STRING = '0.0.2'
6
+ STRING = '0.0.3'
7
7
 
8
8
  module_function
9
9
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ihasa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexandre Ignjatovic
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-16 00:00:00.000000000 Z
11
+ date: 2016-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis