graphql-armory 0.1.0.pre13 → 0.1.0.pre15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +25 -0
- data/README.md +5 -5
- data/docs/authorization.md +4 -0
- data/docs/encrypted-ids.md +24 -2
- data/docs/persisted-queries.md +4 -0
- data/docs/rate-limiting.md +23 -0
- data/docs/response-caching.md +590 -0
- data/docs/subscriptions.md +261 -0
- data/docs/telemetry.md +39 -1
- data/docs/versioning.md +5 -0
- data/lib/graphql/armory/authorization/authorize.rb +3 -0
- data/lib/graphql/armory/authorization/scope.rb +2 -0
- data/lib/graphql/armory/caching/audit/command.rb +51 -0
- data/lib/graphql/armory/caching/audit/context_class.rb +117 -0
- data/lib/graphql/armory/caching/audit/declarations.rb +166 -0
- data/lib/graphql/armory/caching/audit/own_checks.rb +150 -0
- data/lib/graphql/armory/caching/audit/public_rules.rb +158 -0
- data/lib/graphql/armory/caching/audit/report.rb +113 -0
- data/lib/graphql/armory/caching/audit.rb +136 -0
- data/lib/graphql/armory/caching/base.rb +118 -0
- data/lib/graphql/armory/caching/canonical.rb +138 -0
- data/lib/graphql/armory/caching/charge.rb +86 -0
- data/lib/graphql/armory/caching/codec.rb +108 -0
- data/lib/graphql/armory/caching/context_methods.rb +88 -0
- data/lib/graphql/armory/caching/controls/app_schema.rb +70 -0
- data/lib/graphql/armory/caching/controls/context.rb +30 -0
- data/lib/graphql/armory/caching/controls/operations.rb +37 -0
- data/lib/graphql/armory/caching/controls/schema.rb +123 -0
- data/lib/graphql/armory/caching/controls/store.rb +16 -0
- data/lib/graphql/armory/caching/controls/walk_schema.rb +197 -0
- data/lib/graphql/armory/caching/controls.rb +19 -0
- data/lib/graphql/armory/caching/digester.rb +48 -0
- data/lib/graphql/armory/caching/errors.rb +16 -0
- data/lib/graphql/armory/caching/gate/admission.rb +67 -0
- data/lib/graphql/armory/caching/gate/signals.rb +65 -0
- data/lib/graphql/armory/caching/gate/store_check.rb +60 -0
- data/lib/graphql/armory/caching/gate/substitute.rb +61 -0
- data/lib/graphql/armory/caching/gate.rb +384 -0
- data/lib/graphql/armory/caching/integration/guard.rb +55 -0
- data/lib/graphql/armory/caching/integration/install.rb +262 -0
- data/lib/graphql/armory/caching/integration/query.rb +42 -0
- data/lib/graphql/armory/caching/integration.rb +141 -0
- data/lib/graphql/armory/caching/keyer.rb +208 -0
- data/lib/graphql/armory/caching/memory_store.rb +164 -0
- data/lib/graphql/armory/caching/nulls.rb +121 -0
- data/lib/graphql/armory/caching/purge.rb +98 -0
- data/lib/graphql/armory/caching/record_keys.rb +109 -0
- data/lib/graphql/armory/caching/recorder/context.rb +178 -0
- data/lib/graphql/armory/caching/recorder/log.rb +142 -0
- data/lib/graphql/armory/caching/recorder/null.rb +24 -0
- data/lib/graphql/armory/caching/recorder/shared_writes.rb +58 -0
- data/lib/graphql/armory/caching/recorder/substitute.rb +119 -0
- data/lib/graphql/armory/caching/recorder.rb +34 -0
- data/lib/graphql/armory/caching/redis_store/scripts.rb +159 -0
- data/lib/graphql/armory/caching/redis_store.rb +162 -0
- data/lib/graphql/armory/caching/rspec.rb +25 -0
- data/lib/graphql/armory/caching/schema_identity.rb +102 -0
- data/lib/graphql/armory/caching/store/null.rb +31 -0
- data/lib/graphql/armory/caching/store/substitute.rb +144 -0
- data/lib/graphql/armory/caching/store.rb +34 -0
- data/lib/graphql/armory/caching/tags.rb +39 -0
- data/lib/graphql/armory/caching/values/decision.rb +45 -0
- data/lib/graphql/armory/caching/values/declaration.rb +114 -0
- data/lib/graphql/armory/caching/values/entry.rb +108 -0
- data/lib/graphql/armory/caching/values/key.rb +39 -0
- data/lib/graphql/armory/caching/values/lookup.rb +35 -0
- data/lib/graphql/armory/caching/values/policy.rb +44 -0
- data/lib/graphql/armory/caching/viewer.rb +112 -0
- data/lib/graphql/armory/caching/walk/directives.rb +45 -0
- data/lib/graphql/armory/caching/walk/relay.rb +54 -0
- data/lib/graphql/armory/caching/walk/run.rb +130 -0
- data/lib/graphql/armory/caching/walk/tally.rb +38 -0
- data/lib/graphql/armory/caching/walk.rb +46 -0
- data/lib/graphql/armory/caching.rb +69 -0
- data/lib/graphql/armory/encoder/legacy.rb +14 -0
- data/lib/graphql/armory/encoder/null.rb +14 -1
- data/lib/graphql/armory/encoder/substitute.rb +54 -2
- data/lib/graphql/armory/encoder/versioned.rb +36 -0
- data/lib/graphql/armory/encoder.rb +80 -1
- data/lib/graphql/armory/persisted_queries.rb +8 -1
- data/lib/graphql/armory/rate_limits/cached_charge.rb +43 -0
- data/lib/graphql/armory/rate_limits/concurrency.rb +12 -5
- data/lib/graphql/armory/rate_limits/invalid_queries.rb +3 -2
- data/lib/graphql/armory/rate_limits/points.rb +21 -9
- data/lib/graphql/armory/rate_limits/time.rb +15 -8
- data/lib/graphql/armory/rate_limits.rb +9 -0
- data/lib/graphql/armory/subscriptions/ably/key.rb +49 -0
- data/lib/graphql/armory/subscriptions/ably/publisher.rb +132 -0
- data/lib/graphql/armory/subscriptions/ably/token_request.rb +42 -0
- data/lib/graphql/armory/subscriptions/ably/webhook.rb +96 -0
- data/lib/graphql/armory/subscriptions/ably.rb +126 -0
- data/lib/graphql/armory/subscriptions/audit.rb +92 -0
- data/lib/graphql/armory/subscriptions/controls/dependencies.rb +37 -0
- data/lib/graphql/armory/subscriptions/controls/event_log.rb +30 -0
- data/lib/graphql/armory/subscriptions/controls/id.rb +26 -0
- data/lib/graphql/armory/subscriptions/controls/record.rb +34 -0
- data/lib/graphql/armory/subscriptions/controls/schema.rb +187 -0
- data/lib/graphql/armory/subscriptions/controls/viewer.rb +99 -0
- data/lib/graphql/armory/subscriptions/controls.rb +19 -0
- data/lib/graphql/armory/subscriptions/core/channels.rb +93 -0
- data/lib/graphql/armory/subscriptions/core/clock.rb +40 -0
- data/lib/graphql/armory/subscriptions/core/controls.rb +29 -0
- data/lib/graphql/armory/subscriptions/core/delivery.rb +250 -0
- data/lib/graphql/armory/subscriptions/core/settings.rb +182 -0
- data/lib/graphql/armory/subscriptions/core/trigger.rb +63 -0
- data/lib/graphql/armory/subscriptions/core/viewer.rb +153 -0
- data/lib/graphql/armory/subscriptions/core/write.rb +172 -0
- data/lib/graphql/armory/subscriptions/core.rb +200 -0
- data/lib/graphql/armory/subscriptions/errors.rb +30 -0
- data/lib/graphql/armory/subscriptions/http/null.rb +17 -0
- data/lib/graphql/armory/subscriptions/http/substitute.rb +56 -0
- data/lib/graphql/armory/subscriptions/http.rb +93 -0
- data/lib/graphql/armory/subscriptions/memory_store.rb +115 -0
- data/lib/graphql/armory/subscriptions/pusher/signature.rb +45 -0
- data/lib/graphql/armory/subscriptions/pusher.rb +192 -0
- data/lib/graphql/armory/subscriptions/redis_store/scripts.rb +115 -0
- data/lib/graphql/armory/subscriptions/redis_store.rb +157 -0
- data/lib/graphql/armory/subscriptions/store/null.rb +23 -0
- data/lib/graphql/armory/subscriptions/store/substitute.rb +155 -0
- data/lib/graphql/armory/subscriptions/store.rb +38 -0
- data/lib/graphql/armory/subscriptions/topic.rb +253 -0
- data/lib/graphql/armory/subscriptions/transport/null.rb +28 -0
- data/lib/graphql/armory/subscriptions/transport/substitute.rb +150 -0
- data/lib/graphql/armory/subscriptions/transport.rb +41 -0
- data/lib/graphql/armory/subscriptions/values/capabilities.rb +50 -0
- data/lib/graphql/armory/subscriptions/values/message.rb +31 -0
- data/lib/graphql/armory/subscriptions/values/outcome.rb +42 -0
- data/lib/graphql/armory/subscriptions/values/record.rb +176 -0
- data/lib/graphql/armory/subscriptions/webhook_app.rb +31 -0
- data/lib/graphql/armory/subscriptions.rb +33 -0
- data/lib/graphql/armory/telemetry.rb +8 -4
- data/lib/graphql/armory/version.rb +1 -1
- data/lib/graphql/armory/versioning/audit.rb +1 -9
- data/lib/graphql/armory/versioning/integration.rb +60 -2
- data/lib/graphql/armory.rb +2 -0
- data/lib/tasks/graphql_armory.rake +9 -0
- metadata +110 -1
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "graphql/armory/caching/recorder/shared_writes"
|
|
4
|
+
|
|
5
|
+
module GraphQL
|
|
6
|
+
module Armory
|
|
7
|
+
module Caching
|
|
8
|
+
module Recorder
|
|
9
|
+
# The real recorder of one query (the interface is in `recorder.rb`). `Recorder::Context` feeds it; the gate asks
|
|
10
|
+
# it `uncovered(covered)` and `forbidden_writes(covered)` after the run.
|
|
11
|
+
#
|
|
12
|
+
# - A read is an **input** unless this query wrote that key into the provided values earlier (`write`). A read
|
|
13
|
+
# answered by this query's own `scoped_set!` never reaches the Log as a read: the context checks that the
|
|
14
|
+
# value came from the scoped context (see `Context`), since a scoped value covers only its own subtree.
|
|
15
|
+
# - A write (`write` for the provided values, `write_scoped` for the scoped context) is **forbidden** when its
|
|
16
|
+
# key is covered, or when this query read that key as an input before writing it.
|
|
17
|
+
# - Once shared (`share(hash)`), writes to the provided values and exposures of the Hash are published to the
|
|
18
|
+
# Hash's `SharedWrites` board. A sibling's write is forbidden for this query when its key is covered here,
|
|
19
|
+
# when this query read that key through the provided values, or when the sibling had read it before writing.
|
|
20
|
+
# A sibling that exposed the Hash (`read_all`) makes this query report `:all`.
|
|
21
|
+
# - `neutral { }` answers the block's value and stops recording reads (and `read_all`) for the block, in the
|
|
22
|
+
# current Fiber only: a read made by another Fiber meanwhile (dataloader sources) is still recorded. A write
|
|
23
|
+
# inside it (a limiter's record, `context[:rate_limit_points]`) does not make the key "written by this query":
|
|
24
|
+
# a later read of it outside the block is an input, since the value depends on what the neutral reader read.
|
|
25
|
+
# It is still checked as a forbidden write (covered, or read before) and still published to siblings: a
|
|
26
|
+
# neutral reader that overwrites a covered key changes what every later read sees.
|
|
27
|
+
#
|
|
28
|
+
# `new` is useful at once: it records, with nothing shared. Thread-safe.
|
|
29
|
+
class Log
|
|
30
|
+
def initialize
|
|
31
|
+
@inputs = {} # key => true, in the order first read
|
|
32
|
+
@read = {} # every key read through the provided values, input or not
|
|
33
|
+
@written = {} # keys this query wrote into the provided values
|
|
34
|
+
@scoped = {} # keys this query wrote into its scoped context
|
|
35
|
+
@writes = {} # key => true when written after an input read of it
|
|
36
|
+
@all = false
|
|
37
|
+
@neutral = {}.compare_by_identity # Fiber => depth
|
|
38
|
+
@board = nil
|
|
39
|
+
@lock = Mutex.new
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Joins the board of the provided Hash `provided`, publishing what was written or exposed before.
|
|
43
|
+
def share(provided)
|
|
44
|
+
board = SharedWrites.for(provided)
|
|
45
|
+
writes, all = @lock.synchronize do
|
|
46
|
+
return self if @board.equal?(board)
|
|
47
|
+
|
|
48
|
+
@board = board
|
|
49
|
+
[@writes.select { |key, _| @written.key?(key) }, @all]
|
|
50
|
+
end
|
|
51
|
+
writes.each { |key, after_read| board.write(self, key, after_read) }
|
|
52
|
+
board.expose(self) if all
|
|
53
|
+
self
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def read(key)
|
|
57
|
+
@lock.synchronize do
|
|
58
|
+
next if @neutral.key?(Fiber.current)
|
|
59
|
+
|
|
60
|
+
@read[key] = true
|
|
61
|
+
@inputs[key] = true unless @written.key?(key)
|
|
62
|
+
end
|
|
63
|
+
nil
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def read_all
|
|
67
|
+
board = @lock.synchronize do
|
|
68
|
+
next if @neutral.key?(Fiber.current)
|
|
69
|
+
|
|
70
|
+
@all = true
|
|
71
|
+
@board
|
|
72
|
+
end
|
|
73
|
+
board&.expose(self)
|
|
74
|
+
nil
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# A write into the provided values (`[]=`, `delete` of a provided key).
|
|
78
|
+
def write(key)
|
|
79
|
+
after_read, board = @lock.synchronize do
|
|
80
|
+
@written[key] = true unless @neutral.key?(Fiber.current)
|
|
81
|
+
[note_write(key), @board]
|
|
82
|
+
end
|
|
83
|
+
board&.write(self, key, after_read)
|
|
84
|
+
nil
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# A write into this query's scoped context (`scoped_merge!`, `scoped_set!`, `context.scoped`): siblings never
|
|
88
|
+
# see it, and a later read sees it only inside the subtree it was set for.
|
|
89
|
+
def write_scoped(key)
|
|
90
|
+
@lock.synchronize do
|
|
91
|
+
@scoped[key] = true unless @neutral.key?(Fiber.current)
|
|
92
|
+
note_write(key)
|
|
93
|
+
end
|
|
94
|
+
nil
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def scoped_written?(key)
|
|
98
|
+
@lock.synchronize { @scoped.key?(key) }
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def neutral
|
|
102
|
+
fiber = Fiber.current
|
|
103
|
+
@lock.synchronize { @neutral[fiber] = @neutral.fetch(fiber, 0) + 1 }
|
|
104
|
+
begin
|
|
105
|
+
yield
|
|
106
|
+
ensure
|
|
107
|
+
@lock.synchronize do
|
|
108
|
+
depth = @neutral.fetch(fiber) - 1
|
|
109
|
+
depth.zero? ? @neutral.delete(fiber) : @neutral[fiber] = depth
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def uncovered(covered)
|
|
115
|
+
keys, all, board = @lock.synchronize { [@inputs.keys, @all, @board] }
|
|
116
|
+
keys = keys.reject { |key| covered.include?(key) }
|
|
117
|
+
all || board&.exposed_by_others?(self) ? [ALL, *keys] : keys
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def forbidden_writes(covered)
|
|
121
|
+
own, read, board = @lock.synchronize { [@writes.dup, @read.dup, @board] }
|
|
122
|
+
forbidden = own.select { |key, after_read| after_read || covered.include?(key) }.keys
|
|
123
|
+
(board&.writes_by_others(self) || []).each do |key, after_read|
|
|
124
|
+
forbidden << key if after_read || covered.include?(key) || read.key?(key)
|
|
125
|
+
end
|
|
126
|
+
forbidden.uniq
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def inspect = "#<#{self.class.name}>"
|
|
130
|
+
|
|
131
|
+
private
|
|
132
|
+
|
|
133
|
+
# Called under the lock. Answers whether this write follows an input read of its key.
|
|
134
|
+
def note_write(key)
|
|
135
|
+
after_read = @inputs.key?(key)
|
|
136
|
+
@writes[key] = @writes.fetch(key, false) || after_read
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Caching
|
|
6
|
+
module Recorder
|
|
7
|
+
# The recorder of a query whose context records nothing: it reports the whole context uncovered, so the gate
|
|
8
|
+
# never stores an answer it could not check.
|
|
9
|
+
class Null
|
|
10
|
+
def read(_key) = nil
|
|
11
|
+
def read_all = nil
|
|
12
|
+
def write(_key) = nil
|
|
13
|
+
|
|
14
|
+
def neutral
|
|
15
|
+
yield
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def uncovered(_covered) = [ALL]
|
|
19
|
+
def forbidden_writes(_covered) = []
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Caching
|
|
6
|
+
module Recorder
|
|
7
|
+
# What the queries built from one provided Hash did to it. graphql-ruby keeps the Hash given as `context:`
|
|
8
|
+
# (`Query::Context#initialize`), so queries of one multiplex given the same Hash object see each other's
|
|
9
|
+
# `[]=` and `delete`. Each query's `Log` joins the board of its Hash and reports its siblings' writes and
|
|
10
|
+
# exposures through its own `forbidden_writes` and `uncovered`, so the gate asks each query only about itself.
|
|
11
|
+
#
|
|
12
|
+
# Boards are found by the Hash's identity, never by equality: two equal Hashes are two requests. The registry
|
|
13
|
+
# holds the Hash weakly, and a board lives as long as a Log that joined it.
|
|
14
|
+
class SharedWrites
|
|
15
|
+
REGISTRY = ObjectSpace::WeakMap.new
|
|
16
|
+
LOCK = Mutex.new
|
|
17
|
+
private_constant :REGISTRY, :LOCK
|
|
18
|
+
|
|
19
|
+
# The board of `provided`, created on first use.
|
|
20
|
+
def self.for(provided)
|
|
21
|
+
LOCK.synchronize { REGISTRY[provided] ||= new }
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def initialize
|
|
25
|
+
@writes = {}.compare_by_identity # log => { key => written after that log read it }
|
|
26
|
+
@exposers = {}.compare_by_identity
|
|
27
|
+
@lock = Mutex.new
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def write(log, key, after_read)
|
|
31
|
+
@lock.synchronize do
|
|
32
|
+
keys = (@writes[log] ||= {})
|
|
33
|
+
keys[key] = keys.fetch(key, false) || after_read
|
|
34
|
+
end
|
|
35
|
+
nil
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# `log` handed out the Hash itself (`namespace(nil)`, `to_h`): anything may have changed it.
|
|
39
|
+
def expose(log)
|
|
40
|
+
@lock.synchronize { @exposers[log] = true }
|
|
41
|
+
nil
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# `[[key, after_read], ...]` written by every log but `log`.
|
|
45
|
+
def writes_by_others(log)
|
|
46
|
+
@lock.synchronize do
|
|
47
|
+
@writes.reject { |writer, _| writer.equal?(log) }.flat_map { |_, keys| keys.to_a }
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def exposed_by_others?(log)
|
|
52
|
+
@lock.synchronize { @exposers.each_key.any? { |exposer| !exposer.equal?(log) } }
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Caching
|
|
6
|
+
module Recorder
|
|
7
|
+
# A recorder that keeps what it saw, in order, for specs: `reads`, `writes`, `all_read?`, and `neutral_depth`
|
|
8
|
+
# while inside `neutral`. It answers `uncovered` and `forbidden_writes` with the same rules as the real one.
|
|
9
|
+
class Substitute
|
|
10
|
+
attr_reader :reads, :writes, :neutral_depth
|
|
11
|
+
|
|
12
|
+
def initialize
|
|
13
|
+
@reads = []
|
|
14
|
+
@writes = []
|
|
15
|
+
@all_read = false
|
|
16
|
+
@neutral_depth = 0
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def read(key)
|
|
20
|
+
@reads << key unless neutral?
|
|
21
|
+
nil
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def read_all
|
|
25
|
+
@all_read = true unless neutral?
|
|
26
|
+
nil
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def write(key)
|
|
30
|
+
@writes << [key, @reads.size] unless neutral?
|
|
31
|
+
nil
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def neutral
|
|
35
|
+
@neutral_depth += 1
|
|
36
|
+
yield
|
|
37
|
+
ensure
|
|
38
|
+
@neutral_depth -= 1
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def neutral?
|
|
42
|
+
@neutral_depth.positive?
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def all_read?
|
|
46
|
+
@all_read
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def read?(key)
|
|
50
|
+
@reads.include?(key)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def uncovered(covered)
|
|
54
|
+
keys = @reads.each_with_index.reject { |key, index| covered.include?(key) || written_before?(key, index) }.map(&:first).uniq
|
|
55
|
+
@all_read ? [ALL, *keys] : keys
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def forbidden_writes(covered)
|
|
59
|
+
@writes.select { |key, reads_before| covered.include?(key) || @reads.first(reads_before).include?(key) }.map(&:first).uniq
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
private
|
|
63
|
+
|
|
64
|
+
def written_before?(key, read_index)
|
|
65
|
+
@writes.any? { |written, reads_before| written == key && reads_before <= read_index }
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
public
|
|
69
|
+
|
|
70
|
+
# A context class that records through a `Substitute`, for specs of the modules that read the context
|
|
71
|
+
# neutrally (Versioning, Telemetry, the limiters):
|
|
72
|
+
#
|
|
73
|
+
# schema.context_class(GraphQL::Armory::Caching::Recorder::Substitute::Context)
|
|
74
|
+
# recorder = GraphQL::Armory::Caching::RecordKeys.recorder(query.context)
|
|
75
|
+
#
|
|
76
|
+
# It records `[]`, `fetch`, `dig` and `key?` reads and `[]=` and `delete` writes; the real `Recorder::Context`
|
|
77
|
+
# covers every read and write path.
|
|
78
|
+
class Context < GraphQL::Query::Context
|
|
79
|
+
def initialize(...)
|
|
80
|
+
@armory_recorder = Substitute.new
|
|
81
|
+
super(...)
|
|
82
|
+
RecordKeys.recorder!(self, @armory_recorder)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def [](key)
|
|
86
|
+
@armory_recorder.read(key)
|
|
87
|
+
super
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def fetch(key, *rest, &block)
|
|
91
|
+
@armory_recorder.read(key)
|
|
92
|
+
super
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def dig(key, *rest)
|
|
96
|
+
@armory_recorder.read(key)
|
|
97
|
+
super
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def key?(key)
|
|
101
|
+
@armory_recorder.read(key)
|
|
102
|
+
super
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def []=(key, value)
|
|
106
|
+
@armory_recorder.write(key)
|
|
107
|
+
super
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def delete(key)
|
|
111
|
+
@armory_recorder.write(key)
|
|
112
|
+
super
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Caching
|
|
6
|
+
# Records which context keys a query read and wrote, from its context's construction on, so the gate refuses to
|
|
7
|
+
# store an answer that depended on a key the cache key does not hold. A tripwire, not a proof: it sees only
|
|
8
|
+
# reads through the context's public methods (docs/response-caching.md "Inputs you must declare").
|
|
9
|
+
#
|
|
10
|
+
# Each query's recorder lives in `context.namespace(:armory_cache)[:recorder]` (`RecordKeys.recorder`). Every
|
|
11
|
+
# recorder answers:
|
|
12
|
+
#
|
|
13
|
+
# recorder.read(key) # a key read through [], fetch, dig or key?, present or absent
|
|
14
|
+
# recorder.read_all # to_h, namespace(nil), scoped_context: the whole Hash was read
|
|
15
|
+
# recorder.write(key) # []=, delete, scoped_merge!, scoped_set!
|
|
16
|
+
# recorder.neutral { } # records nothing for the block's duration
|
|
17
|
+
# recorder.uncovered(covered) -> [] # keys read and not covered, :all when the whole Hash was read; a key
|
|
18
|
+
# # this query wrote before reading it is not an input
|
|
19
|
+
# recorder.forbidden_writes(covered) -> [] # keys written that are covered or that this query read earlier
|
|
20
|
+
#
|
|
21
|
+
# `Null` (no recorder: report everything uncovered, so nothing is stored), `Substitute` (records, for specs) and
|
|
22
|
+
# `Substitute::Context` (a context class that records through a Substitute) stand in for the real one (`Log`).
|
|
23
|
+
module Recorder
|
|
24
|
+
ALL = :all
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
require "graphql/armory/caching/recorder/null"
|
|
31
|
+
require "graphql/armory/caching/recorder/substitute"
|
|
32
|
+
require "graphql/armory/caching/recorder/shared_writes"
|
|
33
|
+
require "graphql/armory/caching/recorder/log"
|
|
34
|
+
require "graphql/armory/caching/recorder/context"
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Caching
|
|
6
|
+
class RedisStore
|
|
7
|
+
# One Lua script per operation, so each one is atomic and one round trip. Every script reads the meta hash
|
|
8
|
+
# (KEYS[1]); the entry scripts also read the entry hash (KEYS[2]). Layout:
|
|
9
|
+
#
|
|
10
|
+
# <prefix>meta hash, no TTL: epoch, seq, max_ttl (seconds), cursor, and m:<tag digest> => "<seq>:<ms>"
|
|
11
|
+
# <prefix>e:<key digest> hash expiring with the entry: blob, tags (JSON array of digests), expires_at (ms)
|
|
12
|
+
#
|
|
13
|
+
# The clock is Redis's own (`TIME`, in Integer milliseconds), so processes on different machines agree on it.
|
|
14
|
+
# Scripts call `TIME` and `HSCAN` before writing, which needs Redis 5 or later (scripts replicate their effects).
|
|
15
|
+
module Scripts
|
|
16
|
+
PRELUDE = <<~LUA
|
|
17
|
+
local function now_ms()
|
|
18
|
+
local time = redis.call("TIME")
|
|
19
|
+
return tonumber(time[1]) * 1000 + math.floor(tonumber(time[2]) / 1000)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
local function ms(value)
|
|
23
|
+
return string.format("%.0f", value)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
-- The meta record. A missing one, or one whose epoch or seq cannot be read, is replaced by a fresh one with
|
|
27
|
+
-- the candidate epoch and seq 0: a lost meta record is "everything purged". It also keeps the longest
|
|
28
|
+
-- max_ttl of every store on this prefix, so a store with a shorter one never prunes a mark another
|
|
29
|
+
-- store's runs still need.
|
|
30
|
+
local function meta(key, candidate, max_ttl)
|
|
31
|
+
local state = redis.call("HMGET", key, "epoch", "seq", "max_ttl")
|
|
32
|
+
local epoch, seq, kept = state[1], tonumber(state[2]), tonumber(state[3])
|
|
33
|
+
if not epoch or epoch == "" or not seq or seq < 0 or seq ~= math.floor(seq) then
|
|
34
|
+
redis.call("DEL", key)
|
|
35
|
+
redis.call("HSET", key, "epoch", candidate, "seq", 0, "max_ttl", max_ttl)
|
|
36
|
+
return candidate, 0
|
|
37
|
+
end
|
|
38
|
+
if not kept or kept < max_ttl then redis.call("HSET", key, "max_ttl", max_ttl) end
|
|
39
|
+
return epoch, seq
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
-- A tag's purge mark (the seq of its latest purge), or nil. A mark that does not parse is an error: the
|
|
43
|
+
-- caller runs the query uncached rather than answer an entry without the mark that may reject it.
|
|
44
|
+
local function mark(key, tag)
|
|
45
|
+
local value = redis.call("HGET", key, "m:" .. tag)
|
|
46
|
+
if not value then return nil end
|
|
47
|
+
local seq = tonumber(string.match(value, "^(%d+):%d+$"))
|
|
48
|
+
if not seq then error({ err = "graphql-armory: corrupt purge mark in " .. key }) end
|
|
49
|
+
return seq
|
|
50
|
+
end
|
|
51
|
+
LUA
|
|
52
|
+
|
|
53
|
+
# KEYS: meta, entry. ARGV: candidate epoch, max_ttl.
|
|
54
|
+
# Answers { blob or nil, tags, { tag, mark, ... }, seq, epoch, now }. Anything under the entry key that is not
|
|
55
|
+
# one of its records, or is expired, is a miss.
|
|
56
|
+
LOOKUP = <<~LUA
|
|
57
|
+
#{PRELUDE}
|
|
58
|
+
local now = now_ms()
|
|
59
|
+
local epoch, seq = meta(KEYS[1], ARGV[1], tonumber(ARGV[2]))
|
|
60
|
+
local blob, tags, marks = false, {}, {}
|
|
61
|
+
|
|
62
|
+
if redis.call("TYPE", KEYS[2]).ok == "hash" then
|
|
63
|
+
local entry = redis.call("HMGET", KEYS[2], "blob", "tags", "expires_at")
|
|
64
|
+
local expires_at = tonumber(entry[3])
|
|
65
|
+
if entry[1] and entry[2] and expires_at and now < expires_at then
|
|
66
|
+
local ok, decoded = pcall(cjson.decode, entry[2])
|
|
67
|
+
local valid = ok and type(decoded) == "table"
|
|
68
|
+
if valid then
|
|
69
|
+
for _, tag in ipairs(decoded) do
|
|
70
|
+
if type(tag) ~= "string" then valid = false end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
if valid then
|
|
74
|
+
blob, tags = entry[1], decoded
|
|
75
|
+
for _, tag in ipairs(tags) do
|
|
76
|
+
local purged = mark(KEYS[1], tag)
|
|
77
|
+
if purged then
|
|
78
|
+
table.insert(marks, tag)
|
|
79
|
+
table.insert(marks, purged)
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
return { blob, tags, marks, seq, epoch, now }
|
|
87
|
+
LUA
|
|
88
|
+
|
|
89
|
+
# KEYS: meta, entry. ARGV: candidate epoch, max_ttl, blob, tags (JSON), ttl, seq, epoch, stored_at.
|
|
90
|
+
# Answers 1 when stored, 0 when refused: another epoch, a seq ahead of the store's, a tag purged after the
|
|
91
|
+
# lookup, or an entry already expired.
|
|
92
|
+
WRITE = <<~LUA
|
|
93
|
+
#{PRELUDE}
|
|
94
|
+
local now = now_ms()
|
|
95
|
+
local epoch, seq = meta(KEYS[1], ARGV[1], tonumber(ARGV[2]))
|
|
96
|
+
local ttl, entry_seq, stored_at = tonumber(ARGV[5]), tonumber(ARGV[6]), tonumber(ARGV[8])
|
|
97
|
+
|
|
98
|
+
if ARGV[7] ~= epoch or entry_seq > seq then return 0 end
|
|
99
|
+
for _, tag in ipairs(cjson.decode(ARGV[4])) do
|
|
100
|
+
local purged = mark(KEYS[1], tag)
|
|
101
|
+
if purged and purged > entry_seq then return 0 end
|
|
102
|
+
end
|
|
103
|
+
local expires_at = stored_at + ttl * 1000
|
|
104
|
+
if now >= expires_at then return 0 end
|
|
105
|
+
|
|
106
|
+
redis.call("DEL", KEYS[2])
|
|
107
|
+
redis.call("HSET", KEYS[2], "blob", ARGV[3], "tags", ARGV[4], "expires_at", ms(expires_at))
|
|
108
|
+
redis.call("PEXPIRE", KEYS[2], ms(expires_at - now))
|
|
109
|
+
return 1
|
|
110
|
+
LUA
|
|
111
|
+
|
|
112
|
+
# KEYS: meta. ARGV: candidate epoch, max_ttl, tag digests...
|
|
113
|
+
# Increments seq, marks each tag with it, then prunes up to 100 marks older than the prefix's max_ttl,
|
|
114
|
+
# walking the hash with a cursor kept in it. Answers the new seq.
|
|
115
|
+
#
|
|
116
|
+
# Pruning is safe: an entry a mark rejects was looked up before that purge, so it was stamped no later than
|
|
117
|
+
# the purge and, its TTL being at most max_ttl, has expired once the mark is older than max_ttl. A write
|
|
118
|
+
# that late is refused as expired.
|
|
119
|
+
PURGE = <<~LUA
|
|
120
|
+
#{PRELUDE}
|
|
121
|
+
local now = now_ms()
|
|
122
|
+
meta(KEYS[1], ARGV[1], tonumber(ARGV[2]))
|
|
123
|
+
local seq = redis.call("HINCRBY", KEYS[1], "seq", 1)
|
|
124
|
+
local stamp = ms(seq) .. ":" .. ms(now)
|
|
125
|
+
for index = 3, #ARGV do
|
|
126
|
+
redis.call("HSET", KEYS[1], "m:" .. ARGV[index], stamp)
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
local keep = tonumber(redis.call("HGET", KEYS[1], "max_ttl")) * 1000
|
|
130
|
+
local scan = redis.call("HSCAN", KEYS[1], redis.call("HGET", KEYS[1], "cursor") or "0", "MATCH", "m:*", "COUNT", 100)
|
|
131
|
+
local found, pruned = scan[2], 0
|
|
132
|
+
for index = 1, #found, 2 do
|
|
133
|
+
if pruned >= 100 then break end
|
|
134
|
+
local at = tonumber(string.match(found[index + 1], "^%d+:(%d+)$"))
|
|
135
|
+
if at and now - at > keep then
|
|
136
|
+
redis.call("HDEL", KEYS[1], found[index])
|
|
137
|
+
pruned = pruned + 1
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
redis.call("HSET", KEYS[1], "cursor", scan[1])
|
|
141
|
+
|
|
142
|
+
return seq
|
|
143
|
+
LUA
|
|
144
|
+
|
|
145
|
+
# KEYS: meta. ARGV: the new epoch, max_ttl.
|
|
146
|
+
# Replaces the epoch and drops every mark, keeping seq and the prefix's max_ttl. Answers the new epoch.
|
|
147
|
+
PURGE_ALL = <<~LUA
|
|
148
|
+
#{PRELUDE}
|
|
149
|
+
local _, seq = meta(KEYS[1], ARGV[1], tonumber(ARGV[2]))
|
|
150
|
+
local kept = redis.call("HGET", KEYS[1], "max_ttl")
|
|
151
|
+
redis.call("DEL", KEYS[1])
|
|
152
|
+
redis.call("HSET", KEYS[1], "epoch", ARGV[1], "seq", ms(seq), "max_ttl", kept)
|
|
153
|
+
return ARGV[1]
|
|
154
|
+
LUA
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
end
|