-
-
Notifications
You must be signed in to change notification settings - Fork 440
Expand file tree
/
Copy pathfailover.rb
More file actions
97 lines (81 loc) 路 2.55 KB
/
Copy pathfailover.rb
File metadata and controls
97 lines (81 loc) 路 2.55 KB
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
89
90
91
92
93
94
95
96
97
module Flipper
module Adapters
class Failover
include ::Flipper::Adapter
# Public: Build a new failover instance.
#
# primary - The primary flipper adapter.
# secondary - The secondary flipper adapter which services reads when
# the primary adapter is unavailable.
# options - Hash of options:
# :dual_write - Boolean, whether to update secondary when
# primary is updated
# :errors - Array of exception types for which to failover
attr_reader :primary, :secondary
def initialize(primary, secondary, options = {})
@primary = primary
@secondary = secondary
@dual_write = options.fetch(:dual_write, false)
@errors = options.fetch(:errors, [ StandardError ])
end
def adapter_stack
"#{name}(primary: #{@primary.adapter_stack}, secondary: #{@secondary.adapter_stack})"
end
def features
@primary.features
rescue *@errors
@secondary.features
end
def get(feature)
@primary.get(feature)
rescue *@errors
@secondary.get(feature)
end
def get_multi(features)
@primary.get_multi(features)
rescue *@errors
@secondary.get_multi(features)
end
def get_all(**kwargs)
@primary.get_all(**kwargs)
rescue *@errors
@secondary.get_all(**kwargs)
end
def add(feature)
@primary.add(feature).tap do
@secondary.add(feature) if @dual_write
end
end
def remove(feature)
@primary.remove(feature).tap do
@secondary.remove(feature) if @dual_write
end
end
def clear(feature)
@primary.clear(feature).tap do
@secondary.clear(feature) if @dual_write
end
end
def enable(feature, gate, thing)
@primary.enable(feature, gate, thing).tap do
@secondary.enable(feature, gate, thing) if @dual_write
end
end
def disable(feature, gate, thing)
@primary.disable(feature, gate, thing).tap do
@secondary.disable(feature, gate, thing) if @dual_write
end
end
def read_integer(key)
@primary.read_integer(key)
rescue *@errors
@secondary.read_integer(key)
end
def set_integer_if_greater(key, value)
accepted = @primary.set_integer_if_greater(key, value)
@secondary.set_integer_if_greater(key, value) if accepted && @dual_write
accepted
end
end
end
end