rolling-limit 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: ca1642a98f230734665f4bb94a0ef43f234eeed9
4
- data.tar.gz: ed5845dbd3829e8f944224caa61d9634c59e7da5
2
+ SHA256:
3
+ metadata.gz: fc44e4fe6e5308274bb2a0a4f19f9ddb937c5dae2a2fae267ccbf14485acb682
4
+ data.tar.gz: 9942164196f7c49908ab3565714f4e7204d831e3c903937b6ddaa213ab39d891
5
5
  SHA512:
6
- metadata.gz: 85a03de41fb244b4b338a62a0662484b8f5a95ddb3a90c725f3f29ad89304b5a0925927c72057dda67bcbc088181e0b78fcf4ca3dc6fa5fa9359db49cc492ab4
7
- data.tar.gz: 973bc7f1e6d104d3ecd6a0b6cd5babdb6710d0188a2fbfab62dad044cc5a1d7db82ce489934bbe7c08ca7efbd1d6d3c1625512adc622b06b334d2e1b3df2b6b5
6
+ metadata.gz: 524aa999b157efa01494a54f1092ff4fe467845c5958315ec78adde8b14adef059477409847b867e5a593a5fc69c65defb1937f32e7df308c6e662adc269a0bb
7
+ data.tar.gz: 6ce018c6f166c526ab99b0ecb858b3cea52593afe21f9b033dfd43509644e9432ee71d21e70f8a6b0ceb05d4bc8dd4a7337cf132c90a05bcafddf72a1c08e323
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
- ````ruby
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
- TODO: Write usage instructions here
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
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
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 install this gem onto your local machine, run `bundle exec rake install`. 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).
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
-
@@ -0,0 +1,14 @@
1
+ version: "3"
2
+
3
+ services:
4
+ app:
5
+ build: .
6
+ environment:
7
+ - REDIS_HOST=redis
8
+ volumes:
9
+ - .:/usr/local/src/rolling-limit
10
+ depends_on:
11
+ - redis
12
+
13
+ redis:
14
+ image: redis:6
@@ -1,5 +1,5 @@
1
1
  module Rolling
2
2
  class Limit
3
- VERSION = "0.1.1"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -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", "~> 3.0"
31
- spec.add_development_dependency "bundler", "~> 1.12"
30
+ spec.add_dependency "redis", '~> 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,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rolling-limit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
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: 2016-08-11 00:00:00.000000000 Z
11
+ date: 2023-03-31 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: '3.0'
19
+ version: '5.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
25
- - !ruby/object:Gem::Version
26
- version: '3.0'
27
- - !ruby/object:Gem::Dependency
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
- - - ~>
24
+ - - "~>"
39
25
  - !ruby/object:Gem::Version
40
- version: '1.12'
26
+ version: '5.0'
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: rake
43
29
  requirement: !ruby/object:Gem::Requirement
44
30
  requirements:
45
- - - ~>
31
+ - - "~>"
46
32
  - !ruby/object:Gem::Version
47
33
  version: '10.0'
48
34
  type: :development
49
35
  prerelease: false
50
36
  version_requirements: !ruby/object:Gem::Requirement
51
37
  requirements:
52
- - - ~>
38
+ - - "~>"
53
39
  - !ruby/object:Gem::Version
54
40
  version: '10.0'
55
41
  - !ruby/object:Gem::Dependency
56
42
  name: rspec
57
43
  requirement: !ruby/object:Gem::Requirement
58
44
  requirements:
59
- - - ~>
45
+ - - "~>"
60
46
  - !ruby/object:Gem::Version
61
47
  version: '3.0'
62
48
  type: :development
63
49
  prerelease: false
64
50
  version_requirements: !ruby/object:Gem::Requirement
65
51
  requirements:
66
- - - ~>
52
+ - - "~>"
67
53
  - !ruby/object:Gem::Version
68
54
  version: '3.0'
69
55
  description: Uses redis sorted sets to allow easy rate limiting
@@ -73,15 +59,17 @@ executables: []
73
59
  extensions: []
74
60
  extra_rdoc_files: []
75
61
  files:
76
- - .gitignore
77
- - .rspec
78
- - .rubocop.yml
79
- - .travis.yml
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".rubocop.yml"
65
+ - ".travis.yml"
66
+ - Dockerfile
80
67
  - Gemfile
81
68
  - README.md
82
69
  - Rakefile
83
70
  - bin/console
84
71
  - bin/setup
72
+ - docker-compose.yml
85
73
  - lib/rolling/limit.rb
86
74
  - lib/rolling/limit/version.rb
87
75
  - rolling-limit.gemspec
@@ -90,24 +78,23 @@ licenses:
90
78
  - MIT
91
79
  metadata:
92
80
  allowed_push_host: https://rubygems.org/
93
- post_install_message:
81
+ post_install_message:
94
82
  rdoc_options: []
95
83
  require_paths:
96
84
  - lib
97
85
  required_ruby_version: !ruby/object:Gem::Requirement
98
86
  requirements:
99
- - - '>='
87
+ - - ">="
100
88
  - !ruby/object:Gem::Version
101
89
  version: '0'
102
90
  required_rubygems_version: !ruby/object:Gem::Requirement
103
91
  requirements:
104
- - - '>='
92
+ - - ">="
105
93
  - !ruby/object:Gem::Version
106
94
  version: '0'
107
95
  requirements: []
108
- rubyforge_project:
109
- rubygems_version: 2.0.14.1
110
- signing_key:
96
+ rubygems_version: 3.1.2
97
+ signing_key:
111
98
  specification_version: 4
112
99
  summary: A redis-based rolling rate limiter
113
100
  test_files: []