rookout 0.1.24 → 0.1.25
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rookout/augs/aug_factory.rb +7 -2
- data/lib/rookout/augs/aug_rate_limiter.rb +20 -11
- data/lib/rookout/commit.rb +1 -1
- data/lib/rookout/logger.rb +5 -2
- data/lib/rookout/processor/namespaces/ruby_object_namespace.rb +2 -0
- data/lib/rookout/utils.rb +11 -0
- data/lib/rookout/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a2bdf85b56744c08bb1dbd03f5e877a1e5ff9408f10f7ebc496c90616630d782
|
4
|
+
data.tar.gz: 8f8fd300f8ef303fb3f2ef136687ecb408aa490588af126d4ce0a6702932a948
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6e82f777a0498faee329e943cceed997111f1cb0ea75e9103294aac7b0cc53c4bd05d0af4060f748869159a1179586b0c8b0337b3652e83682f033fe2c3323a5
|
7
|
+
data.tar.gz: a765e0817ae1d2959a90c6c13902b42bb02566b1d5c1a7932fb2ca044c1c3fd1ea5882804555dca0d9c2afd6052aaa52d2dc178323b19302f0ebe8b28a25a1e2
|
@@ -10,6 +10,7 @@ module Rookout
|
|
10
10
|
require_relative "locations/location_exception_handler"
|
11
11
|
require_relative "aug_rate_limiter"
|
12
12
|
require_relative "aug"
|
13
|
+
require_relative "../utils"
|
13
14
|
|
14
15
|
class AugFactory
|
15
16
|
def initialize output
|
@@ -34,7 +35,8 @@ module Rookout
|
|
34
35
|
|
35
36
|
rate_limit = create_rate_limit configuration
|
36
37
|
|
37
|
-
|
38
|
+
max_aug_time_ns = Utils.milliseconds_to_nanoseconds max_aug_time
|
39
|
+
aug = Aug.new aug_id, action, condition, rate_limit, max_aug_time_ns
|
38
40
|
|
39
41
|
location_configuration = configuration["location"]
|
40
42
|
raise Exceptions::RookAugInvalidKey.new("location", configuration) unless location_configuration.is_a? Hash
|
@@ -72,7 +74,10 @@ module Rookout
|
|
72
74
|
|
73
75
|
rate_limit_modifier = configuration["rateLimitModifier"] || 5
|
74
76
|
|
75
|
-
|
77
|
+
window_quota_ns = Utils.milliseconds_to_nanoseconds window_quota
|
78
|
+
window_size_ns = Utils.milliseconds_to_nanoseconds window_size
|
79
|
+
|
80
|
+
AugRateLimiter.new window_quota_ns, window_size_ns, rate_limit_modifier
|
76
81
|
end
|
77
82
|
end
|
78
83
|
end
|
@@ -1,10 +1,12 @@
|
|
1
1
|
module Rookout
|
2
2
|
module Augs
|
3
3
|
require_relative "../exceptions"
|
4
|
+
require_relative "../utils"
|
4
5
|
|
5
6
|
class AugRateLimiter
|
6
7
|
def initialize quota, window_size, active_limit
|
7
8
|
@quota = quota
|
9
|
+
@has_quota = !@quota.nil? && (@quota > 0)
|
8
10
|
@window_size = window_size
|
9
11
|
@active_weight = quota / active_limit
|
10
12
|
|
@@ -18,24 +20,23 @@ module Rookout
|
|
18
20
|
start_time ||= Time.now
|
19
21
|
|
20
22
|
# If quota, verify it
|
21
|
-
if @
|
23
|
+
if @has_quota
|
22
24
|
# Get current time
|
23
|
-
|
25
|
+
now_ns = Utils.time_to_nanoseconds start_time
|
24
26
|
|
25
27
|
# Calculate window keys
|
26
|
-
current_window_key =
|
27
|
-
prev_window_key = current_window_key - @window_size
|
28
|
+
current_window_key, prev_window_key = timestamp_to_window_keys now_ns
|
28
29
|
|
29
30
|
@mutex.synchronize do
|
30
31
|
# Clean old windows
|
31
|
-
cleanup
|
32
|
+
cleanup now_ns
|
32
33
|
|
33
34
|
# Increase active count
|
34
35
|
@active_count += 1
|
35
36
|
active = true
|
36
37
|
|
37
38
|
# If exceeding quota
|
38
|
-
if current_usage(
|
39
|
+
if current_usage(now_ns, current_window_key, prev_window_key) > @quota
|
39
40
|
warning = Processor::RookError.new Exceptions::RookRuleRateLimited.new
|
40
41
|
UserWarnings.notify_warning warning
|
41
42
|
return
|
@@ -46,7 +47,9 @@ module Rookout
|
|
46
47
|
begin
|
47
48
|
yield
|
48
49
|
ensure
|
49
|
-
|
50
|
+
if @has_quota
|
51
|
+
@mutex.synchronize { record_usage current_window_key, Utils.time_to_nanoseconds(Time.now) - now_ns }
|
52
|
+
end
|
50
53
|
end
|
51
54
|
ensure
|
52
55
|
# Reduce active count
|
@@ -55,7 +58,13 @@ module Rookout
|
|
55
58
|
|
56
59
|
private
|
57
60
|
|
58
|
-
def
|
61
|
+
def timestamp_to_window_keys now_ns
|
62
|
+
current_window_key = (now_ns / @window_size) * @window_size
|
63
|
+
prev_window_key = current_window_key - @window_size
|
64
|
+
[current_window_key, prev_window_key]
|
65
|
+
end
|
66
|
+
|
67
|
+
def current_usage now_ns, current_window_key, prev_window_key
|
59
68
|
# Get usage information
|
60
69
|
current_window_usage = @windows[current_window_key]
|
61
70
|
if current_window_usage.nil?
|
@@ -65,7 +74,7 @@ module Rookout
|
|
65
74
|
prev_window_usage = @windows[prev_window_key] || 0
|
66
75
|
|
67
76
|
# Previous window weight
|
68
|
-
prev_weight = 1 - (
|
77
|
+
prev_weight = 1 - (now_ns - current_window_key) / @window_size.to_f
|
69
78
|
|
70
79
|
# Final weighted usage
|
71
80
|
(prev_window_usage * prev_weight) + (@active_count * @active_weight) + current_window_usage
|
@@ -80,14 +89,14 @@ module Rookout
|
|
80
89
|
@windows[current_window_key] += [duration, 5].max.to_f
|
81
90
|
end
|
82
91
|
|
83
|
-
def cleanup
|
92
|
+
def cleanup now_ns
|
84
93
|
# Don't bother with contention
|
85
94
|
return if @mutex.locked?
|
86
95
|
|
87
96
|
@mutex.synchronize do
|
88
97
|
# every 5 windows-times, clear windows older than 10 window-times
|
89
98
|
if @windows.length > 10
|
90
|
-
@windows.reject! { |key, _| key < (
|
99
|
+
@windows.reject! { |key, _| key < (now_ns - @window_size * 5) }
|
91
100
|
end
|
92
101
|
end
|
93
102
|
end
|
data/lib/rookout/commit.rb
CHANGED
data/lib/rookout/logger.rb
CHANGED
@@ -113,8 +113,11 @@ module Rookout
|
|
113
113
|
end
|
114
114
|
|
115
115
|
def build_handlers
|
116
|
-
|
117
|
-
|
116
|
+
if Config.logger_log_to_stderr
|
117
|
+
@handlers.push new_file_handler unless Config.logger_filename.nil? || Config.logger_filename.empty?
|
118
|
+
@handlers.push new_stderr_handler
|
119
|
+
end
|
120
|
+
|
118
121
|
@handlers.push new_remote_handler
|
119
122
|
end
|
120
123
|
|
data/lib/rookout/utils.rb
CHANGED
@@ -6,4 +6,15 @@ module Utils
|
|
6
6
|
def uuid
|
7
7
|
SecureRandom.uuid.gsub(/-/, "")
|
8
8
|
end
|
9
|
+
|
10
|
+
def milliseconds_to_nanoseconds milliseconds
|
11
|
+
nano = milliseconds * (10**6)
|
12
|
+
nano.to_i
|
13
|
+
end
|
14
|
+
|
15
|
+
def time_to_nanoseconds time_obj
|
16
|
+
secs = time_obj.to_i
|
17
|
+
nsecs = time_obj.nsec
|
18
|
+
(10**9) * secs + nsecs
|
19
|
+
end
|
9
20
|
end
|
data/lib/rookout/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rookout
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.25
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Liran Haimovitch
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-08-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: binding_of_caller
|
@@ -72,14 +72,14 @@ dependencies:
|
|
72
72
|
requirements:
|
73
73
|
- - '='
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: 3.
|
75
|
+
version: 3.17.3
|
76
76
|
type: :runtime
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
80
|
- - '='
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: 3.
|
82
|
+
version: 3.17.3
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: google-style
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|