ratelimit 1.0.2 → 1.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: f9759fc3e7da10acf75e0b15ba6f90a901257543
4
- data.tar.gz: b3bcdc0daa3076dfe081942e90a922125a772019
3
+ metadata.gz: 3ca5c11e810378b9f764fc4df1a9eb95c5be682f
4
+ data.tar.gz: 29be916842600285b975409daa377a6ef0a47d7b
5
5
  SHA512:
6
- metadata.gz: e4a346d5455ff73cd2218f312ac08f098f2acc80041995a1b9e00ad93dfa37ba27d27be293c2a741f15e2963416083008e7aecdac3f7f96498f32eef37d39a8b
7
- data.tar.gz: 7af9dcaddc5bf2f17cb8d335384c3f87eda9336fd0303f3ad8b7ec1c99bdd9c81bcec37cc7e491d77548c974a19b7abcebfc475d694f51398cb941d346fc1334
6
+ metadata.gz: a8bf4fdb9ccb0e298870c1cb6e19d44e90d9fb92d5f5f311dd7d68eff139150c38b1a7bfcd3175c0cff317b3d96fbe02c72f20a4acdf26243cd3c28f4a74d0b3
7
+ data.tar.gz: fa1910970d4f93ec8e5c947012344a002bb0e8f7ed56b130fb8b454ae9ff373a140dd2e9e4865bd13772e51ba5485ca2448f0c2165f92d7549c41756d8935bbf
@@ -1,9 +1,7 @@
1
1
  script: "bundle exec rspec"
2
2
  rvm:
