bottleneck 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -1
- data/Gemfile.lock +6 -3
- data/README.md +24 -11
- data/bottleneck.gemspec +1 -0
- data/config/bottleneck.yml +3 -0
- data/config/redis.yml +2 -0
- data/lib/bottleneck/core.rb +2 -2
- data/lib/bottleneck/version.rb +1 -1
- data/lib/bottleneck.rb +12 -5
- metadata +17 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 42281ac9ac20f1a15452c335a67fa510bc4cf4a1
|
4
|
+
data.tar.gz: 21f2ebab21d35f82ed6a67a5406ae376e71d1ffa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 895fc354c07a4d1f775c087ed100ff4e257a6a51d544560fe81b8522ec06242897ad9a29df5257ac4768b6b2a13a969df3007032b7f6ad5861739adaf3d993a6
|
7
|
+
data.tar.gz: eae36948a25b82c5f1b3a2f5b86e39bb163a0866c10629de0c78959f7902b7a2d7b486abd04fd3b222281ea9da65130fafcf0f22e102b56b5e3701a5b20e4088
|
data/.rubocop.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
bottleneck (0.4.
|
5
|
-
redis
|
4
|
+
bottleneck (0.4.1)
|
5
|
+
redis (~> 4.0)
|
6
|
+
redis-namespace (~> 1.6.0)
|
6
7
|
|
7
8
|
GEM
|
8
9
|
remote: https://rubygems.org/
|
@@ -16,6 +17,8 @@ GEM
|
|
16
17
|
rainbow (3.0.0)
|
17
18
|
rake (10.5.0)
|
18
19
|
redis (4.0.1)
|
20
|
+
redis-namespace (1.6.0)
|
21
|
+
redis (>= 3.0.4)
|
19
22
|
rspec (3.7.0)
|
20
23
|
rspec-core (~> 3.7.0)
|
21
24
|
rspec-expectations (~> 3.7.0)
|
@@ -47,7 +50,7 @@ DEPENDENCIES
|
|
47
50
|
bundler (~> 1.16)
|
48
51
|
rake (~> 10.0)
|
49
52
|
rspec (~> 3.0)
|
50
|
-
rubocop
|
53
|
+
rubocop (~> 0.52.1)
|
51
54
|
|
52
55
|
BUNDLED WITH
|
53
56
|
1.16.0
|
data/README.md
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
# Bottleneck
|
2
2
|
|
3
|
-
|
3
|
+
Bottleneck - simple Redis based requests limiter with two params:
|
4
4
|
|
5
|
-
|
5
|
+
* time_period_seconds: 3600
|
6
|
+
* max_requests_count: 100
|
6
7
|
|
7
8
|
## Installation
|
8
9
|
|
@@ -22,22 +23,34 @@ Or install it yourself as:
|
|
22
23
|
|
23
24
|
## Usage
|
24
25
|
|
25
|
-
|
26
|
+
Create two yaml files in config dir:
|
26
27
|
|
27
|
-
|
28
|
+
**bottleneck.yml**
|
28
29
|
|
29
|
-
|
30
|
+
limits:
|
31
|
+
time_period_seconds: 3600
|
32
|
+
max_requests_count: 100
|
30
33
|
|
31
|
-
|
34
|
+
**redis.yml**
|
35
|
+
host: 'localhost'
|
36
|
+
port: 6379
|
37
|
+
|
38
|
+
Add before action:
|
39
|
+
class ApplicationController < ActionController::API
|
40
|
+
before_action :check_limit
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def check_limit
|
45
|
+
result = Bottleneck.check(request)
|
46
|
+
render status: result[:status], json: { message: result[:message] }
|
47
|
+
end
|
48
|
+
end
|
32
49
|
|
33
50
|
## Contributing
|
34
51
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
52
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/maratgaliev/bottleneck. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
53
|
|
37
54
|
## License
|
38
55
|
|
39
56
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
40
|
-
|
41
|
-
## Code of Conduct
|
42
|
-
|
43
|
-
Everyone interacting in the Bottleneck project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/bottleneck/blob/master/CODE_OF_CONDUCT.md).
|
data/bottleneck.gemspec
CHANGED
data/config/redis.yml
ADDED
data/lib/bottleneck/core.rb
CHANGED
data/lib/bottleneck/version.rb
CHANGED
data/lib/bottleneck.rb
CHANGED
@@ -2,23 +2,28 @@ require "bottleneck/version"
|
|
2
2
|
require "bottleneck/core"
|
3
3
|
require "yaml"
|
4
4
|
require "redis"
|
5
|
+
require "redis-namespace"
|
5
6
|
|
6
7
|
module Bottleneck
|
7
8
|
class << self
|
8
|
-
def check(
|
9
|
-
Core.new(
|
9
|
+
def check(ip)
|
10
|
+
Core.new(ip).run
|
10
11
|
end
|
11
12
|
|
12
13
|
def storage
|
13
14
|
init_storage
|
14
15
|
end
|
15
16
|
|
16
|
-
|
17
|
-
def init_storage
|
17
|
+
def redis_conn
|
18
18
|
redis_conf = load_config("redis.yml")
|
19
19
|
Redis.new(host: redis_conf["host"], port: redis_conf["port"])
|
20
20
|
end
|
21
21
|
|
22
|
+
# Init redis client from config
|
23
|
+
def init_storage
|
24
|
+
Redis::Namespace.new(:bottleneck, redis: redis_conn)
|
25
|
+
end
|
26
|
+
|
22
27
|
# Get limits from config
|
23
28
|
def config
|
24
29
|
load_config("bottleneck.yml")
|
@@ -27,7 +32,9 @@ module Bottleneck
|
|
27
32
|
private
|
28
33
|
|
29
34
|
def load_config(file)
|
30
|
-
|
35
|
+
# Check for Rails defined, or use current path
|
36
|
+
root = (defined?(Rails) && Rails.respond_to?(:root) && Rails.root) || Dir.pwd
|
37
|
+
path = "#{root}/config/#{file}"
|
31
38
|
raise "No #{file} file found in your config directory!" unless File.exist?(path)
|
32
39
|
YAML.load_file(path)
|
33
40
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bottleneck
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marat Galiev
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '4.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: redis-namespace
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 1.6.0
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 1.6.0
|
83
97
|
description: Simple Redis based Rate Limiter for Rails applications
|
84
98
|
email:
|
85
99
|
- kazanlug@gmail.com
|
@@ -99,6 +113,8 @@ files:
|
|
99
113
|
- bin/console
|
100
114
|
- bin/setup
|
101
115
|
- bottleneck.gemspec
|
116
|
+
- config/bottleneck.yml
|
117
|
+
- config/redis.yml
|
102
118
|
- lib/bottleneck.rb
|
103
119
|
- lib/bottleneck/constants.rb
|
104
120
|
- lib/bottleneck/core.rb
|