rack-berater 0.3.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rack/berater/prioritizer.rb +6 -20
- data/lib/rack/berater/rails_prioritizer.rb +17 -0
- data/lib/rack/berater/railtie.rb +1 -1
- data/lib/rack/berater/rspec.rb +9 -0
- data/lib/rack/berater/version.rb +1 -1
- data/lib/rack/berater.rb +1 -0
- data/spec/prioritizer_spec.rb +5 -87
- data/spec/rails_prioritizer_spec.rb +100 -0
- metadata +20 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d7cdec5112bd0a056e38bba20ca0c6c25c84f6703f1c031469b815806ef7f25a
|
4
|
+
data.tar.gz: 390634760abee230969aa03f51ad7eb86dc3372f17d4d0c1597cb759db4804fa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cae54a952dec822b533f3f16d555cc5215e888f0783c3fc5d9d266cc8f7b7f14884068e603e19e4e2c1880adcff1bd2adbf73de025cc93095da14b105f89cc8e
|
7
|
+
data.tar.gz: 44b0008bc762dfd16b28a9a731ee8c5be5a766e48341828b7c49df9967fbb48cc213955c199b61cbd5abae2bae7e5b4b207ce46e8e6afdd22564d916be49f73a
|
@@ -9,6 +9,8 @@ module Rack
|
|
9
9
|
def initialize(app, options = {})
|
10
10
|
@app = app
|
11
11
|
@header = options[:header] || HEADER
|
12
|
+
|
13
|
+
synchronize { @@cache ||= {} }
|
12
14
|
end
|
13
15
|
|
14
16
|
def call(env)
|
@@ -48,31 +50,15 @@ module Rack
|
|
48
50
|
Thread.current[ENV_KEY] = priority
|
49
51
|
end
|
50
52
|
|
51
|
-
@@cache_prefix = 'Berater:Rack:Prioritizer'
|
52
53
|
def cache_key_for(env)
|
53
|
-
req = Rack::Request.new(env)
|
54
|
-
|
55
|
-
req_method = env[Rack::REQUEST_METHOD].downcase
|
56
|
-
path = ''
|
57
|
-
|
58
|
-
if defined?(Rails) && Rails.application
|
59
|
-
res = Rails.application.routes.recognize_path(env[Rack::PATH_INFO])
|
60
|
-
path = res.values_at(:controller, :action).compact.join('#')
|
61
|
-
end
|
62
|
-
|
63
|
-
if path.empty?
|
64
|
-
path = env['PATH_INFO'].gsub(%r{/[0-9]+(/|$)}, '/x\1')
|
65
|
-
end
|
66
|
-
|
67
54
|
[
|
68
|
-
|
69
|
-
|
70
|
-
|
55
|
+
env[Rack::REQUEST_METHOD].downcase,
|
56
|
+
|
57
|
+
# normalize RESTful paths
|
58
|
+
env['PATH_INFO'].gsub(%r{/[0-9]+(/|$)}, '/x\1'),
|
71
59
|
].join(':')
|
72
60
|
end
|
73
61
|
|
74
|
-
@@cache = {}
|
75
|
-
|
76
62
|
def cache_get(key)
|
77
63
|
synchronize { @@cache[key] }
|
78
64
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'action_controller/metal'
|
2
|
+
require 'action_dispatch'
|
3
|
+
|
4
|
+
module Rack
|
5
|
+
class Berater
|
6
|
+
class RailsPrioritizer < Prioritizer
|
7
|
+
def cache_key_for(env)
|
8
|
+
Rails.application.routes.recognize_path(
|
9
|
+
env[Rack::PATH_INFO],
|
10
|
+
method: env[Rack::REQUEST_METHOD],
|
11
|
+
).values_at(:controller, :action).compact.join('#')
|
12
|
+
rescue ActionController::RoutingError
|
13
|
+
super
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/rack/berater/railtie.rb
CHANGED
@@ -5,7 +5,7 @@ module Rack
|
|
5
5
|
class Railtie < Rails::Railtie
|
6
6
|
initializer 'rack.berater' do |app|
|
7
7
|
if ::Berater.middleware.include?(::Berater::Middleware::LoadShedder)
|
8
|
-
app.middleware.use Rack::Berater::
|
8
|
+
app.middleware.use Rack::Berater::RailsPrioritizer
|
9
9
|
end
|
10
10
|
|
11
11
|
app.middleware.use Rack::Berater
|
data/lib/rack/berater/version.rb
CHANGED
data/lib/rack/berater.rb
CHANGED
data/spec/prioritizer_spec.rb
CHANGED
@@ -1,9 +1,4 @@
|
|
1
1
|
describe Rack::Berater::Prioritizer do
|
2
|
-
after do
|
3
|
-
cache.clear
|
4
|
-
Thread.current[described_class::ENV_KEY] = nil
|
5
|
-
end
|
6
|
-
|
7
2
|
let(:cache) { described_class.class_variable_get(:@@cache) }
|
8
3
|
|
9
4
|
describe '#call' do
|
@@ -117,12 +112,8 @@ describe Rack::Berater::Prioritizer do
|
|
117
112
|
context 'with a basic env' do
|
118
113
|
let(:env) { Rack::MockRequest.env_for('/') }
|
119
114
|
|
120
|
-
it 'has a Berater prefix' do
|
121
|
-
is_expected.to match /^Berater:/
|
122
|
-
end
|
123
|
-
|
124
115
|
it 'combines the verb and path' do
|
125
|
-
is_expected.to match %r{
|
116
|
+
is_expected.to match %r{get:/$}
|
126
117
|
end
|
127
118
|
end
|
128
119
|
|
@@ -130,7 +121,7 @@ describe Rack::Berater::Prioritizer do
|
|
130
121
|
let(:env) { Rack::MockRequest.env_for('/', method: 'PUT') }
|
131
122
|
|
132
123
|
it 'combines the verb and path' do
|
133
|
-
is_expected.to match %r{
|
124
|
+
is_expected.to match %r{put:/$}
|
134
125
|
end
|
135
126
|
end
|
136
127
|
|
@@ -138,7 +129,7 @@ describe Rack::Berater::Prioritizer do
|
|
138
129
|
let(:env) { Rack::MockRequest.env_for('/user/123') }
|
139
130
|
|
140
131
|
it 'normalizes the id' do
|
141
|
-
is_expected.to match %r{:/user/x$}
|
132
|
+
is_expected.to match %r{get:/user/x$}
|
142
133
|
end
|
143
134
|
end
|
144
135
|
|
@@ -146,7 +137,7 @@ describe Rack::Berater::Prioritizer do
|
|
146
137
|
let(:env) { Rack::MockRequest.env_for('/user/123/') }
|
147
138
|
|
148
139
|
it 'normalizes the id and keeps the trailing slash' do
|
149
|
-
is_expected.to match %r{:/user/x/$}
|
140
|
+
is_expected.to match %r{get:/user/x/$}
|
150
141
|
end
|
151
142
|
end
|
152
143
|
|
@@ -154,7 +145,7 @@ describe Rack::Berater::Prioritizer do
|
|
154
145
|
let(:env) { Rack::MockRequest.env_for('/user/123/friend/456') }
|
155
146
|
|
156
147
|
it 'normalizes both ids' do
|
157
|
-
is_expected.to match %r{:/user/x/friend/x$}
|
148
|
+
is_expected.to match %r{get:/user/x/friend/x$}
|
158
149
|
end
|
159
150
|
end
|
160
151
|
end
|
@@ -219,77 +210,4 @@ describe Rack::Berater::Prioritizer do
|
|
219
210
|
# end
|
220
211
|
# end
|
221
212
|
end
|
222
|
-
|
223
|
-
context 'as Rails middleware' do
|
224
|
-
before do
|
225
|
-
class EchoController < ActionController::Base
|
226
|
-
def index
|
227
|
-
render plain: Rack::Berater::Prioritizer.current_priority
|
228
|
-
end
|
229
|
-
|
230
|
-
def six
|
231
|
-
response.set_header(Rack::Berater::Prioritizer::HEADER, '6')
|
232
|
-
index
|
233
|
-
end
|
234
|
-
|
235
|
-
def nine
|
236
|
-
response.set_header(Rack::Berater::Prioritizer::HEADER, '9')
|
237
|
-
index
|
238
|
-
end
|
239
|
-
end
|
240
|
-
|
241
|
-
Rails.application = Class.new(Rails::Application) do
|
242
|
-
config.eager_load = false
|
243
|
-
config.hosts.clear # disable hostname filtering
|
244
|
-
end
|
245
|
-
Rails.application.middleware.use described_class
|
246
|
-
Rails.initialize!
|
247
|
-
|
248
|
-
Rails.application.routes.draw do
|
249
|
-
get '/' => 'echo#index'
|
250
|
-
get '/six' => 'echo#six'
|
251
|
-
get '/nine' => 'echo#nine'
|
252
|
-
end
|
253
|
-
end
|
254
|
-
|
255
|
-
let(:app) { Rails.application }
|
256
|
-
let(:middleware) { described_class.new(app) }
|
257
|
-
|
258
|
-
after { Rails.application = nil }
|
259
|
-
|
260
|
-
describe '#cache_key_for' do
|
261
|
-
subject { described_class.new(app).send(:cache_key_for, env) }
|
262
|
-
|
263
|
-
let(:env) { Rack::MockRequest.env_for('/') }
|
264
|
-
|
265
|
-
it 'uses the controller and action name' do
|
266
|
-
is_expected.to match %r{:echo#index$}
|
267
|
-
end
|
268
|
-
end
|
269
|
-
|
270
|
-
context 'when a priority header is sent' do
|
271
|
-
before { header described_class::HEADER, priority }
|
272
|
-
|
273
|
-
let(:priority) { '6' }
|
274
|
-
|
275
|
-
it 'sets the priority' do
|
276
|
-
expect(get('/six').body).to eq priority
|
277
|
-
end
|
278
|
-
end
|
279
|
-
|
280
|
-
context 'when the app returns a priority' do
|
281
|
-
it 'does not know the first time the controller is called' do
|
282
|
-
expect(get('/six').body).to be_empty
|
283
|
-
expect(get('/nine').body).to be_empty
|
284
|
-
end
|
285
|
-
|
286
|
-
it 'caches the repsonses for the second time' do
|
287
|
-
expect(get('/six').body).to be_empty
|
288
|
-
expect(get('/nine').body).to be_empty
|
289
|
-
|
290
|
-
expect(get('/six').body).to eq '6'
|
291
|
-
expect(get('/nine').body).to eq '9'
|
292
|
-
end
|
293
|
-
end
|
294
|
-
end
|
295
213
|
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'rspec/rails'
|
2
|
+
|
3
|
+
describe Rack::Berater::RailsPrioritizer do
|
4
|
+
before do
|
5
|
+
class EchoController < ActionController::Base
|
6
|
+
def index
|
7
|
+
render plain: Rack::Berater::Prioritizer.current_priority
|
8
|
+
end
|
9
|
+
|
10
|
+
def six
|
11
|
+
response.set_header(Rack::Berater::Prioritizer::HEADER, '6')
|
12
|
+
index
|
13
|
+
end
|
14
|
+
|
15
|
+
def nine
|
16
|
+
response.set_header(Rack::Berater::Prioritizer::HEADER, '9')
|
17
|
+
index
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
Rails.application = Class.new(Rails::Application) do
|
22
|
+
config.eager_load = false
|
23
|
+
config.hosts.clear # disable hostname filtering
|
24
|
+
# config.logger = ActiveSupport::Logger.new($stdout)
|
25
|
+
end
|
26
|
+
Rails.application.middleware.use described_class
|
27
|
+
Rails.initialize!
|
28
|
+
|
29
|
+
Rails.application.routes.draw do
|
30
|
+
get '/' => 'echo#index'
|
31
|
+
get '/six' => 'echo#six'
|
32
|
+
post '/nine' => 'echo#nine'
|
33
|
+
get '/redirect' => redirect('/')
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
let(:app) { Rails.application }
|
38
|
+
let(:middleware) { described_class.new(app) }
|
39
|
+
|
40
|
+
after do
|
41
|
+
Rails.application = nil
|
42
|
+
end
|
43
|
+
|
44
|
+
let(:cache) { described_class.class_variable_get(:@@cache) }
|
45
|
+
|
46
|
+
describe '#cache_key_for' do
|
47
|
+
subject { described_class.new(app).method(:cache_key_for) }
|
48
|
+
|
49
|
+
it 'uses the controller and action name' do
|
50
|
+
expect(
|
51
|
+
subject.call(Rack::MockRequest.env_for('/'))
|
52
|
+
).to match /echo#index/
|
53
|
+
|
54
|
+
expect(
|
55
|
+
subject.call(Rack::MockRequest.env_for('/six'))
|
56
|
+
).to match /echo#six/
|
57
|
+
|
58
|
+
expect(
|
59
|
+
subject.call(Rack::MockRequest.env_for('/nine', method: 'POST'))
|
60
|
+
).to match /echo#nine/
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'falls back to Rack style names' do
|
64
|
+
expect(
|
65
|
+
subject.call(Rack::MockRequest.env_for('/nine'))
|
66
|
+
).to match %r{get:/nine}
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'works with redirects' do
|
70
|
+
expect(
|
71
|
+
subject.call(Rack::MockRequest.env_for('/redirect'))
|
72
|
+
).to match %r{get:/redirect}
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context 'when a priority header is sent' do
|
77
|
+
before { header described_class::HEADER, priority }
|
78
|
+
|
79
|
+
let(:priority) { '6' }
|
80
|
+
|
81
|
+
it 'sets the priority' do
|
82
|
+
expect(get('/six').body).to eq priority
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
context 'when the app returns a priority' do
|
87
|
+
it 'does not know the first time the controller is called' do
|
88
|
+
expect(get('/six').body).to be_empty
|
89
|
+
expect(post('/nine').body).to be_empty
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'caches the repsonses for the second time' do
|
93
|
+
expect(get('/six').body).to be_empty
|
94
|
+
expect(post('/nine').body).to be_empty
|
95
|
+
|
96
|
+
expect(get('/six').body).to eq '6'
|
97
|
+
expect(post('/nine').body).to eq '9'
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-berater
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Pepper
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-11-
|
11
|
+
date: 2021-11-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: berater
|
@@ -108,6 +108,20 @@ dependencies:
|
|
108
108
|
- - ">="
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rspec-rails
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
111
125
|
- !ruby/object:Gem::Dependency
|
112
126
|
name: simplecov
|
113
127
|
requirement: !ruby/object:Gem::Requirement
|
@@ -131,10 +145,13 @@ files:
|
|
131
145
|
- lib/rack-berater.rb
|
132
146
|
- lib/rack/berater.rb
|
133
147
|
- lib/rack/berater/prioritizer.rb
|
148
|
+
- lib/rack/berater/rails_prioritizer.rb
|
134
149
|
- lib/rack/berater/railtie.rb
|
150
|
+
- lib/rack/berater/rspec.rb
|
135
151
|
- lib/rack/berater/version.rb
|
136
152
|
- spec/limiter_spec.rb
|
137
153
|
- spec/prioritizer_spec.rb
|
154
|
+
- spec/rails_prioritizer_spec.rb
|
138
155
|
- spec/railtie_spec.rb
|
139
156
|
- spec/rescuer_spec.rb
|
140
157
|
homepage: https://github.com/dpep/rack-berater
|
@@ -161,6 +178,7 @@ signing_key:
|
|
161
178
|
specification_version: 4
|
162
179
|
summary: Rack::Berater
|
163
180
|
test_files:
|
181
|
+
- spec/rails_prioritizer_spec.rb
|
164
182
|
- spec/railtie_spec.rb
|
165
183
|
- spec/rescuer_spec.rb
|
166
184
|
- spec/prioritizer_spec.rb
|