graphql-armory 0.1.0.pre13 → 0.1.0.pre14
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 +17 -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 +4 -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/integration.rb +2 -1
- data/lib/graphql/armory.rb +2 -0
- data/lib/tasks/graphql_armory.rake +9 -0
- metadata +110 -1
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
require "json"
|
|
5
|
+
require "securerandom"
|
|
6
|
+
require "graphql/armory/caching/redis_store/scripts"
|
|
7
|
+
|
|
8
|
+
module GraphQL
|
|
9
|
+
module Armory
|
|
10
|
+
module Caching
|
|
11
|
+
# Answers kept in Redis, shared by every process of the app: the store for production.
|
|
12
|
+
#
|
|
13
|
+
# store = GraphQL::Armory::Caching::RedisStore.build(redis: Redis.new(url: ENV.fetch("CACHE_REDIS_URL")))
|
|
14
|
+
#
|
|
15
|
+
# Keeps the store contract (`Store`): each operation is one Lua script (`Scripts`), so it is atomic and one round
|
|
16
|
+
# trip, and the clock is Redis's own `TIME`. The epoch, the purge sequence and every purge mark live in one hash
|
|
17
|
+
# per prefix, `<prefix>meta`, without a TTL: under a `volatile-*` eviction policy it is never evicted, and under
|
|
18
|
+
# an `allkeys-*` policy it can only be evicted whole, which every store reads as "everything purged".
|
|
19
|
+
#
|
|
20
|
+
# - `max_ttl:` (seconds, default 3600) is the longest TTL it stores; purge marks are pruned once older than the
|
|
21
|
+
# longest `max_ttl` of the stores sharing the prefix. Set it to the `max_ttl:` given to `use`.
|
|
22
|
+
# - `purge_replicas:` runs `WAIT n` after each purge and raises `PurgeError` when fewer replicas acknowledged it
|
|
23
|
+
# within `purge_timeout:` seconds. Without it, a purge acknowledged by a primary that fails before replicating
|
|
24
|
+
# it can be lost.
|
|
25
|
+
# - Redis Cluster: every script touches the meta hash, so the prefix needs a hash tag that puts every key in one
|
|
26
|
+
# slot, as in `prefix: "graphql-armory:{cache}:"`.
|
|
27
|
+
#
|
|
28
|
+
# Redis errors reach the caller unchanged: the gate runs the query uncached on a lookup or write error, and turns
|
|
29
|
+
# a purge error into `PurgeError`. `redis:` is a redis-rb client (>= 4.8), or anything answering `eval`,
|
|
30
|
+
# `evalsha` and `call` the same way, on Redis 5 or later. This gem does not depend on the redis gem: the app adds
|
|
31
|
+
# it. Keep the cache in a Redis of its own: PersistedQueries and RateLimits keys need `noeviction`.
|
|
32
|
+
class RedisStore
|
|
33
|
+
DEFAULT_PREFIX = "graphql-armory:cache:"
|
|
34
|
+
DEFAULT_MAX_TTL = 3600
|
|
35
|
+
DEFAULT_PURGE_TIMEOUT = 1
|
|
36
|
+
|
|
37
|
+
module Client
|
|
38
|
+
# The Redis of a store nobody wired: it holds nothing, stores nothing, and a purge through it raises.
|
|
39
|
+
class Null; end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Validates the settings and wires the real Redis client.
|
|
43
|
+
def self.build(redis:, prefix: DEFAULT_PREFIX, purge_replicas: nil, purge_timeout: DEFAULT_PURGE_TIMEOUT, max_ttl: DEFAULT_MAX_TTL)
|
|
44
|
+
unless redis.respond_to?(:eval) && redis.respond_to?(:evalsha) && redis.respond_to?(:call)
|
|
45
|
+
raise ConfigurationError, "redis: must be a Redis client that runs scripts (eval, evalsha and call), like Redis.new from the redis gem"
|
|
46
|
+
end
|
|
47
|
+
raise ConfigurationError, "prefix: must be a non-empty String, got #{prefix.inspect}" unless prefix.is_a?(String) && !prefix.empty?
|
|
48
|
+
unless purge_replicas.nil? || (purge_replicas.is_a?(Integer) && purge_replicas.positive?)
|
|
49
|
+
raise ConfigurationError, "purge_replicas: must be nil or a positive Integer, got #{purge_replicas.inspect}"
|
|
50
|
+
end
|
|
51
|
+
unless purge_timeout.is_a?(Numeric) && purge_timeout.positive?
|
|
52
|
+
raise ConfigurationError, "purge_timeout: must be a positive number of seconds, got #{purge_timeout.inspect}"
|
|
53
|
+
end
|
|
54
|
+
raise ConfigurationError, "max_ttl: must be a positive Integer of seconds, got #{max_ttl.inspect}" unless max_ttl.is_a?(Integer) && max_ttl.positive?
|
|
55
|
+
|
|
56
|
+
new(redis: redis, prefix: prefix, purge_replicas: purge_replicas, purge_timeout: purge_timeout, max_ttl: max_ttl)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
attr_reader :prefix, :max_ttl, :purge_replicas, :purge_timeout
|
|
60
|
+
|
|
61
|
+
# `new` is inert: with no Redis wired every lookup misses, every write is refused and every purge raises.
|
|
62
|
+
def initialize(redis: Client::Null.new, prefix: DEFAULT_PREFIX, purge_replicas: nil, purge_timeout: DEFAULT_PURGE_TIMEOUT, max_ttl: DEFAULT_MAX_TTL)
|
|
63
|
+
@redis = redis
|
|
64
|
+
@prefix = prefix
|
|
65
|
+
@purge_replicas = purge_replicas
|
|
66
|
+
@purge_timeout = purge_timeout
|
|
67
|
+
@max_ttl = max_ttl
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def lookup(key_digest)
|
|
71
|
+
check_digest!(key_digest, "key_digest")
|
|
72
|
+
return Lookup.miss(seq: 0, epoch: "unwired", now: 0) unless wired?
|
|
73
|
+
|
|
74
|
+
blob, tags, marks, seq, epoch, now = script(Scripts::LOOKUP, [meta_key, entry_key(key_digest)], SecureRandom.hex(16), @max_ttl)
|
|
75
|
+
Lookup.new(blob: blob, tags: tags, marks: marks.each_slice(2).to_h { |tag, mark| [tag, Integer(mark)] }, seq: seq, epoch: epoch, now: now)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def write(key_digest, blob, tags:, ttl:, seq:, epoch:, stored_at:)
|
|
79
|
+
check_write!(key_digest, blob, tags, ttl, seq, epoch, stored_at)
|
|
80
|
+
return false unless wired?
|
|
81
|
+
|
|
82
|
+
stored = script(Scripts::WRITE, [meta_key, entry_key(key_digest)], SecureRandom.hex(16), @max_ttl, blob, JSON.generate(tags), ttl, seq, epoch, stored_at)
|
|
83
|
+
stored == 1
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def purge(tag_digests)
|
|
87
|
+
raise ArgumentError, "tag_digests must be an Array" unless tag_digests.is_a?(Array)
|
|
88
|
+
|
|
89
|
+
tag_digests.each { |tag| check_digest!(tag, "tag digest") }
|
|
90
|
+
unwired! unless wired?
|
|
91
|
+
|
|
92
|
+
seq = script(Scripts::PURGE, [meta_key], SecureRandom.hex(16), @max_ttl, *tag_digests.uniq)
|
|
93
|
+
replicated!
|
|
94
|
+
seq
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def purge_all
|
|
98
|
+
unwired! unless wired?
|
|
99
|
+
|
|
100
|
+
epoch = script(Scripts::PURGE_ALL, [meta_key], SecureRandom.hex(16), @max_ttl)
|
|
101
|
+
replicated!
|
|
102
|
+
epoch
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def inspect = "#<#{self.class.name} prefix=#{@prefix.inspect} max_ttl=#{@max_ttl}>"
|
|
106
|
+
alias_method :to_s, :inspect
|
|
107
|
+
|
|
108
|
+
private
|
|
109
|
+
|
|
110
|
+
def wired?
|
|
111
|
+
!@redis.is_a?(Client::Null)
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def unwired!
|
|
115
|
+
raise PurgeError, "RedisStore has no Redis wired, so nothing was purged: build it with RedisStore.build(redis:)"
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def meta_key = "#{@prefix}meta"
|
|
119
|
+
def entry_key(key_digest) = "#{@prefix}e:#{key_digest}"
|
|
120
|
+
|
|
121
|
+
# Only keyed hex digests reach the keyspace: nothing in it names a viewer, a tag or an operation in clear.
|
|
122
|
+
def check_digest!(value, name)
|
|
123
|
+
raise ArgumentError, "#{name} must be 64 hex characters" unless value.is_a?(String) && Key::HEX.match?(value)
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def check_write!(key_digest, blob, tags, ttl, seq, epoch, stored_at)
|
|
127
|
+
check_digest!(key_digest, "key_digest")
|
|
128
|
+
raise ArgumentError, "blob must be a String" unless blob.is_a?(String)
|
|
129
|
+
raise ArgumentError, "tags: must be an Array" unless tags.is_a?(Array)
|
|
130
|
+
|
|
131
|
+
tags.each { |tag| check_digest!(tag, "tag digest") }
|
|
132
|
+
raise ArgumentError, "ttl: must be a positive Integer of seconds" unless ttl.is_a?(Integer) && ttl.positive?
|
|
133
|
+
raise ArgumentError, "ttl: #{ttl} is above this store's max_ttl (#{@max_ttl}): purge marks are kept only that long" if ttl > @max_ttl
|
|
134
|
+
raise ArgumentError, "seq: must be a non-negative Integer" unless seq.is_a?(Integer) && seq >= 0
|
|
135
|
+
raise ArgumentError, "epoch: must be a non-empty String" unless epoch.is_a?(String) && !epoch.empty?
|
|
136
|
+
raise ArgumentError, "stored_at: must be Integer milliseconds" unless stored_at.is_a?(Integer) && stored_at >= 0
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# WAIT blocks until `purge_replicas` replicas acknowledged every write of this connection, or the timeout.
|
|
140
|
+
def replicated!
|
|
141
|
+
return unless @purge_replicas
|
|
142
|
+
|
|
143
|
+
acknowledged = @redis.call("WAIT", @purge_replicas, (@purge_timeout * 1000).ceil)
|
|
144
|
+
return if acknowledged >= @purge_replicas
|
|
145
|
+
|
|
146
|
+
raise PurgeError, "the purge reached #{acknowledged} of #{@purge_replicas} replicas within #{@purge_timeout}s; retry it"
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# EVALSHA sends only the script's digest; Redis forgets scripts on restart or SCRIPT FLUSH, and then EVAL
|
|
150
|
+
# sends it whole once more.
|
|
151
|
+
def script(source, keys, *argv)
|
|
152
|
+
argv = argv.map(&:to_s)
|
|
153
|
+
@redis.evalsha(Digest::SHA1.hexdigest(source), keys: keys, argv: argv)
|
|
154
|
+
rescue StandardError => error
|
|
155
|
+
raise unless error.message.start_with?("NOSCRIPT")
|
|
156
|
+
|
|
157
|
+
@redis.eval(source, keys: keys, argv: argv)
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rspec/expectations"
|
|
4
|
+
require "graphql/armory/caching/audit"
|
|
5
|
+
|
|
6
|
+
# `require "graphql/armory/caching/rspec"` in your spec helper, then:
|
|
7
|
+
#
|
|
8
|
+
# expect(AppSchema).to have_safe_cache_declarations
|
|
9
|
+
# expect(AppSchema).to have_safe_cache_declarations(own_checks: { Types::Product => { authorized?: "reads the class only" } })
|
|
10
|
+
# expect(AppSchema).to have_safe_cache_declarations(strict: true) # information (:directive) fails it too
|
|
11
|
+
RSpec::Matchers.define :have_safe_cache_declarations do |*positional, own_checks: {}, strict: false|
|
|
12
|
+
raise ArgumentError, "have_safe_cache_declarations takes own_checks: and strict:" unless positional.empty?
|
|
13
|
+
|
|
14
|
+
match do |schema|
|
|
15
|
+
(@report = GraphQL::Armory::Caching::Audit.(schema, own_checks: own_checks, strict: strict)).ok?
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
failure_message do |schema|
|
|
19
|
+
"expected #{schema} to have safe cache declarations, but the cache audit found:\n#{@report}"
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
failure_message_when_negated do |schema|
|
|
23
|
+
"expected #{schema} not to have safe cache declarations, but the cache audit found no problems"
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
|
|
5
|
+
module GraphQL
|
|
6
|
+
module Armory
|
|
7
|
+
module Caching
|
|
8
|
+
# What a query runs against, as two SHA-256 digests the keyer puts in every key:
|
|
9
|
+
#
|
|
10
|
+
# identity = SchemaIdentity.new
|
|
11
|
+
# identity.schema_digest(AppSchema, "2025-06-01", :"armory_v_2025-06-01") # to_definition of what that version sees
|
|
12
|
+
# identity.document_digest(query.document) # the printed document
|
|
13
|
+
#
|
|
14
|
+
# The schema digest is computed on the first request that needs it, never when `use` runs (the schema is not
|
|
15
|
+
# finished then), once per schema, version label and visibility profile. A schema reloaded in development, or two
|
|
16
|
+
# deploys that share a `release:` by mistake, never share answers across definitions. The document digest is
|
|
17
|
+
# memoized per document object: PersistedQueries' stored documents are shared, so each is printed once per process.
|
|
18
|
+
#
|
|
19
|
+
# Both memos compare by identity (two equal documents are two entries, never one digest for two objects) and are
|
|
20
|
+
# bounded: past `limit` entries a memo starts over, so raw documents or reloaded schemas cannot grow it without end.
|
|
21
|
+
# They are not `ObjectSpace::WeakMap`s: a WeakMap holds its values weakly too, so a digest String held only there is
|
|
22
|
+
# collected at the next GC. Two threads computing the same first digest compute the same value; the first stored
|
|
23
|
+
# wins. `computed` records each computation, for specs.
|
|
24
|
+
class SchemaIdentity
|
|
25
|
+
LIMIT = 10_000
|
|
26
|
+
# `computed` keeps the latest computations only.
|
|
27
|
+
RECORDED = 100
|
|
28
|
+
|
|
29
|
+
# graphql-ruby could not print the schema (graphql before 2.6 with json 3.x raises on a deprecation reason). The
|
|
30
|
+
# failure is memoized like a digest: the schema is not printed again on every request.
|
|
31
|
+
class Unprintable < StandardError; end
|
|
32
|
+
UNPRINTABLE = :unprintable
|
|
33
|
+
|
|
34
|
+
attr_reader :computed
|
|
35
|
+
|
|
36
|
+
def initialize(limit: LIMIT)
|
|
37
|
+
raise ArgumentError, "limit: must be a positive Integer" unless limit.is_a?(Integer) && limit.positive?
|
|
38
|
+
|
|
39
|
+
@limit = limit
|
|
40
|
+
@schemas = {}.compare_by_identity
|
|
41
|
+
@documents = {}.compare_by_identity
|
|
42
|
+
@lock = Mutex.new
|
|
43
|
+
@computed = []
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# SHA-256 of `schema.to_definition(context: { armory_full_schema: true, armory_version: label })`, with
|
|
47
|
+
# `visibility_profile: profile` when the query runs under a named profile (Versioning's `profiles: true` names
|
|
48
|
+
# the same one from the label). `armory_full_schema` lets Authorization's `visible_when:` show every member, so the
|
|
49
|
+
# digest is the schema's and not one viewer's view of it.
|
|
50
|
+
def schema_digest(schema, label, profile)
|
|
51
|
+
found = @lock.synchronize { @schemas[schema]&.[]([label, profile]) }
|
|
52
|
+
found ||= compute_schema_digest(schema, label, profile)
|
|
53
|
+
raise Unprintable, "#{schema.name || "the schema"} cannot be printed by this graphql-ruby" if found == UNPRINTABLE
|
|
54
|
+
|
|
55
|
+
found
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def compute_schema_digest(schema, label, profile)
|
|
59
|
+
context = { armory_full_schema: true }
|
|
60
|
+
context[:armory_version] = label unless label.nil?
|
|
61
|
+
context[:visibility_profile] = profile unless profile.nil?
|
|
62
|
+
digest = begin
|
|
63
|
+
Digest::SHA256.hexdigest(schema.to_definition(context: context)).freeze
|
|
64
|
+
rescue Versioning::RefusedVersionError
|
|
65
|
+
raise
|
|
66
|
+
rescue StandardError
|
|
67
|
+
UNPRINTABLE
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
@lock.synchronize do
|
|
71
|
+
record([:schema, label, profile])
|
|
72
|
+
@schemas.clear if @schemas.size >= @limit && !@schemas.key?(schema)
|
|
73
|
+
(@schemas[schema] ||= {})[[label, profile]] ||= digest
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
private :compute_schema_digest
|
|
77
|
+
|
|
78
|
+
# SHA-256 of `document.to_query_string`.
|
|
79
|
+
def document_digest(document)
|
|
80
|
+
found = @lock.synchronize { @documents[document] }
|
|
81
|
+
return found if found
|
|
82
|
+
|
|
83
|
+
digest = Digest::SHA256.hexdigest(document.to_query_string).freeze
|
|
84
|
+
@lock.synchronize do
|
|
85
|
+
record([:document])
|
|
86
|
+
@documents.clear if @documents.size >= @limit
|
|
87
|
+
@documents[document] ||= digest
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def inspect = "#<#{self.class.name}>"
|
|
92
|
+
|
|
93
|
+
private
|
|
94
|
+
|
|
95
|
+
def record(computation)
|
|
96
|
+
@computed.shift if @computed.size >= RECORDED
|
|
97
|
+
@computed << computation
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Caching
|
|
6
|
+
module Store
|
|
7
|
+
# The store of a cache nobody wired: every lookup misses, every write is refused and purges change nothing.
|
|
8
|
+
# It caches nothing, so `Gate.new` never serves an answer. `use` refuses it: a cache that stores nothing.
|
|
9
|
+
class Null
|
|
10
|
+
EPOCH = "null"
|
|
11
|
+
|
|
12
|
+
def lookup(_key_digest)
|
|
13
|
+
Lookup.miss(seq: 0, epoch: EPOCH, now: 0)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def write(_key_digest, _blob, tags:, ttl:, seq:, epoch:, stored_at:)
|
|
17
|
+
false
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def purge(_tag_digests)
|
|
21
|
+
0
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def purge_all
|
|
25
|
+
EPOCH
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "securerandom"
|
|
4
|
+
|
|
5
|
+
module GraphQL
|
|
6
|
+
module Armory
|
|
7
|
+
module Caching
|
|
8
|
+
module Store
|
|
9
|
+
# An in-memory store on a clock the test moves (`now`, Integer milliseconds) that keeps the store contract
|
|
10
|
+
# (`Store`), records its calls, fails when told, answers what it is told and can lose its meta record: for
|
|
11
|
+
# tests, instead of hand-written fakes.
|
|
12
|
+
#
|
|
13
|
+
# store = Store::Substitute.new
|
|
14
|
+
# store.fail_with(Redis::TimeoutError.new, on: :write)
|
|
15
|
+
# store.answer(:lookup, some_lookup)
|
|
16
|
+
# store.reset! # the meta record is gone: every older entry fails the epoch check
|
|
17
|
+
# store.calls # => [[:lookup, key_digest, {}], [:write, key_digest, {tags:, ...}], ...]
|
|
18
|
+
class Substitute
|
|
19
|
+
OPERATIONS = %i[lookup write purge purge_all].freeze
|
|
20
|
+
|
|
21
|
+
attr_accessor :now
|
|
22
|
+
attr_reader :calls
|
|
23
|
+
|
|
24
|
+
def initialize(now: 1_000_000)
|
|
25
|
+
raise ArgumentError, "now: must be Integer milliseconds" unless now.is_a?(Integer)
|
|
26
|
+
|
|
27
|
+
@now = now
|
|
28
|
+
@entries = {}
|
|
29
|
+
@meta = nil
|
|
30
|
+
@calls = []
|
|
31
|
+
@failures = {}
|
|
32
|
+
@answers = {}
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Raises `error` from `on` (an operation, or :all) for the next `times` calls, or every call until `recover`.
|
|
36
|
+
def fail_with(error, on: :all, times: nil)
|
|
37
|
+
raise ArgumentError, "on: must be one of #{(OPERATIONS + [:all]).inspect}, got #{on.inspect}" unless (OPERATIONS + [:all]).include?(on)
|
|
38
|
+
raise ArgumentError, "times: must be nil (every call) or a positive Integer, got #{times.inspect}" unless times.nil? || (times.is_a?(Integer) && times.positive?)
|
|
39
|
+
|
|
40
|
+
@failures[on] = [error, times]
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def recover
|
|
44
|
+
@failures.clear
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Answers `value` from `operation` instead of the real answer, for answers no real store gives.
|
|
48
|
+
def answer(operation, value)
|
|
49
|
+
raise ArgumentError, "operation must be one of #{OPERATIONS.inspect}" unless OPERATIONS.include?(operation)
|
|
50
|
+
|
|
51
|
+
@answers[operation] = value
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Loses the meta record (eviction, a flush, a failover to a replica without it). Entries stay. Not recorded.
|
|
55
|
+
def reset!
|
|
56
|
+
@meta = nil
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Moves the store's seq back, as a failover to a replica that lost recent increments would. Not recorded.
|
|
60
|
+
def rewind_seq(to)
|
|
61
|
+
meta[:seq] = to
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# The current meta record, for assertions. Not recorded.
|
|
65
|
+
def epoch = meta[:epoch]
|
|
66
|
+
def seq = meta[:seq]
|
|
67
|
+
|
|
68
|
+
# Every stored blob by key digest, for assertions such as "nothing in the store names the viewer". Not recorded.
|
|
69
|
+
def blobs
|
|
70
|
+
@entries.transform_values { |entry| entry[:blob] }
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Overwrites what is stored under `key_digest` as someone with write access to the store could. Not recorded.
|
|
74
|
+
def poison(key_digest, blob, tags: [], expires_at: @now + 60_000)
|
|
75
|
+
@entries[key_digest] = { blob: blob, tags: tags, expires_at: expires_at }
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def lookup(key_digest)
|
|
79
|
+
perform(:lookup, key_digest) do
|
|
80
|
+
entry = @entries[key_digest]
|
|
81
|
+
entry = nil if entry && @now >= entry[:expires_at]
|
|
82
|
+
tags = entry ? entry[:tags] : []
|
|
83
|
+
Lookup.new(blob: entry&.fetch(:blob), tags: tags, marks: meta[:marks].slice(*tags), seq: meta[:seq], epoch: meta[:epoch], now: @now)
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def write(key_digest, blob, tags:, ttl:, seq:, epoch:, stored_at:)
|
|
88
|
+
perform(:write, key_digest, tags: tags, ttl: ttl, seq: seq, epoch: epoch, stored_at: stored_at) do
|
|
89
|
+
expires_at = stored_at + (ttl * 1000)
|
|
90
|
+
if epoch != meta[:epoch] || seq > meta[:seq] || tags.any? { |tag| meta[:marks].fetch(tag, -1) > seq } || @now >= expires_at
|
|
91
|
+
false
|
|
92
|
+
else
|
|
93
|
+
@entries[key_digest] = { blob: blob, tags: tags.dup, expires_at: expires_at }
|
|
94
|
+
true
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def purge(tag_digests)
|
|
100
|
+
perform(:purge, nil, tags: tag_digests) do
|
|
101
|
+
meta[:seq] += 1
|
|
102
|
+
tag_digests.each { |tag| meta[:marks][tag] = meta[:seq] }
|
|
103
|
+
meta[:seq]
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def purge_all
|
|
108
|
+
perform(:purge_all, nil) do
|
|
109
|
+
meta[:epoch] = SecureRandom.hex(16)
|
|
110
|
+
meta[:marks].clear
|
|
111
|
+
meta[:epoch]
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
private
|
|
116
|
+
|
|
117
|
+
def meta
|
|
118
|
+
@meta ||= { epoch: SecureRandom.hex(16), seq: 0, marks: {} }
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def perform(operation, key_digest, **arguments)
|
|
122
|
+
@calls << [operation, key_digest, arguments]
|
|
123
|
+
fail!(operation)
|
|
124
|
+
return @answers[operation] if @answers.key?(operation)
|
|
125
|
+
|
|
126
|
+
yield
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def fail!(operation)
|
|
130
|
+
on = @failures.key?(operation) ? operation : :all
|
|
131
|
+
error, times = @failures[on]
|
|
132
|
+
return unless error
|
|
133
|
+
|
|
134
|
+
if times
|
|
135
|
+
times -= 1
|
|
136
|
+
times.zero? ? @failures.delete(on) : @failures[on] = [error, times]
|
|
137
|
+
end
|
|
138
|
+
raise error
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Caching
|
|
6
|
+
# Where answers live: `MemoryStore`, `RedisStore`, or any object answering the same four methods. Every key and
|
|
7
|
+
# tag a store sees is a keyed hex digest; a store never sees a viewer, a variable or an operation in clear.
|
|
8
|
+
#
|
|
9
|
+
# store.lookup(key_digest) -> Lookup # one round trip
|
|
10
|
+
# store.write(key_digest, blob, tags:, ttl:, seq:, epoch:, stored_at:) -> true | false
|
|
11
|
+
# store.purge(tag_digests) -> Integer # the new seq; raises on failure
|
|
12
|
+
# store.purge_all -> String # the new epoch; raises on failure
|
|
13
|
+
#
|
|
14
|
+
# The contract every store keeps (`spec/support/caching_store_contract.rb`):
|
|
15
|
+
#
|
|
16
|
+
# - One meta record holds `epoch` (a random String), `seq` (an Integer, 0 when created) and a purge mark per
|
|
17
|
+
# purged tag. It is never evicted on its own. `lookup` and `write` create it, with a new random epoch, when it
|
|
18
|
+
# is missing: a lost meta record is "everything purged".
|
|
19
|
+
# - `lookup` answers the blob and its tag digests only while `now < stored_at + ttl * 1000`, with the marks of
|
|
20
|
+
# those tags, the current `seq` and `epoch`, and the store clock `now` (Integer milliseconds).
|
|
21
|
+
# - `write` answers false and stores nothing when `epoch` is not the current one, when `seq` is greater than the
|
|
22
|
+
# current seq (a replica that lost increments), when any of `tags` has a mark greater than `seq` (a purge
|
|
23
|
+
# landed after the lookup), or when the entry is already expired at the store clock. Otherwise it stores the
|
|
24
|
+
# blob with its tags, expiring at `stored_at + ttl * 1000`, and answers true.
|
|
25
|
+
# - `purge` increments `seq` and sets each tag's mark to the new value, atomically; `purge_all` replaces the
|
|
26
|
+
# epoch (every entry stored before fails the epoch check) and drops the marks.
|
|
27
|
+
module Store
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
require "graphql/armory/caching/store/null"
|
|
34
|
+
require "graphql/armory/caching/store/substitute"
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module GraphQL
|
|
6
|
+
module Armory
|
|
7
|
+
module Caching
|
|
8
|
+
# The purge tags an entry carries, in clear, before the Digester turns them into keyed digests. The walk builds
|
|
9
|
+
# `custom` and `type` tags into the Policy, the keyer the `viewer` tag, and `purge` the same strings from what the
|
|
10
|
+
# app passes, so both sides always agree. Each kind has its own prefix, so a custom tag can never be a type or a
|
|
11
|
+
# viewer tag; the viewer's class and id are JSON-encoded, so no id can forge a separator.
|
|
12
|
+
module Tags
|
|
13
|
+
ANONYMOUS = "viewer:anonymous"
|
|
14
|
+
|
|
15
|
+
module_function
|
|
16
|
+
|
|
17
|
+
def custom(name)
|
|
18
|
+
raise ArgumentError, "a custom tag is a non-empty String" unless name.is_a?(String) && !name.strip.empty?
|
|
19
|
+
|
|
20
|
+
"tag:#{name}"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def type(graphql_name)
|
|
24
|
+
raise ArgumentError, "a type tag takes a GraphQL type name" unless graphql_name.is_a?(String) && !graphql_name.empty?
|
|
25
|
+
|
|
26
|
+
"type:#{graphql_name}"
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# `class_name` is the principal's Ruby class name, `id` a non-empty String or an Integer.
|
|
30
|
+
def viewer(class_name, id)
|
|
31
|
+
raise ArgumentError, "a viewer tag needs the principal's class name" unless class_name.is_a?(String) && !class_name.empty?
|
|
32
|
+
raise ArgumentError, "a viewer id is a non-empty String or an Integer" unless id.is_a?(Integer) || (id.is_a?(String) && !id.empty?)
|
|
33
|
+
|
|
34
|
+
"viewer:#{JSON.generate([class_name, id])}"
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GraphQL
|
|
4
|
+
module Armory
|
|
5
|
+
module Caching
|
|
6
|
+
# What the gate decided for one query: answer it from `entry` (hit), run it and maybe store it under `key`
|
|
7
|
+
# (miss, with the `policy` and the `lookup` it was decided on), or run it uncached (pass, with a `reason`).
|
|
8
|
+
Decision = Struct.new(:kind, :entry, :key, :policy, :lookup, :reason, keyword_init: true) do
|
|
9
|
+
def self.hit(entry)
|
|
10
|
+
new(kind: :hit, entry: entry, key: nil, policy: nil, lookup: nil, reason: nil)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def self.miss(key, policy, lookup)
|
|
14
|
+
new(kind: :miss, entry: nil, key: key, policy: policy, lookup: lookup, reason: nil)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.pass(reason)
|
|
18
|
+
new(kind: :pass, entry: nil, key: nil, policy: nil, lookup: nil, reason: reason)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def initialize(kind:, entry:, key:, policy:, lookup:, reason:)
|
|
22
|
+
case kind
|
|
23
|
+
when :hit
|
|
24
|
+
raise ArgumentError, "a hit needs an Entry" unless entry.is_a?(Entry)
|
|
25
|
+
when :miss
|
|
26
|
+
raise ArgumentError, "a miss needs a Key" unless key.is_a?(Key)
|
|
27
|
+
raise ArgumentError, "a miss needs a cacheable Policy" unless policy.is_a?(Policy) && policy.cacheable?
|
|
28
|
+
raise ArgumentError, "a miss needs a Lookup" unless lookup.is_a?(Lookup)
|
|
29
|
+
when :pass
|
|
30
|
+
raise ArgumentError, "a pass needs a reason Symbol, got #{reason.inspect}" unless reason.is_a?(Symbol)
|
|
31
|
+
else
|
|
32
|
+
raise ArgumentError, "Decision kind must be :hit, :miss or :pass, got #{kind.inspect}"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
super
|
|
36
|
+
freeze
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def hit? = kind == :hit
|
|
40
|
+
def miss? = kind == :miss
|
|
41
|
+
def pass? = kind == :pass
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|