simple-throttle 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/README.md +36 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/throttle/time_window/runner.rb +41 -0
- data/lib/throttle/version.rb +3 -0
- data/lib/throttle.rb +17 -0
- data/simple-throttle.gemspec +30 -0
- metadata +112 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 64857d974825a0acfc9ea727baa6677cb65bc259
|
4
|
+
data.tar.gz: 82873bafc96d5958a22240c2314eed21f7880368
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ecee0e9ed26d1f6a21721b94651681cb4afc38315ac6ac03082fccc0c9a2d460c2ab74707bfacd2fbe1ac9fdec5461f1131c76be467f9f20f11d83d02f6ee8a2
|
7
|
+
data.tar.gz: be1e90d260664b00b8b84e69bb0907578b1ee3f8a09012f09f416e08c585998b3d85a3b25b324b42a360a6f4665f4b57a806c06550d715009e4a5dd93448ec87
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# Throttle
|
2
|
+
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
Add this line to your application's Gemfile:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
gem 'simple-throttle'
|
9
|
+
```
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install simple-throttle
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
@throttle = Throttle.per_hour(1) #allow one call per hour
|
23
|
+
@throttle.run { puts 'Hello World!' } #executes immediately
|
24
|
+
@throttle.run { puts 'Hello World!' } #sleeps for 1 hour before executing.
|
25
|
+
```
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
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.
|
30
|
+
|
31
|
+
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).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/simple-throttle.
|
36
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "simple/throttle"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
module Throttle
|
2
|
+
module TimeWindow
|
3
|
+
##
|
4
|
+
# Executes at most a number of times within given time window.
|
5
|
+
#
|
6
|
+
class Runner
|
7
|
+
# Parameters:
|
8
|
+
# +wait_s+: minimum number os seconds between +count+ block invocations
|
9
|
+
# +count+: maximum number of times block may be called between +wait_s+ seconds
|
10
|
+
def initialize(wait_s, count, sleep = ->(seconds) { sleep seconds })
|
11
|
+
@max = count
|
12
|
+
@last = 0.0 #last invocation timestamp
|
13
|
+
@count = 0
|
14
|
+
@wait_s = wait_s.to_f
|
15
|
+
@sleep = sleep
|
16
|
+
end
|
17
|
+
|
18
|
+
# Run given +block+ either immediately (if allowed) or after sleep.
|
19
|
+
def run(&block)
|
20
|
+
#puts "count: #{@count}, delta: #{Time.now.to_f - @last}"
|
21
|
+
now = Time.now.to_f
|
22
|
+
delta = now - @last
|
23
|
+
if delta >= @wait_s
|
24
|
+
#puts "resetting"
|
25
|
+
@last = now
|
26
|
+
@count = 0
|
27
|
+
end
|
28
|
+
|
29
|
+
if @count >= @max
|
30
|
+
sleep_interval = @wait_s - delta
|
31
|
+
#puts "throttle forced sleep for #{sleep_interval}"
|
32
|
+
@sleep.call sleep_interval
|
33
|
+
end
|
34
|
+
|
35
|
+
@count += 1
|
36
|
+
#puts "running "
|
37
|
+
yield
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/lib/throttle.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require "throttle/version"
|
2
|
+
require "throttle/time_window/runner"
|
3
|
+
|
4
|
+
module Throttle
|
5
|
+
|
6
|
+
HOUR_SECONDS = 3600
|
7
|
+
DAYS_SECONDS = 24 * HOUR_SECONDS
|
8
|
+
|
9
|
+
def self.per_hour(count)
|
10
|
+
TimeWindow::Runner.new(HOUR_SECONDS, count)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.per_day(count)
|
14
|
+
TimeWindow::Runner.new(DAYS_SECONDS, count)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'throttle/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "simple-throttle"
|
8
|
+
spec.version = Throttle::VERSION
|
9
|
+
spec.authors = ["Alexandre Maia"]
|
10
|
+
spec.email = ["alexandre.maia@gmail.com"]
|
11
|
+
spec.summary = %q{Limit amount of executions with time windows.}
|
12
|
+
spec.homepage = "https://www.github.com/cxz/simple-throttle"
|
13
|
+
spec.license = %q{MIT}
|
14
|
+
|
15
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
16
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
17
|
+
#if spec.respond_to?(:metadata)
|
18
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
19
|
+
#else
|
20
|
+
# raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
21
|
+
#end
|
22
|
+
|
23
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
29
|
+
spec.add_development_dependency "timecop"
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple-throttle
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alexandre Maia
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-08-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: timecop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- alexandre.maia@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- bin/console
|
83
|
+
- bin/setup
|
84
|
+
- lib/throttle.rb
|
85
|
+
- lib/throttle/time_window/runner.rb
|
86
|
+
- lib/throttle/version.rb
|
87
|
+
- simple-throttle.gemspec
|
88
|
+
homepage: https://www.github.com/cxz/simple-throttle
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.5.1
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: Limit amount of executions with time windows.
|
112
|
+
test_files: []
|