-
-
Notifications
You must be signed in to change notification settings - Fork 411
/
memoizer.rb
88 lines (77 loc) · 2.62 KB
/
memoizer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
module Flipper
module Middleware
class Memoizer
# Public: Initializes an instance of the Memoizer middleware. Flipper must
# be configured with a default instance or the flipper instance must be
# setup in the env of the request. You can do this by using the
# Flipper::Middleware::SetupEnv middleware.
#
# app - The app this middleware is included in.
# opts - The Hash of options.
# :preload - Boolean to preload all features or Array of Symbol feature names to preload.
#
# Examples
#
# use Flipper::Middleware::Memoizer
#
# # using with preload_all features
# use Flipper::Middleware::Memoizer, preload_all: true
#
# # using with preload specific features
# use Flipper::Middleware::Memoizer, preload: [:stats, :search, :some_feature]
#
def initialize(app, opts = {})
if opts.is_a?(Flipper::DSL) || opts.is_a?(Proc)
raise 'Flipper::Middleware::Memoizer no longer initializes with a flipper instance or block. Read more at: https://git.io/vSo31.'
end
if opts[:preload_all]
warn "Flipper::Middleware::Memoizer: `preload_all` is deprecated, use `preload: true`"
opts[:preload] = true
end
@app = app
@opts = opts
@env_key = opts.fetch(:env_key, 'flipper')
end
def call(env)
request = Rack::Request.new(env)
if memoize?(request)
memoized_call(env)
else
@app.call(env)
end
end
private
def memoize?(request)
if @opts[:if]
@opts[:if].call(request)
elsif @opts[:unless]
!@opts[:unless].call(request)
else
true
end
end
def memoized_call(env)
reset_on_body_close = false
flipper = env.fetch(@env_key) { Flipper }
# Already memoizing. This instance does not need to do anything.
if flipper.memoizing?
warn "Flipper::Middleware::Memoizer appears to be running twice. Read how to resolve this at https://github.com/jnunemaker/flipper/pull/523"
return @app.call(env)
end
flipper.memoize = true
case @opts[:preload]
when true then flipper.preload_all
when Array then flipper.preload(@opts[:preload])
end
response = @app.call(env)
response[2] = Rack::BodyProxy.new(response[2]) do
flipper.memoize = false
end
reset_on_body_close = true
response
ensure
flipper.memoize = false if flipper && !reset_on_body_close
end
end
end
end