redrate 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 17be9b2352cd8fc522f23f0d1960a2cc68faf7f9558855c3865106e8d5156dc4
4
+ data.tar.gz: 66b57b5b79aa2a0c1516b0309a659e1a9cd72c71336a8b13cd44388335dfb933
5
+ SHA512:
6
+ metadata.gz: 4890aa4e150cac89204ccffe758a66a9310ad6a0e5d9ddce0cd8169500ca6a9feb3c8363770bc8d0f5f3121ffa63d9ae8335a6bc709b03179e26bd5a81f33ba9
7
+ data.tar.gz: 4d20d7e6adca159fe5942304c40fade9f1a554646cb193c5900719edc68fed986aac78aafb1488b27f1bfe5ce7757cc706af5a130eb014450e10be9372226f16
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,50 @@
1
+ inherit_gem:
2
+ bixby: bixby_default.yml
3
+
4
+ AllCops:
5
+ TargetRubyVersion: 2.3
6
+ DisplayCopNames: true
7
+ Exclude:
8
+ - 'vendor/**/*'
9
+
10
+ Rails:
11
+ Enabled: true
12
+ Rails/FilePath:
13
+ Exclude:
14
+
15
+ Metrics/ClassLength:
16
+ Max: 130
17
+ Exclude:
18
+ Metrics/LineLength:
19
+ Max: 400
20
+ Metrics/BlockLength:
21
+ Exclude:
22
+ - '*.gemspec'
23
+ - 'spec/**/*'
24
+
25
+ RSpec/NestedGroups:
26
+ Enabled: false
27
+
28
+ RSpec/DescribeClass:
29
+ Exclude:
30
+ - 'spec/javascripts/**/*'
31
+
32
+ RSpec/LeadingSubject:
33
+ Enabled: false
34
+
35
+ RSpec/ExampleLength:
36
+ Enabled: false
37
+
38
+ RSpec/MultipleExpectations:
39
+ Enabled: false
40
+
41
+ Style/NumericLiterals:
42
+ MinDigits: 7
43
+
44
+ Layout/IndentationConsistency:
45
+ EnforcedStyle: rails
46
+
47
+ Naming/FileName:
48
+ Exclude:
49
+ - 'browse-everything.gemspec'
50
+ - 'Gemfile'
@@ -0,0 +1,9 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ services:
6
+ - redis-server
7
+ rvm:
8
+ - 2.5.0
9
+ before_install: gem install bundler -v 1.17.1
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+ source 'https://rubygems.org'
3
+
4
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
5
+
6
+ # Specify your gem's dependencies in redrate.gemspec
7
+ gemspec
@@ -0,0 +1,71 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ redrate (0.1.0)
5
+ redis
6
+ redlock
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.0)
12
+ bixby (1.0.0)
13
+ rubocop (~> 0.50, <= 0.52.1)
14
+ rubocop-rspec (~> 1.22, <= 1.22.2)
15
+ byebug (10.0.2)
16
+ coderay (1.1.2)
17
+ diff-lcs (1.3)
18
+ method_source (0.9.2)
19
+ parallel (1.13.0)
20
+ parser (2.6.0.0)
21
+ ast (~> 2.4.0)
22
+ powerpack (0.1.2)
23
+ pry (0.12.2)
24
+ coderay (~> 1.1.0)
25
+ method_source (~> 0.9.0)
26
+ pry-byebug (3.6.0)
27
+ byebug (~> 10.0)
28
+ pry (~> 0.10)
29
+ rainbow (3.0.0)
30
+ rake (10.5.0)
31
+ redis (4.1.0)
32
+ redlock (1.0.0)
33
+ redis (>= 3.0.0, < 5.0)
34
+ rspec (3.8.0)
35
+ rspec-core (~> 3.8.0)
36
+ rspec-expectations (~> 3.8.0)
37
+ rspec-mocks (~> 3.8.0)
38
+ rspec-core (3.8.0)
39
+ rspec-support (~> 3.8.0)
40
+ rspec-expectations (3.8.2)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.8.0)
43
+ rspec-mocks (3.8.0)
44
+ diff-lcs (>= 1.2.0, < 2.0)
45
+ rspec-support (~> 3.8.0)
46
+ rspec-support (3.8.0)
47
+ rubocop (0.52.1)
48
+ parallel (~> 1.10)
49
+ parser (>= 2.4.0.2, < 3.0)
50
+ powerpack (~> 0.1)
51
+ rainbow (>= 2.2.2, < 4.0)
52
+ ruby-progressbar (~> 1.7)
53
+ unicode-display_width (~> 1.0, >= 1.0.1)
54
+ rubocop-rspec (1.22.2)
55
+ rubocop (>= 0.52.1)
56
+ ruby-progressbar (1.10.0)
57
+ unicode-display_width (1.4.1)
58
+
59
+ PLATFORMS
60
+ ruby
61
+
62
+ DEPENDENCIES
63
+ bixby
64
+ bundler (~> 1.17)
65
+ pry-byebug
66
+ rake (~> 10.0)
67
+ redrate!
68
+ rspec (~> 3.0)
69
+
70
+ BUNDLED WITH
71
+ 1.17.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Michael Klein
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,97 @@
1
+ # Redrate [![Build Status](https://travis-ci.com/nulib/redrate.svg?branch=master)](https://travis-ci.com/nulib/redrate)
2
+
3
+ A distributed rate limiter for Ruby, based on [ruby-limiter](https://github.com/Shopify/limiter).
4
+ By using redis as the ring store, `Redrate` can enforce the rate limit across multiple
5
+ running instances of the same code, e.g., to enforce limits on shared use of a common
6
+ rate-limited API or other resource.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'redrate'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install redrate
23
+
24
+ ## Usage
25
+
26
+ ### Basic Usage
27
+
28
+ To rate limit calling an instance method, a mixin is provided. Simply specify
29
+ the method to me limited, and the maximum rate that the method can be called.
30
+ This rate is (by default) a number of requests per minute.
31
+
32
+ ``` ruby
33
+ class Widget
34
+ extend Redrate::Mixin
35
+
36
+ # limit the rate we can call tick to 300 times per minute
37
+ #
38
+ # when the rate has been exceeded, a call to tick will block
39
+ # until the rate limit would not be exceeded
40
+ limit_method :tick, rate: 300
41
+
42
+ ...
43
+ end
44
+ ```
45
+
46
+ To specify the rate in terms of an interval shorter (or longer) than 1 minute, an
47
+ optional `interval` parameter can be provided to specify the throttling period in seconds.
48
+
49
+ ``` ruby
50
+ class Widget
51
+ extend Redrate::Mixin
52
+
53
+ # limit the rate we can call tick to 5 times per second
54
+ #
55
+ # when the rate has been exceeded, a call to tick will block
56
+ # until the rate limit would not be exceeded
57
+ limit_method :tick, rate: 5, interval: 1
58
+
59
+ ...
60
+ end
61
+ ```
62
+
63
+ ### Advanced Usage
64
+
65
+ In cases where the mixin is not appropriate the `RedRate::Queue` class can be used
66
+ directly. As in the mixin examples above, the `interval` parameter is optional (and
67
+ defaults to 1 minute).
68
+
69
+ ``` ruby
70
+ class Widget
71
+ def initialize
72
+ # create a rate-limited queue which allows 10000 operations per hour
73
+ @queue = RedRate::Queue.new(10000, interval: 3600)
74
+ end
75
+
76
+ def tick
77
+ # this operation will block until less than 10000 shift calls have been
78
+ # made within the last hour
79
+ @queue.shift
80
+ # do something
81
+ end
82
+ end
83
+ ```
84
+
85
+ ## Development
86
+
87
+ 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.
88
+
89
+ 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).
90
+
91
+ ## Contributing
92
+
93
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nulib/redrate.
94
+
95
+ ## License
96
+
97
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+ require 'bundler/gem_tasks'
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: :spec
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'redrate'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+ require 'redrate/ring'
3
+ require 'redrate/queue'
4
+ require 'redrate/mixin'
5
+ require 'redrate/version'
6
+
7
+ module Redrate
8
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Redrate
4
+ module Mixin
5
+ def limit_method(method, rate:, interval: 60)
6
+ queue = Queue.new(rate, interval: interval, key: "#{self.name}##{method}")
7
+
8
+ mixin = Module.new do
9
+ define_method(method) do |*args|
10
+ queue.shift
11
+ super(*args)
12
+ end
13
+ end
14
+
15
+ prepend mixin
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'time'
4
+
5
+ module Redrate
6
+ class Queue
7
+ EPOCH = 0
8
+
9
+ def initialize(size, interval: 60, key: 'queue')
10
+ @ring = Ring.new(size, key, EPOCH)
11
+ @interval = interval
12
+ end
13
+
14
+ def shift
15
+ time = nil
16
+
17
+ @ring.lock do
18
+ sleep_until(@ring.current + @interval)
19
+ @ring.rotate!
20
+ end
21
+
22
+ time
23
+ end
24
+
25
+ private
26
+
27
+ def sleep_until(time)
28
+ interval = time - @ring.now
29
+ return unless interval.positive?
30
+ sleep(interval)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'redis'
4
+ require 'redlock'
5
+
6
+ module Redrate
7
+ class Ring
8
+ def initialize(size, key, default)
9
+ @redis = Redis.current
10
+ @redlock = Redlock::Client.new([@redis])
11
+ @size = size
12
+ @key = key
13
+ @default = default
14
+
15
+ initialize_ring
16
+ end
17
+
18
+ def head
19
+ @redis.get(head_key).to_i
20
+ end
21
+
22
+ def current
23
+ (@redis.lindex(ring_key, head) || @default).to_i
24
+ end
25
+
26
+ def rotate!
27
+ current_head = head
28
+ current_time = now
29
+ @redis.pipelined do
30
+ @redis.lset(ring_key, current_head, current_time.to_s)
31
+ @redis.set(head_key, (current_head + 1) % @size)
32
+ end
33
+ end
34
+
35
+ def lock
36
+ completed = false
37
+ timeout_time = monotonic_time + 2000
38
+ while !completed && (monotonic_time < timeout_time)
39
+ @redlock.lock(lock_key, 2000) do |locked|
40
+ if locked
41
+ yield
42
+ completed = true
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ def now
49
+ @redis.time[0]
50
+ end
51
+
52
+ private
53
+
54
+ def head_key
55
+ @head_key ||= key_for(:head)
56
+ end
57
+
58
+ def ring_key
59
+ @ring_key ||= key_for(:ring)
60
+ end
61
+
62
+ def lock_key
63
+ @lock_key ||= key_for(:lock)
64
+ end
65
+
66
+ def key_for(thing)
67
+ ['rate', @key, thing].join(':')
68
+ end
69
+
70
+ def initialize_ring
71
+ return unless @redis.llen(ring_key).zero?
72
+
73
+ @redis.pipelined do
74
+ 1.upto(@size) do
75
+ @redis.lpush(ring_key, @default)
76
+ end
77
+ @redis.set(head_key, 0)
78
+ end
79
+ end
80
+
81
+ def monotonic_time
82
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ module Redrate
3
+ VERSION = '0.1.0'
4
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'redrate/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'redrate'
9
+ spec.version = Redrate::VERSION
10
+ spec.authors = ['Michael B. Klein']
11
+ spec.email = ['mbklein@gmail.com']
12
+
13
+ spec.summary = 'An easy, distributed rate limiter using Redis'
14
+ spec.homepage = 'https://github.com/nulib/redrate.git'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_dependency 'redis'
25
+ spec.add_dependency 'redlock'
26
+ spec.add_development_dependency 'bixby'
27
+ spec.add_development_dependency 'bundler', '~> 1.17'
28
+ spec.add_development_dependency 'pry-byebug'
29
+ spec.add_development_dependency 'rake', '~> 10.0'
30
+ spec.add_development_dependency 'rspec', '~> 3.0'
31
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redrate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael B. Klein
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-02-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: redis
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: redlock
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bixby
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.17'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.17'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.0'
111
+ description:
112
+ email:
113
+ - mbklein@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".rubocop.yml"
121
+ - ".travis.yml"
122
+ - Gemfile
123
+ - Gemfile.lock
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - bin/console
128
+ - bin/setup
129
+ - lib/redrate.rb
130
+ - lib/redrate/mixin.rb
131
+ - lib/redrate/queue.rb
132
+ - lib/redrate/ring.rb
133
+ - lib/redrate/version.rb
134
+ - redrate.gemspec
135
+ homepage: https://github.com/nulib/redrate.git
136
+ licenses:
137
+ - MIT
138
+ metadata: {}
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubyforge_project:
155
+ rubygems_version: 2.7.3
156
+ signing_key:
157
+ specification_version: 4
158
+ summary: An easy, distributed rate limiter using Redis
159
+ test_files: []