traffic_jam 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 805e6a84771b1d0933268ad372a33224ac7f5d26
|
4
|
+
data.tar.gz: 978bfe19a6637a37efa7b501c53a919104165d18
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eb81caf639081a1313d893e2c3cc61e8c4d097061e4a6abbb3fbfcd03936dc4709f7fb745d9e206252265e61f3ed41d4351752657fd47d2990b70f2db8ac9a5f
|
7
|
+
data.tar.gz: abd29587f6e1bbda2f5a3abba3bf470429846d0c928bf9d2d40797b17b4c66881395846f199305a874bf6dad778bb4edac39dbb0627b7eb0f7bf69caeec88014
|
data/lib/traffic_jam.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
require 'ostruct'
|
2
2
|
require 'digest/md5'
|
3
|
-
require_relative 'traffic_jam/errors'
|
4
3
|
require_relative 'traffic_jam/configuration'
|
4
|
+
require_relative 'traffic_jam/errors'
|
5
|
+
require_relative 'traffic_jam/gcra_limit'
|
6
|
+
require_relative 'traffic_jam/lifetime_limit'
|
5
7
|
require_relative 'traffic_jam/limit'
|
6
8
|
require_relative 'traffic_jam/limit_group'
|
7
|
-
require_relative 'traffic_jam/simple_limit'
|
8
9
|
require_relative 'traffic_jam/rolling_limit'
|
9
|
-
require_relative 'traffic_jam/lifetime_limit'
|
10
10
|
|
11
11
|
module TrafficJam
|
12
12
|
include Errors
|
@@ -2,21 +2,28 @@ require_relative 'limit'
|
|
2
2
|
require_relative 'scripts'
|
3
3
|
|
4
4
|
module TrafficJam
|
5
|
-
#
|
6
|
-
#
|
7
|
-
# This means that if the period or max value for an action, value key changes,
|
8
|
-
# the used and remaining values cannot be preserved.
|
9
|
-
#
|
10
|
-
# This works by storing a key in Redis with a millisecond-precision expiry
|
5
|
+
# GCRA (Generic Cell Rate Algorithm) is a leaky bucket type rate limiting
|
6
|
+
# algorithm. GCRA works by storing a key in Redis with a ms-precision expiry
|
11
7
|
# representing the time that the limit will be completely reset. Each
|
12
8
|
# increment operation converts the increment amount into the number of
|
13
9
|
# milliseconds to be added to the expiry.
|
14
10
|
#
|
11
|
+
# When a request comes in, we take the existing expiry value, subtract a fixed
|
12
|
+
# amount representing the limit’s total burst capacity from it, and compare
|
13
|
+
# the result to the current time. This result represents the next time to
|
14
|
+
# allow a request. If it’s in the past, we allow the incoming request, and if
|
15
|
+
# it’s in the future, we don’t. After a successful request, a new expiry is
|
16
|
+
# calculated. (see https://brandur.org/rate-limiting)
|
17
|
+
#
|
18
|
+
# This limit type does not support decrements or changing the max value without
|
19
|
+
# a complete reset. This means that if the period or max value for an
|
20
|
+
# action/value key changes, the used and remaining values cannot be preserved.
|
21
|
+
#
|
15
22
|
# Example: Limit is 5 per 10 seconds.
|
16
23
|
# An increment by 1 first sets the key to expire in 2s.
|
17
24
|
# Another immediate increment by 4 sets the expiry to 10s.
|
18
25
|
# Subsequent increments fail until clock time catches up to expiry
|
19
|
-
class
|
26
|
+
class GCRALimit < Limit
|
20
27
|
# Increment the amount used by the given number. Does not perform increment
|
21
28
|
# if the operation would exceed the limit. Returns whether the operation was
|
22
29
|
# successful.
|
@@ -42,9 +49,9 @@ module TrafficJam
|
|
42
49
|
result =
|
43
50
|
begin
|
44
51
|
redis.evalsha(
|
45
|
-
Scripts::
|
52
|
+
Scripts::INCREMENT_GCRA_HASH, keys: [key], argv: argv)
|
46
53
|
rescue Redis::CommandError
|
47
|
-
redis.eval(Scripts::
|
54
|
+
redis.eval(Scripts::INCREMENT_GCRA, keys: [key], argv: argv)
|
48
55
|
end
|
49
56
|
|
50
57
|
case result
|
@@ -57,7 +64,7 @@ module TrafficJam
|
|
57
64
|
else
|
58
65
|
raise Errors::UnknownReturnValue,
|
59
66
|
"Received unexpected return value #{result} from " \
|
60
|
-
"
|
67
|
+
"increment_gcra eval"
|
61
68
|
end
|
62
69
|
end
|
63
70
|
|
@@ -91,4 +98,7 @@ module TrafficJam
|
|
91
98
|
"#{config.key_prefix}:s"
|
92
99
|
end
|
93
100
|
end
|
101
|
+
|
102
|
+
# alias for backward compatibility
|
103
|
+
SimpleLimit = GCRALimit
|
94
104
|
end
|
data/lib/traffic_jam/scripts.rb
CHANGED
@@ -10,8 +10,8 @@ module TrafficJam
|
|
10
10
|
|
11
11
|
INCREMENT_SCRIPT = load('increment')
|
12
12
|
INCREMENT_SCRIPT_HASH = Digest::SHA1.hexdigest(INCREMENT_SCRIPT)
|
13
|
-
|
14
|
-
|
13
|
+
INCREMENT_GCRA = load('increment_gcra')
|
14
|
+
INCREMENT_GCRA_HASH = Digest::SHA1.hexdigest(INCREMENT_GCRA)
|
15
15
|
INCREMENT_ROLLING = load('increment_rolling')
|
16
16
|
INCREMENT_ROLLING_HASH = Digest::SHA1.hexdigest(INCREMENT_ROLLING)
|
17
17
|
INCRBY = load('incrby')
|
File without changes
|
metadata
CHANGED
@@ -1,29 +1,35 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: traffic_jam
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jim Posen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-09-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '3.0'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '5'
|
20
23
|
type: :runtime
|
21
24
|
prerelease: false
|
22
25
|
version_requirements: !ruby/object:Gem::Requirement
|
23
26
|
requirements:
|
24
|
-
- - "
|
27
|
+
- - ">="
|
25
28
|
- !ruby/object:Gem::Version
|
26
29
|
version: '3.0'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '5'
|
27
33
|
- !ruby/object:Gem::Dependency
|
28
34
|
name: rake
|
29
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -47,16 +53,16 @@ files:
|
|
47
53
|
- lib/traffic_jam.rb
|
48
54
|
- lib/traffic_jam/configuration.rb
|
49
55
|
- lib/traffic_jam/errors.rb
|
56
|
+
- lib/traffic_jam/gcra_limit.rb
|
50
57
|
- lib/traffic_jam/lifetime_limit.rb
|
51
58
|
- lib/traffic_jam/limit.rb
|
52
59
|
- lib/traffic_jam/limit_group.rb
|
53
60
|
- lib/traffic_jam/rolling_limit.rb
|
54
61
|
- lib/traffic_jam/scripts.rb
|
55
|
-
- lib/traffic_jam/simple_limit.rb
|
56
62
|
- scripts/incrby.lua
|
57
63
|
- scripts/increment.lua
|
64
|
+
- scripts/increment_gcra.lua
|
58
65
|
- scripts/increment_rolling.lua
|
59
|
-
- scripts/increment_simple.lua
|
60
66
|
- scripts/sum_rolling.lua
|
61
67
|
homepage: https://github.com/coinbase/traffic_jam
|
62
68
|
licenses:
|
@@ -78,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
78
84
|
version: '0'
|
79
85
|
requirements: []
|
80
86
|
rubyforge_project:
|
81
|
-
rubygems_version: 2.6.
|
87
|
+
rubygems_version: 2.6.8
|
82
88
|
signing_key:
|
83
89
|
specification_version: 4
|
84
90
|
summary: Library for time-based rate limiting
|