simple-throttle 0.0.1 → 0.0.11

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: 64857d974825a0acfc9ea727baa6677cb65bc259
4
- data.tar.gz: 82873bafc96d5958a22240c2314eed21f7880368
3
+ metadata.gz: 240699b699a121b5e2c61603da086dc0783fee5a
4
+ data.tar.gz: eb41f346aabdeae4e649b62beda29cea99c1ca3d
5
5
  SHA512:
6
- metadata.gz: ecee0e9ed26d1f6a21721b94651681cb4afc38315ac6ac03082fccc0c9a2d460c2ab74707bfacd2fbe1ac9fdec5461f1131c76be467f9f20f11d83d02f6ee8a2
7
- data.tar.gz: be1e90d260664b00b8b84e69bb0907578b1ee3f8a09012f09f416e08c585998b3d85a3b25b324b42a360a6f4665f4b57a806c06550d715009e4a5dd93448ec87
6
+ metadata.gz: 5375d65514aaca8c347576add62c8507e37769557e2fd485a5644c0d1b0cb78d4121a22f1aec99fc838c00533e8e01d99ddf418efad630e722b4ab80375b2ac5
7
+ data.tar.gz: 172875d5289072f07404a1bdfdee50c695517e6a12be2309fc17dc50f3e067c1fc1c2ad92a5720711e5fe52f7c9055f483544a3c2cabf4b226ce1ba43a6ec8b3
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in simple-throttle.gemspec
3
+ # Specify your gem's dependencies in simple-simple_throttle.gemspec
4
4
  gemspec
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require "bundler/setup"
4
- require "simple/throttle"
4
+ require "simple_throttle"
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
@@ -0,0 +1,22 @@
1
+ require "simple_throttle/version"
2
+ require "simple_throttle/time_window/runner"
3
+ require "simple_throttle/manual/runner"
4
+
5
+ module SimpleThrottle
6
+
7
+ HOUR_SECONDS = 3600
8
+ DAYS_SECONDS = 24 * HOUR_SECONDS
9
+
10
+ def self.per_hour(count)
11
+ TimeWindow::Runner.new(HOUR_SECONDS, count.to_i)
12
+ end
13
+
14
+ def self.per_day(count)
15
+ TimeWindow::Runner.new(DAYS_SECONDS, count.to_i)
16
+ end
17
+
18
+ def self.hourly_manual(count)
19
+ Manual::Runner.new(HOUR_SECONDS, count.to_i)
20
+ end
21
+ end
22
+
@@ -0,0 +1,33 @@
1
+ module SimpleThrottle
2
+ module Manual
3
+ class Runner
4
+ #client can manually override the remaining count.
5
+ attr_accessor :remaining
6
+
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
+ @remaining = @max
13
+ @wait_s = wait_s.to_f
14
+ @sleep = sleep
15
+ end
16
+
17
+ # Run given +block+ either immediately (if allowed) or after sleep.
18
+ def run(&block)
19
+ if @remaining < 1
20
+ @sleep.call @wait_s
21
+ @remaining = @max
22
+ end
23
+
24
+ @remaining -= 1
25
+ yield
26
+ end
27
+
28
+ def within_limit?
29
+ @remaining > 0
30
+ end
31
+ end
32
+ end
33
+ end
@@ -1,4 +1,4 @@
1
- module Throttle
1
+ module SimpleThrottle
2
2
  module TimeWindow
3
3
  ##
4
4
  # Executes at most a number of times within given time window.
@@ -18,17 +18,12 @@ module Throttle
18
18
  # Run given +block+ either immediately (if allowed) or after sleep.
19
19
  def run(&block)
20
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
21
 
29
- if @count >= @max
22
+ restart_window_if_needed
23
+
24
+ if !within_limit?
30
25
  sleep_interval = @wait_s - delta
31
- #puts "throttle forced sleep for #{sleep_interval}"
26
+ #puts "simple_throttle forced sleep for #{sleep_interval}"
32
27
  @sleep.call sleep_interval
33
28
  end
34
29
 
@@ -36,6 +31,25 @@ module Throttle
36
31
  #puts "running "
37
32
  yield
38
33
  end
34
+
35
+ def within_limit?
36
+ restart_window_if_needed
37
+ @count < @max
38
+ end
39
+
40
+ def delta
41
+ now = Time.now.to_f
42
+ now - @last
43
+ end
44
+
45
+ def restart_window_if_needed
46
+ if delta >= @wait_s
47
+ #puts "resetting"
48
+ @last = Time.now.to_f
49
+ @count = 0
50
+ end
51
+ end
52
+
39
53
  end
40
54
  end
41
55
  end
@@ -0,0 +1,3 @@
1
+ module SimpleThrottle
2
+ VERSION = "0.0.11".freeze
3
+ end
@@ -1,15 +1,15 @@
1
1
  # coding: utf-8
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'throttle/version'
4
+ require 'simple_throttle/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "simple-throttle"
8
- spec.version = Throttle::VERSION
8
+ spec.version = SimpleThrottle::VERSION
9
9
  spec.authors = ["Alexandre Maia"]
10
10
  spec.email = ["alexandre.maia@gmail.com"]
11
11
  spec.summary = %q{Limit amount of executions with time windows.}
12
- spec.homepage = "https://www.github.com/cxz/simple-throttle"
12
+ spec.homepage = "https://www.github.com/cxz/simple-simple_throttle"
13
13
  spec.license = %q{MIT}
14
14
 
15
15
  # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple-throttle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexandre Maia
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-26 00:00:00.000000000 Z
11
+ date: 2016-09-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -81,11 +81,12 @@ files:
81
81
  - Rakefile
82
82
  - bin/console
83
83
  - bin/setup
84
- - lib/throttle.rb
85
- - lib/throttle/time_window/runner.rb
86
- - lib/throttle/version.rb
84
+ - lib/simple_throttle.rb
85
+ - lib/simple_throttle/manual/runner.rb
86
+ - lib/simple_throttle/time_window/runner.rb
87
+ - lib/simple_throttle/version.rb
87
88
  - simple-throttle.gemspec
88
- homepage: https://www.github.com/cxz/simple-throttle
89
+ homepage: https://www.github.com/cxz/simple-simple_throttle
89
90
  licenses:
90
91
  - MIT
91
92
  metadata: {}
@@ -1,17 +0,0 @@
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
-
@@ -1,3 +0,0 @@
1
- module Throttle
2
- VERSION = "0.0.1".freeze
3
- end