rolling-limit 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/Dockerfile +9 -0
- data/README.md +57 -19
- data/docker-compose.yml +14 -0
- data/lib/rolling/limit/version.rb +1 -1
- data/rolling-limit.gemspec +1 -2
- metadata +27 -34
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b4a4aafd9962db770186991d46ff93a8ce746a0d53eeaa9be88053ef61d80116
|
4
|
+
data.tar.gz: 7dc80b3d85874bc3ce453d13aab8f27ebf5bfdffc1bd6bdfc9c51b8fd82b5751
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c97735055e5c648d69e67a638f38a6a80395515fee1a1f97d673b5206ebe7add4b9883901d5a2b035f9ff4b568638df857bcb968c53a713132c0732ee549e589
|
7
|
+
data.tar.gz: d42b46c1e84549a03b34c897b40e76c1017eada51ec881fcaef5374e507f76606638167585f634f9040d6ee4b196e8c77796b96045f0b674d71d1f969334488c
|
data/Dockerfile
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
FROM ruby:3.0
|
2
|
+
|
3
|
+
WORKDIR /usr/local/src/rolling-limit
|
4
|
+
|
5
|
+
COPY lib/rolling/limit/version.rb ./lib/rolling/limit/
|
6
|
+
# Preinstall latest gems specified by gemspec; note that they may be overwritten by Gemfile.lock from mounted app dir
|
7
|
+
COPY Gemfile rolling-limit.gemspec ./
|
8
|
+
|
9
|
+
RUN bundle install
|
data/README.md
CHANGED
@@ -2,21 +2,7 @@
|
|
2
2
|
|
3
3
|
A redis-backed rate limiter, using redis sorted sets.
|
4
4
|
|
5
|
-
|
6
|
-
require 'rolling/limit'
|
7
|
-
|
8
|
-
rl = Rolling::Limit.new(redis: connection, key: "unique key",
|
9
|
-
max_operations: 5, timespan: 60)
|
10
|
-
|
11
|
-
rl.remaining # => 4
|
12
|
-
rl.remaining # => 3
|
13
|
-
rl.remaining # => 2
|
14
|
-
rl.remaining # => 1
|
15
|
-
rl.remaining # => 0
|
16
|
-
rl.remaining # => false
|
17
|
-
# ... 61s later
|
18
|
-
rl.remaining # => 4
|
19
|
-
````
|
5
|
+
The operations are not guaranteed to be atomic, so this will be subject to race conditions - but it should be good enough for most uses where the intent is to prevent high usage, rather than to accurately count the exact number of operations within a particular time period.
|
20
6
|
|
21
7
|
## Installation
|
22
8
|
|
@@ -36,15 +22,67 @@ Or install it yourself as:
|
|
36
22
|
|
37
23
|
## Usage
|
38
24
|
|
39
|
-
|
25
|
+
Instantiate a `Rolling::Limit` object and then use `#remaining` to determine if an operation may be performed within the specified limits.
|
26
|
+
|
27
|
+
e.g.
|
28
|
+
```ruby
|
29
|
+
require 'rolling/limit'
|
30
|
+
|
31
|
+
|
32
|
+
limit = Rolling::Limit.new(redis: redis, key: "u:#{user.id}:api", max_operations: 100, timespan: 60)
|
33
|
+
|
34
|
+
if limit.remaining
|
35
|
+
perform_api_operation(user)
|
36
|
+
else
|
37
|
+
raise "Limit exceeded - try again soon"
|
38
|
+
end
|
39
|
+
|
40
|
+
```
|
41
|
+
|
42
|
+
or
|
43
|
+
|
44
|
+
e.g
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
require 'rolling/limit'
|
48
|
+
|
49
|
+
limit = Rolling::Limit.new(redis: connection, key: "unique key",
|
50
|
+
max_operations: 5, timespan: 60)
|
51
|
+
|
52
|
+
limit.remaining # => 4
|
53
|
+
limit.remaining # => 3
|
54
|
+
limit.remaining # => 2
|
55
|
+
limit.remaining # => 1
|
56
|
+
limit.remaining # => 0
|
57
|
+
limit.remaining # => false
|
58
|
+
# ... 61s later
|
59
|
+
limit.remaining # => 4
|
60
|
+
|
61
|
+
```
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
# Rolling::Limit is a class that maintains a rolling limit of no more than
|
65
|
+
# <max_operations> operations for a given <key> within <timespan> seconds.
|
66
|
+
class Rolling::Limit
|
67
|
+
def initialize(redis:, key:, max_operations:, timespan:)
|
68
|
+
|
69
|
+
# Increments the counter and returns truthy (with number of remaining
|
70
|
+
# operations)
|
71
|
+
def remaining
|
72
|
+
|
73
|
+
# Resets this counter
|
74
|
+
def reset!
|
75
|
+
|
76
|
+
# Returns true if there are remaining operations, without incrementing
|
77
|
+
def remaining?
|
78
|
+
```
|
40
79
|
|
41
80
|
## Development
|
42
81
|
|
43
|
-
|
82
|
+
Run `docker-compose build` to build a Docker image. You can then run `docker-compose run --rm app bundle exec rspec` to run automated tests.
|
44
83
|
|
45
|
-
To
|
84
|
+
To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
46
85
|
|
47
86
|
## Contributing
|
48
87
|
|
49
88
|
Bug reports and pull requests are welcome on GitHub at https://github.com/livelink/rolling-limit.
|
50
|
-
|
data/docker-compose.yml
ADDED
data/rolling-limit.gemspec
CHANGED
@@ -27,8 +27,7 @@ Gem::Specification.new do |spec|
|
|
27
27
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
28
|
spec.require_paths = ["lib"]
|
29
29
|
|
30
|
-
spec.add_dependency "redis",
|
31
|
-
spec.add_development_dependency "bundler", "~> 1.12"
|
30
|
+
spec.add_dependency "redis", '>= 3.0.0', '< 5.0'
|
32
31
|
spec.add_development_dependency "rake", "~> 10.0"
|
33
32
|
spec.add_development_dependency "rspec", "~> 3.0"
|
34
33
|
end
|
metadata
CHANGED
@@ -1,69 +1,61 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rolling-limit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Geoff Youngs
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-08-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 3.0.0
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '5.0'
|
20
23
|
type: :runtime
|
21
24
|
prerelease: false
|
22
25
|
version_requirements: !ruby/object:Gem::Requirement
|
23
26
|
requirements:
|
24
|
-
- -
|
27
|
+
- - ">="
|
25
28
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
27
|
-
-
|
28
|
-
name: bundler
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - ~>
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '1.12'
|
34
|
-
type: :development
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - ~>
|
29
|
+
version: 3.0.0
|
30
|
+
- - "<"
|
39
31
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
32
|
+
version: '5.0'
|
41
33
|
- !ruby/object:Gem::Dependency
|
42
34
|
name: rake
|
43
35
|
requirement: !ruby/object:Gem::Requirement
|
44
36
|
requirements:
|
45
|
-
- - ~>
|
37
|
+
- - "~>"
|
46
38
|
- !ruby/object:Gem::Version
|
47
39
|
version: '10.0'
|
48
40
|
type: :development
|
49
41
|
prerelease: false
|
50
42
|
version_requirements: !ruby/object:Gem::Requirement
|
51
43
|
requirements:
|
52
|
-
- - ~>
|
44
|
+
- - "~>"
|
53
45
|
- !ruby/object:Gem::Version
|
54
46
|
version: '10.0'
|
55
47
|
- !ruby/object:Gem::Dependency
|
56
48
|
name: rspec
|
57
49
|
requirement: !ruby/object:Gem::Requirement
|
58
50
|
requirements:
|
59
|
-
- - ~>
|
51
|
+
- - "~>"
|
60
52
|
- !ruby/object:Gem::Version
|
61
53
|
version: '3.0'
|
62
54
|
type: :development
|
63
55
|
prerelease: false
|
64
56
|
version_requirements: !ruby/object:Gem::Requirement
|
65
57
|
requirements:
|
66
|
-
- - ~>
|
58
|
+
- - "~>"
|
67
59
|
- !ruby/object:Gem::Version
|
68
60
|
version: '3.0'
|
69
61
|
description: Uses redis sorted sets to allow easy rate limiting
|
@@ -73,15 +65,17 @@ executables: []
|
|
73
65
|
extensions: []
|
74
66
|
extra_rdoc_files: []
|
75
67
|
files:
|
76
|
-
- .gitignore
|
77
|
-
- .rspec
|
78
|
-
- .rubocop.yml
|
79
|
-
- .travis.yml
|
68
|
+
- ".gitignore"
|
69
|
+
- ".rspec"
|
70
|
+
- ".rubocop.yml"
|
71
|
+
- ".travis.yml"
|
72
|
+
- Dockerfile
|
80
73
|
- Gemfile
|
81
74
|
- README.md
|
82
75
|
- Rakefile
|
83
76
|
- bin/console
|
84
77
|
- bin/setup
|
78
|
+
- docker-compose.yml
|
85
79
|
- lib/rolling/limit.rb
|
86
80
|
- lib/rolling/limit/version.rb
|
87
81
|
- rolling-limit.gemspec
|
@@ -90,24 +84,23 @@ licenses:
|
|
90
84
|
- MIT
|
91
85
|
metadata:
|
92
86
|
allowed_push_host: https://rubygems.org/
|
93
|
-
post_install_message:
|
87
|
+
post_install_message:
|
94
88
|
rdoc_options: []
|
95
89
|
require_paths:
|
96
90
|
- lib
|
97
91
|
required_ruby_version: !ruby/object:Gem::Requirement
|
98
92
|
requirements:
|
99
|
-
- -
|
93
|
+
- - ">="
|
100
94
|
- !ruby/object:Gem::Version
|
101
95
|
version: '0'
|
102
96
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
97
|
requirements:
|
104
|
-
- -
|
98
|
+
- - ">="
|
105
99
|
- !ruby/object:Gem::Version
|
106
100
|
version: '0'
|
107
101
|
requirements: []
|
108
|
-
|
109
|
-
|
110
|
-
signing_key:
|
102
|
+
rubygems_version: 3.0.3
|
103
|
+
signing_key:
|
111
104
|
specification_version: 4
|
112
105
|
summary: A redis-based rolling rate limiter
|
113
106
|
test_files: []
|