3
- - 1.9.2
4
- - 1.9.3
5
- - 2.0.0
6
- - 2.1.1
7
- - 2.1.2
8
- - rbx-2
9
- - jruby-19mode
3
+ - 2.1.10
4
+ - 2.2
5
+ - 2.3
6
+ - 2.4.0
7
+ - jruby-9.0.5.0
data/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
  [![Code Climate](https://img.shields.io/codeclimate/github/ejfinneran/ratelimit.svg)](https://codeclimate.com/github/ejfinneran/ratelimit)
5
5
  [![Coverage Status](https://img.shields.io/coveralls/ejfinneran/ratelimit.svg)](https://coveralls.io/r/ejfinneran/ratelimit)
6
6
 
7
- Ratelimit provides a way to rate limit actions across multiple servers using Redis. This is a port of RateLimit.js found [here](https://github.com/chriso/redback/blob/master/lib/advanced_structures/RateLimit.js) and inspired by [this post](http://chris6f.com/rate-limiting-with-redis).
7
+ Ratelimit provides a way to rate limit actions across multiple servers using Redis. This is a port of RateLimit.js found [here](https://github.com/chriso/redback/blob/master/lib/advanced_structures/RateLimit.js) and inspired by [this post](https://gist.github.com/chriso/54dd46b03155fcf555adccea822193da).
8
8
 
9
9
 
10
10
  ## Installation
@@ -58,6 +58,7 @@ You can also pass a block that will only get executed if the given threshold is
58
58
  ``` rb
59
59
  ratelimit.exec_within_threshold phone_number, threshold: 10, interval: 30 do
60
60
  some_rate_limited_code
61
+ ratelimit.add(phone_number)
61
62
  end
62
63
  ```
63
64
 
@@ -29,7 +29,7 @@ class Ratelimit
29
29
  if @bucket_count < 3
30
30
  raise ArgumentError.new("Cannot have less than 3 buckets")
31
31
  end
32
- @redis = options[:redis]
32
+ @raw_redis = options[:redis]
33
33
  end
34
34
 
35
35
  # Add to the counter for a given subject.
@@ -41,7 +41,7 @@ class Ratelimit
41
41
  def add(subject, count = 1)
42
42
  bucket = get_bucket
43
43
  subject = "#{@key}:#{subject}"
44
- redis.pipelined do
44
+ redis.multi do
45
45
  redis.hincrby(subject, bucket, count)
46
46
  redis.hdel(subject, (bucket + 1) % @bucket_count)
47
47
  redis.hdel(subject, (bucket + 2) % @bucket_count)
@@ -55,7 +55,7 @@ class Ratelimit
55
55
  # @param [Integer] interval How far back (in seconds) to retrieve activity.
56
56
  def count(subject, interval)
57
57
  bucket = get_bucket
58
- interval = [interval, @bucket_interval].max
58
+ interval = [[interval, @bucket_interval].max, @bucket_span].min
59
59
  count = (interval / @bucket_interval).floor
60
60
  subject = "#{@key}:#{subject}"
61
61
 
@@ -97,6 +97,7 @@ class Ratelimit
97
97
  # @example Send an email as long as we haven't send 5 in the last 10 minutes
98
98
  # ratelimit.exec_with_threshold(email, [:threshold => 5, :interval => 600]) do
99
99
  # send_another_email
100
+ # ratelimit.add(email)
100
101
  # end
101
102
  def exec_within_threshold(subject, options = {}, &block)
102
103
  options[:threshold] ||= 30
@@ -114,6 +115,6 @@ class Ratelimit
114
115
  end
115
116
 
116
117
  def redis
117
- @redis ||= Redis::Namespace.new(:ratelimit, :redis => @redis || Redis.new)
118
+ @redis ||= Redis::Namespace.new(:ratelimit, redis: @raw_redis || Redis.new)
118
119
  end
119
120
  end
@@ -1,3 +1,3 @@
1
1
  class Ratelimit
2
- VERSION = "1.0.2"
2
+ VERSION = "1.0.3"
3
3
  end
@@ -1,6 +1,24 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Ratelimit do
4
+ describe '.initialize' do
5
+ subject { described_class.new(key, options) }
6
+
7
+ let(:options) { Hash.new }
8
+
9
+ context 'with key' do
10
+ let(:key) { 'key' }
11
+
12
+ context 'with redis option' do
13
+ let(:redis) { double('redis') }
14
+ let(:options) { super().merge(redis: redis) }
15
+
16
+ it 'wraps redis in redis-namespace' do
17
+ expect(subject.send(:redis)).to be_instance_of(Redis::Namespace)
18
+ end
19
+ end
20
+ end
21
+ end
4
22
 
5
23
  before do
6
24
  @r = Ratelimit.new("key")
@@ -75,7 +93,7 @@ describe Ratelimit do
75
93
  expect(@r.within_bounds?("value1", {:threshold => 10, :interval => 30})).to be false
76
94
  end
77
95
 
78
- it "accept a threshhold and a block that gets executed once it's below the threshold" do
96
+ it "accept a threshold and a block that gets executed once it's below the threshold" do
79
97
  expect(@r.count("key", 30)).to eq(0)
80
98
  31.times do
81
99
  @r.add("key")
@@ -100,10 +118,15 @@ describe Ratelimit do
100
118
  end
101
119
 
102
120
 
103
- it "counts correclty if bucket_span equals count-interval " do
121
+ it "counts correctly if bucket_span equals count-interval " do
104
122
  @r = Ratelimit.new("key", {:bucket_span => 10, bucket_interval: 1})
105
123
  @r.add('value1')
106
124
  expect(@r.count('value1', 10)).to eql(1)
107
125
  end
108
126
 
127
+ it "counts correctly if interval is greater than bucket_span" do
128
+ @r = Ratelimit.new("key", { bucket_span: 10, bucket_interval: 1})
129
+ @r.add('value1')
130
+ expect(@r.count('value1', 40)).to eql(1)
131
+ end
109
132
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ratelimit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - E.J. Finneran
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-09 00:00:00.000000000 Z
11
+ date: 2017-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
@@ -201,4 +201,3 @@ summary: Rate limiting backed by redis
201
201
  test_files:
202
202
  - spec/ratelimit_spec.rb
203
203
  - spec/spec_helper.rb
204
- has_rdoc: