rollie 0.0.1

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: 1a4104ef316b1bf29321667fa9ea4e44dd642f4e
4
+ data.tar.gz: c695f97168e7ea2d7f47fe0a118bef23d8f5e24e
5
+ SHA512:
6
+ metadata.gz: 7f9da971ea951921729261a1920e640dfa9b6d1b8fc4580bb87454e6d7657a90583a52b17005a38ee61fdbfaa0dd7d38501e91cea39e9feefd7bbaf73e5c3068
7
+ data.tar.gz: a3eb75347541a6092ec9a8bfc085dbb5e0368560abf6dd143892d625b6be6f51e3b7693f208b08a7dcc2a287086d4922ad4d2d7afc6ba90d70e5f1924d0a31fe
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2016 Zach Davis
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.
@@ -0,0 +1,5 @@
1
+ # Rollie
2
+
3
+ Rollie is a generic rate limiter backed by Redis for efficient limiting using sliding windows.
4
+
5
+ [![Build Status](https://travis-ci.org/zldavis/rollie.svg?branch=master)](https://travis-ci.org/zldavis/rollie)
@@ -0,0 +1,28 @@
1
+ require "rollie/rate_limiter"
2
+ require "rollie/redis_pool"
3
+ require "rollie/status"
4
+ require "rollie/version"
5
+
6
+ module Rollie
7
+ class << self
8
+ def redis
9
+ raise ArgumentError, "requires a block" unless block_given?
10
+ redis_pool.with do |conn|
11
+ yield(conn)
12
+ end
13
+ end
14
+
15
+ def redis=(hash)
16
+ @redis_pool = if hash.is_a?(ConnectionPool)
17
+ hash
18
+ else
19
+ Rollie::RedisPool.create(hash)
20
+ end
21
+ end
22
+
23
+ def redis_pool
24
+ @redis_pool ||= Rollie::RedisPool.create
25
+ end
26
+ end
27
+
28
+ end
@@ -0,0 +1,42 @@
1
+ module Rollie
2
+
3
+ class RateLimiter
4
+
5
+ def initialize(key, options = {})
6
+ @key = "#{options[:namespace]}#{key}"
7
+ @limit = options[:limit] || 25
8
+ @interval = (options[:interval] || 1000) * 1000
9
+ end
10
+
11
+ def within_limit
12
+ raise ArgumentError, "requires a block" unless block_given?
13
+
14
+ Rollie.redis do |conn|
15
+ status = inc(conn)
16
+ unless status.exceeded?
17
+ yield
18
+ end
19
+ status
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def inc(conn)
26
+ time = (Time.now.to_r * 1000000).round
27
+ old = time - @interval
28
+ range = conn.multi do
29
+ conn.zremrangebyscore(@key, 0, old)
30
+ conn.zadd(@key, time, time)
31
+ conn.zrange(@key, 0, -1)
32
+ conn.expire(@key, (@interval / 1000000).ceil)
33
+ end[2]
34
+
35
+ exceeded = range.length > @limit
36
+ time_remaining = range.first.to_i - time + @interval
37
+ current_count = exceeded ? @limit : range.length
38
+ Rollie::Status.new((time_remaining / 1000).floor, current_count, exceeded)
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,37 @@
1
+ require "connection_pool"
2
+ require "redis"
3
+ require "redis-namespace"
4
+
5
+ module Rollie
6
+ class RedisPool
7
+ class << self
8
+
9
+ def create(options={})
10
+ puts "initialized with options: #{options}"
11
+ pool_size = options[:pool_size] || 5
12
+ pool_timeout = options[:pool_timeout] || 1
13
+
14
+ ConnectionPool.new(:timeout => pool_timeout, :size => pool_size) do
15
+ build_client(options)
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def build_client(options)
22
+ namespace = options[:namespace] || "Rollie"
23
+ client = Redis.new redis_options(options)
24
+ Redis::Namespace.new(namespace, redis: client)
25
+ end
26
+
27
+ def redis_options(options)
28
+ redis = options.dup
29
+ redis[:url] ||= ENV["REDIS_URL"]
30
+ redis[:driver] ||= "ruby"
31
+ redis.delete(:namespace)
32
+ redis
33
+ end
34
+
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,15 @@
1
+ module Rollie
2
+ class Status
3
+ attr_accessor :time_remaining, :count
4
+
5
+ def initialize(time_remaining, count, exceeded)
6
+ @time_remaining = time_remaining
7
+ @count = count
8
+ @exceeded = exceeded
9
+ end
10
+
11
+ def exceeded?
12
+ @exceeded
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module Rollie
2
+ VERSION = "0.0.1".freeze
3
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rollie
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Zach Davis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-12-03 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'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 3.2.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '3.2'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 3.2.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: redis-namespace
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.5'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 1.5.2
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '1.5'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 1.5.2
53
+ - !ruby/object:Gem::Dependency
54
+ name: connection_pool
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '2.2'
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 2.2.0
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '2.2'
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 2.2.0
73
+ description: Generic rate limiter backed by Redis for efficient limiting using sliding
74
+ windows.
75
+ email: zldavis@gmail.com
76
+ executables: []
77
+ extensions: []
78
+ extra_rdoc_files:
79
+ - README.md
80
+ - LICENSE
81
+ files:
82
+ - LICENSE
83
+ - README.md
84
+ - lib/rollie.rb
85
+ - lib/rollie/rate_limiter.rb
86
+ - lib/rollie/redis_pool.rb
87
+ - lib/rollie/status.rb
88
+ - lib/rollie/version.rb
89
+ homepage: https://github.com/zldavis/rollie
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.5.2
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Generic rate limiter backed by Redis for efficient limiting using sliding
113
+ windows.
114
+ test_files: []