Unthrottle 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 07bba615b8615a79a3b6980846713a150cf42d93
4
+ data.tar.gz: 9b2a1940c67ff22e17b226e9751477af87990a68
5
+ SHA512:
6
+ metadata.gz: 2fa655776bf9d8811ed294006632911ceb69ed27322f56f70e05f74e161490fd468752f601e9b0a7719fedce1e4777799f6dd37e51f7e1e6e0eeddc66cbf2cfb
7
+ data.tar.gz: 8e7644469c1d06378da77a14e73889da09d0909a665f2a52e6f571322d6dce795395abb2911298a20cd64c0e3a7834d5165b07dd6f52087cbb1b283392caf4fb
@@ -0,0 +1,35 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /vendor/bundle
26
+ /lib/bundler/man/
27
+
28
+ # for a library or gem, you might want to ignore these files since the code is
29
+ # intended to run in multiple environments; otherwise, check them in:
30
+ # Gemfile.lock
31
+ # .ruby-version
32
+ # .ruby-gemset
33
+
34
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
35
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in Unthrottle.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Shishir Sharma
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 all
13
+ 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 THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,61 @@
1
+ # Unthrottle
2
+
3
+ Centralized rate limiting for ruby processes shareing a single API using Redis.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'unthrottle'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install unthrottle
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'unthrottle'
25
+
26
+ Unthrottle.configure do |config|
27
+ config.host = "localhost"
28
+ config.port = "6379"
29
+ config.db = "test"
30
+
31
+ config.key = "google_geocoding_api"
32
+ config.rate_limit_time = 3 # Millisecs
33
+ config.limit = 3 # Number of api call during timeout
34
+
35
+ config.logger = Logger.new(STDOUT)
36
+ end
37
+
38
+ Unthrottle.api(:timeout => 10) {
39
+ #<Your method call>
40
+ }
41
+ ```
42
+
43
+ ## Development
44
+
45
+ After checking out the repo, run `bin/setup` to install
46
+ dependencies. Then, run `bin/console` for an interactive prompt that
47
+ will allow you to experiment.
48
+
49
+ To install this gem onto your local machine, run `bundle exec rake
50
+ install`. To release a new version, update the version number in
51
+ `version.rb`, and then run `bundle exec rake release` to create a git
52
+ tag for the version, push git commits and tags, and push the `.gem`
53
+ file to [rubygems.org](https://rubygems.org).
54
+
55
+ ## Contributing
56
+
57
+ 1. Fork it ( https://github.com/shishirsharma/Unthrottle/fork )
58
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
59
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
60
+ 4. Push to the branch (`git push origin my-new-feature`)
61
+ 5. Create a new Pull Request
@@ -0,0 +1,3 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ Dir.glob('tasks/**/*.rake').each(&method(:import))
@@ -0,0 +1,85 @@
1
+ require 'unthrottle/version'
2
+ require 'redis'
3
+ require 'timeout'
4
+
5
+ module Unthrottle
6
+ class Configuration
7
+ attr_accessor :redis, :host, :port, :db,
8
+ :key, :rate_limit_time, :limit,
9
+ :logger,
10
+ :log_level
11
+
12
+ def initialize
13
+ @host = 'localhost'
14
+ @port = 6379
15
+ @db = 'test'
16
+
17
+ # Key name => time out in sec
18
+ @key = :global # Api key name
19
+ @rate_limit_time = 60 # Expire time for this key
20
+ @limit = 10 # Api call limit in timeout periode
21
+ @log_level = Logger::INFO
22
+ end
23
+ end
24
+
25
+ class << self
26
+ attr_accessor :config
27
+ end
28
+
29
+ def configure
30
+ @config ||= Configuration.new
31
+ yield(config) if block_given?
32
+ # Create a redis connection if not provided with
33
+ @config.redis ||=
34
+ Redis.new(host: config.host, port: config.port,
35
+ db: config.db)
36
+ @config.logger ||= Logger.new(STDOUT)
37
+ @config.logger.level = @config.log_level
38
+ end
39
+
40
+ # Register an Api Call in redis
41
+ #
42
+ # @param key Api key name to register call with
43
+ # @return current count of this key
44
+ def register_api_call(key)
45
+ keyname = "Unthrottle:#{key}".to_sym # Puting key names in a namespace
46
+ rate_limit_time = @config.rate_limit_time
47
+ # This can possibly return nil and integer
48
+ count = @config.redis.get(keyname).to_i
49
+ # Run it as a redis transaction
50
+ @config.redis.multi do |redis|
51
+ redis.incr(keyname)
52
+ # Set expire if this is a first time we call api
53
+ if count == 0
54
+ redis.expire(keyname, rate_limit_time)
55
+ end
56
+ end
57
+
58
+ return count + 1
59
+ end
60
+
61
+ # Run an api with rate limit
62
+ #
63
+ # @param [Hash] opts input options
64
+ # @options opts [Integer] :timeout milli seconds to wait before you retry api
65
+ def api(timeout: 60)
66
+ sleep_time = 100/1000.0 # 100 millisecs
67
+ Timeout::timeout(timeout) do # secs
68
+ loop do
69
+ count = self.register_api_call(@config.key)
70
+ @config.logger.debug("Attempt api with #{count}")
71
+ if count <= @config.limit
72
+ @config.logger.debug("Going through with #{count}")
73
+ yield # Call the block provided
74
+ return
75
+ end
76
+ @config.logger.info("Rate limit hit! going to sleep with #{sleep_time}")
77
+ sleep(sleep_time) # milli secs
78
+ # Geometric sleep progression for now
79
+ sleep_time += sleep_time
80
+ end
81
+ end
82
+ end
83
+
84
+ module_function :configure, :register_api_call, :api
85
+ end
@@ -0,0 +1,3 @@
1
+ module Unthrottle
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,3 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new(:spec)
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Unthrottle
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Shishir Sharma
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-05-09 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: 3.2.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 3.2.1
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.8'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Centralized rate limiting for multiple web servies, using Redis.
56
+ email:
57
+ - shishirsharma.in@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE
65
+ - README.md
66
+ - Rakefile
67
+ - lib/Unthrottle.rb
68
+ - lib/unthrottle/version.rb
69
+ - tasks/rspec.rake
70
+ homepage: https://github.com/shishirsharma/Unthrottle
71
+ licenses:
72
+ - MIT
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.0.3
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Centralized rate limiting for things, using Redis.
94
+ test_files: []