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 +4 -4
- data/README.md +55 -3
- data/lib/ihasa.rb +5 -3
- data/lib/ihasa/bucket.rb +3 -4
- data/lib/ihasa/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 90ccebca3bb8a2ff3b1bce72077560703cca3946
|
4
|
+
data.tar.gz: 14f0db80a27e810b1e9fdd62d9f90538dff9f138
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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 '
|
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
|
-
|
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
|
|
data/lib/ihasa.rb
CHANGED
@@ -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
|
-
|
16
|
-
def bucket(rate: 5, burst: 10, prefix:
|
17
|
-
|
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
|
data/lib/ihasa/bucket.rb
CHANGED
@@ -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
|
-
|
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
|
|
data/lib/ihasa/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2016-05-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis
|