sendproof-core 0.1.0.alpha.1
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 +7 -0
- data/LICENSE +21 -0
- data/README.md +55 -0
- data/lib/sendproof/assets.rb +70 -0
- data/lib/sendproof/attribution.rb +98 -0
- data/lib/sendproof/capture.rb +66 -0
- data/lib/sendproof/clock.rb +54 -0
- data/lib/sendproof/configuration.rb +241 -0
- data/lib/sendproof/data_store.rb +34 -0
- data/lib/sendproof/dynamodb/store.rb +183 -0
- data/lib/sendproof/dynamodb.rb +17 -0
- data/lib/sendproof/elasticsearch/store.rb +24 -0
- data/lib/sendproof/elasticsearch.rb +17 -0
- data/lib/sendproof/firestore/interceptor.rb +38 -0
- data/lib/sendproof/firestore/store.rb +150 -0
- data/lib/sendproof/firestore.rb +99 -0
- data/lib/sendproof/handler.rb +23 -0
- data/lib/sendproof/identity.rb +37 -0
- data/lib/sendproof/limits.rb +113 -0
- data/lib/sendproof/middleware.rb +68 -0
- data/lib/sendproof/mongo/engine.rb +692 -0
- data/lib/sendproof/mongo/guarded.rb +100 -0
- data/lib/sendproof/mongo/plain.rb +45 -0
- data/lib/sendproof/mongo/store.rb +54 -0
- data/lib/sendproof/mongo/subscriber.rb +21 -0
- data/lib/sendproof/mongo.rb +41 -0
- data/lib/sendproof/mongoid/store.rb +58 -0
- data/lib/sendproof/mongoid.rb +65 -0
- data/lib/sendproof/observed_request.rb +108 -0
- data/lib/sendproof/opensearch/store.rb +21 -0
- data/lib/sendproof/opensearch.rb +17 -0
- data/lib/sendproof/operation_matcher.rb +28 -0
- data/lib/sendproof/pool_hooks.rb +80 -0
- data/lib/sendproof/preview_request.rb +44 -0
- data/lib/sendproof/preview_transport.rb +331 -0
- data/lib/sendproof/protocol.rb +81 -0
- data/lib/sendproof/provider.rb +83 -0
- data/lib/sendproof/recorder.rb +298 -0
- data/lib/sendproof/redaction.rb +98 -0
- data/lib/sendproof/redis/commands.rb +76 -0
- data/lib/sendproof/redis/store.rb +205 -0
- data/lib/sendproof/redis.rb +18 -0
- data/lib/sendproof/search/middleware.rb +39 -0
- data/lib/sendproof/search/store.rb +213 -0
- data/lib/sendproof/search.rb +82 -0
- data/lib/sendproof/stores/classify.rb +346 -0
- data/lib/sendproof/stores/guard.rb +127 -0
- data/lib/sendproof/stores/tables.rb +1328 -0
- data/lib/sendproof/stores.rb +12 -0
- data/lib/sendproof/trace_store.rb +110 -0
- data/lib/sendproof/transaction_control.rb +187 -0
- data/lib/sendproof/ui/action-bar.js +91 -0
- data/lib/sendproof/ui/api.js +239 -0
- data/lib/sendproof/ui/app.css +848 -0
- data/lib/sendproof/ui/app.js +742 -0
- data/lib/sendproof/ui/autofill.js +137 -0
- data/lib/sendproof/ui/code-copy.js +71 -0
- data/lib/sendproof/ui/code-picker.js +149 -0
- data/lib/sendproof/ui/commands.js +197 -0
- data/lib/sendproof/ui/components.js +77 -0
- data/lib/sendproof/ui/context-bar.js +82 -0
- data/lib/sendproof/ui/copy.js +92 -0
- data/lib/sendproof/ui/coverage-popover.js +35 -0
- data/lib/sendproof/ui/crumb-fit.js +80 -0
- data/lib/sendproof/ui/deprecation.js +164 -0
- data/lib/sendproof/ui/display-settings.js +175 -0
- data/lib/sendproof/ui/doc-diff.js +209 -0
- data/lib/sendproof/ui/docs-view.js +347 -0
- data/lib/sendproof/ui/favicon.png +0 -0
- data/lib/sendproof/ui/favicon.svg +1 -0
- data/lib/sendproof/ui/field-help.js +52 -0
- data/lib/sendproof/ui/field-modules.js +23 -0
- data/lib/sendproof/ui/fields/controls.js +179 -0
- data/lib/sendproof/ui/fields/dom.js +36 -0
- data/lib/sendproof/ui/fields/infer.js +71 -0
- data/lib/sendproof/ui/fields/lookup-source.js +63 -0
- data/lib/sendproof/ui/fields/lookup.js +285 -0
- data/lib/sendproof/ui/fields/render.js +496 -0
- data/lib/sendproof/ui/fields/select.js +175 -0
- data/lib/sendproof/ui/fields/validate.js +363 -0
- data/lib/sendproof/ui/fields/validation-formats.js +164 -0
- data/lib/sendproof/ui/fields/validation-references.js +68 -0
- data/lib/sendproof/ui/fields/validation-values.js +137 -0
- data/lib/sendproof/ui/generate-menu.js +44 -0
- data/lib/sendproof/ui/generate.js +184 -0
- data/lib/sendproof/ui/header-editor.js +777 -0
- data/lib/sendproof/ui/headers.js +188 -0
- data/lib/sendproof/ui/highlight.js +257 -0
- data/lib/sendproof/ui/history-popover.js +24 -0
- data/lib/sendproof/ui/i18n.js +253 -0
- data/lib/sendproof/ui/icons.js +87 -0
- data/lib/sendproof/ui/index.html +70 -0
- data/lib/sendproof/ui/issues.js +187 -0
- data/lib/sendproof/ui/json-view.js +166 -0
- data/lib/sendproof/ui/lang-icons.js +62 -0
- data/lib/sendproof/ui/layout.js +262 -0
- data/lib/sendproof/ui/listbox.js +334 -0
- data/lib/sendproof/ui/locales/ar-capture.js +366 -0
- data/lib/sendproof/ui/locales/ar-stores.js +116 -0
- data/lib/sendproof/ui/locales/ar.js +561 -0
- data/lib/sendproof/ui/locales/cs-capture.js +442 -0
- data/lib/sendproof/ui/locales/cs-stores.js +116 -0
- data/lib/sendproof/ui/locales/cs.js +689 -0
- data/lib/sendproof/ui/locales/da-capture.js +347 -0
- data/lib/sendproof/ui/locales/da-stores.js +116 -0
- data/lib/sendproof/ui/locales/da.js +522 -0
- data/lib/sendproof/ui/locales/de-capture.js +347 -0
- data/lib/sendproof/ui/locales/de-stores.js +116 -0
- data/lib/sendproof/ui/locales/de.js +519 -0
- data/lib/sendproof/ui/locales/el-capture.js +347 -0
- data/lib/sendproof/ui/locales/el-stores.js +116 -0
- data/lib/sendproof/ui/locales/el.js +520 -0
- data/lib/sendproof/ui/locales/en-capture.js +349 -0
- data/lib/sendproof/ui/locales/en-stores.js +95 -0
- data/lib/sendproof/ui/locales/en.js +522 -0
- data/lib/sendproof/ui/locales/es-capture.js +353 -0
- data/lib/sendproof/ui/locales/es-stores.js +116 -0
- data/lib/sendproof/ui/locales/es.js +525 -0
- data/lib/sendproof/ui/locales/fa-capture.js +348 -0
- data/lib/sendproof/ui/locales/fa-stores.js +116 -0
- data/lib/sendproof/ui/locales/fa.js +521 -0
- data/lib/sendproof/ui/locales/fi-capture.js +347 -0
- data/lib/sendproof/ui/locales/fi-stores.js +116 -0
- data/lib/sendproof/ui/locales/fi.js +523 -0
- data/lib/sendproof/ui/locales/fr-capture.js +353 -0
- data/lib/sendproof/ui/locales/fr-stores.js +116 -0
- data/lib/sendproof/ui/locales/fr.js +525 -0
- data/lib/sendproof/ui/locales/he-capture.js +358 -0
- data/lib/sendproof/ui/locales/he-stores.js +116 -0
- data/lib/sendproof/ui/locales/he.js +529 -0
- data/lib/sendproof/ui/locales/hi-capture.js +348 -0
- data/lib/sendproof/ui/locales/hi-stores.js +116 -0
- data/lib/sendproof/ui/locales/hi.js +523 -0
- data/lib/sendproof/ui/locales/id-capture.js +347 -0
- data/lib/sendproof/ui/locales/id-stores.js +116 -0
- data/lib/sendproof/ui/locales/id.js +521 -0
- data/lib/sendproof/ui/locales/it-capture.js +361 -0
- data/lib/sendproof/ui/locales/it-stores.js +116 -0
- data/lib/sendproof/ui/locales/it.js +523 -0
- data/lib/sendproof/ui/locales/ja-capture.js +346 -0
- data/lib/sendproof/ui/locales/ja-stores.js +116 -0
- data/lib/sendproof/ui/locales/ja.js +521 -0
- data/lib/sendproof/ui/locales/ko-capture.js +346 -0
- data/lib/sendproof/ui/locales/ko-stores.js +116 -0
- data/lib/sendproof/ui/locales/ko.js +522 -0
- data/lib/sendproof/ui/locales/nb-capture.js +349 -0
- data/lib/sendproof/ui/locales/nb-stores.js +116 -0
- data/lib/sendproof/ui/locales/nb.js +521 -0
- data/lib/sendproof/ui/locales/nl-capture.js +355 -0
- data/lib/sendproof/ui/locales/nl-stores.js +116 -0
- data/lib/sendproof/ui/locales/nl.js +520 -0
- data/lib/sendproof/ui/locales/pl-capture.js +432 -0
- data/lib/sendproof/ui/locales/pl-stores.js +116 -0
- data/lib/sendproof/ui/locales/pl.js +528 -0
- data/lib/sendproof/ui/locales/pt-BR-capture.js +350 -0
- data/lib/sendproof/ui/locales/pt-BR-stores.js +116 -0
- data/lib/sendproof/ui/locales/pt-BR.js +523 -0
- data/lib/sendproof/ui/locales/pt-PT-capture.js +350 -0
- data/lib/sendproof/ui/locales/pt-PT-stores.js +116 -0
- data/lib/sendproof/ui/locales/pt-PT.js +523 -0
- data/lib/sendproof/ui/locales/ru-capture.js +437 -0
- data/lib/sendproof/ui/locales/ru-stores.js +116 -0
- data/lib/sendproof/ui/locales/ru.js +624 -0
- data/lib/sendproof/ui/locales/sv-capture.js +349 -0
- data/lib/sendproof/ui/locales/sv-stores.js +116 -0
- data/lib/sendproof/ui/locales/sv.js +518 -0
- data/lib/sendproof/ui/locales/th-capture.js +347 -0
- data/lib/sendproof/ui/locales/th-stores.js +116 -0
- data/lib/sendproof/ui/locales/th.js +521 -0
- data/lib/sendproof/ui/locales/tr-capture.js +348 -0
- data/lib/sendproof/ui/locales/tr-stores.js +116 -0
- data/lib/sendproof/ui/locales/tr.js +521 -0
- data/lib/sendproof/ui/locales/uk-capture.js +442 -0
- data/lib/sendproof/ui/locales/uk-stores.js +116 -0
- data/lib/sendproof/ui/locales/uk.js +630 -0
- data/lib/sendproof/ui/locales/vi-capture.js +346 -0
- data/lib/sendproof/ui/locales/vi-stores.js +116 -0
- data/lib/sendproof/ui/locales/vi.js +523 -0
- data/lib/sendproof/ui/locales/zh-Hans-capture.js +347 -0
- data/lib/sendproof/ui/locales/zh-Hans-stores.js +116 -0
- data/lib/sendproof/ui/locales/zh-Hans.js +523 -0
- data/lib/sendproof/ui/locales/zh-Hant-capture.js +346 -0
- data/lib/sendproof/ui/locales/zh-Hant-stores.js +116 -0
- data/lib/sendproof/ui/locales/zh-Hant.js +522 -0
- data/lib/sendproof/ui/manifest.json +866 -0
- data/lib/sendproof/ui/markdown.js +39 -0
- data/lib/sendproof/ui/model-view.js +182 -0
- data/lib/sendproof/ui/navigation.js +233 -0
- data/lib/sendproof/ui/operation-header.js +76 -0
- data/lib/sendproof/ui/operations.js +7 -0
- data/lib/sendproof/ui/overview-view.js +63 -0
- data/lib/sendproof/ui/personas.js +59 -0
- data/lib/sendproof/ui/popover.js +71 -0
- data/lib/sendproof/ui/preview-false.js +108 -0
- data/lib/sendproof/ui/request-editor.js +496 -0
- data/lib/sendproof/ui/response-metadata.js +354 -0
- data/lib/sendproof/ui/response-viewer.js +618 -0
- data/lib/sendproof/ui/result-blocked.js +155 -0
- data/lib/sendproof/ui/result-capture.js +25 -0
- data/lib/sendproof/ui/result-database.js +185 -0
- data/lib/sendproof/ui/result-effects.js +176 -0
- data/lib/sendproof/ui/result-headers.js +106 -0
- data/lib/sendproof/ui/result-logs.js +408 -0
- data/lib/sendproof/ui/result-states.js +213 -0
- data/lib/sendproof/ui/results.js +371 -0
- data/lib/sendproof/ui/router.js +118 -0
- data/lib/sendproof/ui/schema-model.js +336 -0
- data/lib/sendproof/ui/schema-tree.js +344 -0
- data/lib/sendproof/ui/schema.js +397 -0
- data/lib/sendproof/ui/security.js +31 -0
- data/lib/sendproof/ui/seeded-menu.js +69 -0
- data/lib/sendproof/ui/settings.js +93 -0
- data/lib/sendproof/ui/shortcuts-sheet.js +73 -0
- data/lib/sendproof/ui/shortcuts.js +190 -0
- data/lib/sendproof/ui/sidebar.js +289 -0
- data/lib/sendproof/ui/signin.js +259 -0
- data/lib/sendproof/ui/snippets.js +530 -0
- data/lib/sendproof/ui/store-marks.js +47 -0
- data/lib/sendproof/ui/stores.js +131 -0
- data/lib/sendproof/ui/styles.css +971 -0
- data/lib/sendproof/ui/toggletip.js +53 -0
- data/lib/sendproof/ui/uuid.js +10 -0
- data/lib/sendproof/ui/view.js +122 -0
- data/lib/sendproof/ui/views.css +653 -0
- data/lib/sendproof/version.rb +9 -0
- data/lib/sendproof/workbench.rb +102 -0
- data/lib/sendproof-core.rb +3 -0
- data/lib/sendproof.rb +121 -0
- metadata +292 -0
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Sendproof
|
|
4
|
+
module Mongo
|
|
5
|
+
# Guarded handles for the plain +mongo+ driver. The driver has no ambient session, so
|
|
6
|
+
# +Store#client+ is a proxy whose +database+, +use+, +[]+ and +collection+ return proxies with the
|
|
7
|
+
# driver's own API, and during a Preview every call that can send a command gets the Preview session
|
|
8
|
+
# in its options:
|
|
9
|
+
#
|
|
10
|
+
# - a call that passes a session other than the Preview's is refused, failing the Preview with 500;
|
|
11
|
+
# - +start_session+ and +with_session+ hand out the Preview session, whose transaction methods join the
|
|
12
|
+
# Preview.
|
|
13
|
+
#
|
|
14
|
+
# The guard itself is not here: the engine's command monitoring classifies every command before it is
|
|
15
|
+
# sent, whichever handle (or none) sent it. These proxies only make the Preview session ride along, so a
|
|
16
|
+
# write made through them is rolled back instead of refused as an escape. Outside a Preview every call
|
|
17
|
+
# passes straight through.
|
|
18
|
+
module Guarded
|
|
19
|
+
# Where each driver method takes its options hash: the number of positional arguments before it.
|
|
20
|
+
OPTIONS = {
|
|
21
|
+
insert_one: 1, insert_many: 1, bulk_write: 1, update_one: 2, update_many: 2, replace_one: 2, delete_one: 1,
|
|
22
|
+
delete_many: 1, find: 1, count_documents: 1, estimated_document_count: 0, distinct: 2, find_one_and_delete: 1,
|
|
23
|
+
find_one_and_replace: 2, find_one_and_update: 2, aggregate: 1, watch: 1, drop: 0, create: 0, indexes: 0,
|
|
24
|
+
command: 1, collection_names: 0, list_collections: 0, collections: 0, list_mongo_databases: 0, database_names: 0,
|
|
25
|
+
list_databases: 0
|
|
26
|
+
}.freeze
|
|
27
|
+
|
|
28
|
+
# The methods that hand back another handle, so the proxy keeps wrapping.
|
|
29
|
+
DERIVED = %i[[] collection database use with].freeze
|
|
30
|
+
|
|
31
|
+
# A proxy around a +Mongo::Client+, +Mongo::Database+ or +Mongo::Collection+.
|
|
32
|
+
class Handle < BasicObject
|
|
33
|
+
def initialize(target, engine)
|
|
34
|
+
@target = target
|
|
35
|
+
@engine = engine
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# @return [Object] the proxied driver object
|
|
39
|
+
def __target__ = @target
|
|
40
|
+
|
|
41
|
+
def respond_to_missing?(name, include_private = false) = @target.respond_to?(name, include_private)
|
|
42
|
+
|
|
43
|
+
def respond_to?(name, include_private = false) = @target.respond_to?(name, include_private)
|
|
44
|
+
|
|
45
|
+
def is_a?(klass) = @target.is_a?(klass)
|
|
46
|
+
alias kind_of? is_a?
|
|
47
|
+
|
|
48
|
+
def inspect = "#<Sendproof guarded #{@target.inspect}>"
|
|
49
|
+
|
|
50
|
+
def ==(other) = @target == (other.respond_to?(:__target__) ? other.__target__ : other)
|
|
51
|
+
|
|
52
|
+
def method_missing(name, *args, **kwargs, &block)
|
|
53
|
+
state = @engine.state
|
|
54
|
+
return derive(name, @target.public_send(name, *args, **kwargs, &block)) unless state
|
|
55
|
+
|
|
56
|
+
@engine.refuse_if_ended(state)
|
|
57
|
+
if name == :start_session
|
|
58
|
+
return state.session
|
|
59
|
+
elsif name == :with_session
|
|
60
|
+
return block.call(state.session)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
result = if OPTIONS.key?(name)
|
|
64
|
+
positional, options = inject(name, args, kwargs, state)
|
|
65
|
+
@target.public_send(name, *positional, options, &block)
|
|
66
|
+
else
|
|
67
|
+
@target.public_send(name, *args, **kwargs, &block)
|
|
68
|
+
end
|
|
69
|
+
derive(name, result)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
private
|
|
73
|
+
|
|
74
|
+
# Adds the Preview session to the call's options, refusing a different explicit session.
|
|
75
|
+
def inject(name, args, kwargs, state)
|
|
76
|
+
# The driver's methods take a trailing options hash, not keywords: Ruby packs keywords into the last
|
|
77
|
+
# positional argument, so do the same (find(_id: 1) is a filter, find({}, limit: 1) has options).
|
|
78
|
+
args += [kwargs] unless kwargs.empty?
|
|
79
|
+
index = OPTIONS.fetch(name)
|
|
80
|
+
positional = args.first(index)
|
|
81
|
+
positional << nil while positional.length < index
|
|
82
|
+
options = args[index].is_a?(::Hash) ? args[index] : {}
|
|
83
|
+
if options[:session] && !options[:session].equal?(state.session)
|
|
84
|
+
::Kernel.raise ::Sendproof::Stores::Guard.refuse_in_preview(
|
|
85
|
+
"A MongoDB call passed its own session during a Preview; the Preview session must carry every command."
|
|
86
|
+
)
|
|
87
|
+
end
|
|
88
|
+
[positional, options.merge(session: state.session)]
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def derive(name, result)
|
|
92
|
+
return result unless DERIVED.include?(name) && result.class.name.to_s.start_with?("Mongo::")
|
|
93
|
+
return result unless [::Mongo::Client, ::Mongo::Database, ::Mongo::Collection].any? { |klass| result.is_a?(klass) }
|
|
94
|
+
|
|
95
|
+
Handle.new(result, @engine)
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "base64"
|
|
4
|
+
require "bigdecimal"
|
|
5
|
+
|
|
6
|
+
module Sendproof
|
|
7
|
+
module Mongo
|
|
8
|
+
# Converts BSON values to plain JSON data, using MongoDB Extended JSON v2 relaxed form for the types
|
|
9
|
+
# JSON lacks (+{"$oid": ...}+, +{"$date": ...}+), so a change record is the document as stored.
|
|
10
|
+
module Plain
|
|
11
|
+
module_function
|
|
12
|
+
|
|
13
|
+
# @return [Object] +nil+, booleans, numbers, strings, arrays and string-keyed hashes
|
|
14
|
+
def call(value)
|
|
15
|
+
case value
|
|
16
|
+
when nil, true, false, Integer, String then value
|
|
17
|
+
when Float then value.finite? ? value : value.to_s
|
|
18
|
+
when Symbol then value.to_s
|
|
19
|
+
when Array then value.map { |item| call(item) }
|
|
20
|
+
when Hash then value.to_h { |name, inner| [name.to_s, call(inner)] }
|
|
21
|
+
when Time then { "$date" => value.utc.iso8601(3) }
|
|
22
|
+
when ::BSON::ObjectId then { "$oid" => value.to_s }
|
|
23
|
+
when ::BSON::Decimal128 then { "$numberDecimal" => value.to_s }
|
|
24
|
+
when BigDecimal then { "$numberDecimal" => value.to_s("F") }
|
|
25
|
+
when ::BSON::Timestamp then { "$timestamp" => { "t" => value.seconds, "i" => value.increment } }
|
|
26
|
+
when ::BSON::Binary then { "$binary" => { "base64" => Base64.strict_encode64(value.data), "subType" => value.type == :generic ? "00" : binary_subtype(value) } }
|
|
27
|
+
when ::BSON::Regexp::Raw then { "$regularExpression" => { "pattern" => value.pattern, "options" => value.options } }
|
|
28
|
+
when Regexp then { "$regularExpression" => { "pattern" => value.source, "options" => regexp_options(value) } }
|
|
29
|
+
when ::BSON::MinKey then { "$minKey" => 1 }
|
|
30
|
+
when ::BSON::MaxKey then { "$maxKey" => 1 }
|
|
31
|
+
when ::BSON::Code then value.code
|
|
32
|
+
else value.respond_to?(:as_json) ? call(value.as_json) : value.to_s
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def binary_subtype(binary)
|
|
37
|
+
{ function: "01", old: "02", uuid_old: "03", uuid: "04", md5: "05", ciphertext: "06", column: "07", sensitive: "08" }.fetch(binary.type, "00")
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def regexp_options(regexp)
|
|
41
|
+
"#{'i' if regexp.options & Regexp::IGNORECASE != 0}#{'m' if regexp.options & Regexp::MULTILINE != 0}"
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Sendproof
|
|
4
|
+
module Mongo
|
|
5
|
+
# The MongoDB data store for the plain +mongo+ driver.
|
|
6
|
+
#
|
|
7
|
+
# catalog = Sendproof::Mongo::Store.new(Mongo::Client.new(url), name: "catalog")
|
|
8
|
+
# config.sendproof.data_store catalog
|
|
9
|
+
# catalog.client[:documents].insert_one(...) # handlers use catalog.client
|
|
10
|
+
#
|
|
11
|
+
# On a replica set or sharded cluster a Preview runs in a transaction that is aborted afterwards; on
|
|
12
|
+
# a standalone server the store runs in +guard+ mode and the first write blocks the Preview.
|
|
13
|
+
class Store < Sendproof::DataStore
|
|
14
|
+
# @return [Object] the guarded client handlers must use: same API, Preview session added during a Preview
|
|
15
|
+
attr_reader :client
|
|
16
|
+
# @return [Engine]
|
|
17
|
+
attr_reader :engine
|
|
18
|
+
|
|
19
|
+
# @param client [Mongo::Client] the driver client to hook
|
|
20
|
+
# @param name [String, nil] display label for discovery, such as +catalog+; never a connection string or host
|
|
21
|
+
# @param treat_as_read [Array<String>, nil] commands you state never write, by exact name (compared
|
|
22
|
+
# case-insensitively, as MongoDB does). A wrongly listed write is not blocked in +guard+ mode.
|
|
23
|
+
# @param preview_timeout [Numeric] seconds a Preview may hold its transaction (default 50, below the
|
|
24
|
+
# server's 60-second lifetime); a Preview still running at the limit answers 500
|
|
25
|
+
# @param hello [#call, nil] test seam: returns the +hello+ document instead of asking the server
|
|
26
|
+
# @param logger [#warn, nil] where a Preview transaction the store couldn't end after a lost abort is
|
|
27
|
+
# reported, with its session id; standard error when nil
|
|
28
|
+
def initialize(client, name: nil, treat_as_read: nil, preview_timeout: Engine::DEFAULT_TIMEOUT, hello: nil, logger: nil)
|
|
29
|
+
super()
|
|
30
|
+
unless client.is_a?(::Mongo::Client)
|
|
31
|
+
raise ArgumentError, "Sendproof::Mongo::Store takes a Mongo::Client from the mongo driver."
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
@name = Stores::Guard.store_label(name)
|
|
35
|
+
reads = Stores::Guard.read_set(treat_as_read, &:downcase)
|
|
36
|
+
@engine = Engine.new(client, reads: reads, preview_timeout: preview_timeout, hello: hello, logger: logger)
|
|
37
|
+
@client = Guarded::Handle.new(client, @engine)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def system = "mongodb"
|
|
41
|
+
|
|
42
|
+
def describe
|
|
43
|
+
{ system: "mongodb", name: @name, **@engine.detect }
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def begin_preview(_context, recorder) = @engine.begin_preview(recorder)
|
|
47
|
+
|
|
48
|
+
def finish_preview(recorder) = @engine.finish_preview(recorder)
|
|
49
|
+
|
|
50
|
+
# Turns the hook off: nothing is guarded or logged afterwards.
|
|
51
|
+
def unhook! = @engine.unhook!
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Sendproof
|
|
4
|
+
module Mongo
|
|
5
|
+
# The command-monitoring subscriber the {Engine} registers on its client. The Ruby driver calls
|
|
6
|
+
# +started+ on the calling thread before it writes the command to the socket, and an exception raised
|
|
7
|
+
# from it propagates out of the driver call, so the engine can refuse a command before any byte of it
|
|
8
|
+
# is sent. That is behavior of the driver, not a documented hook: the Preview tests prove it with an
|
|
9
|
+
# independent read, and the engine falls back to refusing the Preview's confirmation if a later driver
|
|
10
|
+
# stops propagating it.
|
|
11
|
+
class Subscriber
|
|
12
|
+
def initialize(engine)
|
|
13
|
+
@engine = engine
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def started(event) = @engine.command_started(event)
|
|
17
|
+
def succeeded(event) = @engine.command_succeeded(event)
|
|
18
|
+
def failed(event) = @engine.command_failed(event)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "mongo"
|
|
4
|
+
require "sendproof"
|
|
5
|
+
|
|
6
|
+
module Sendproof
|
|
7
|
+
# Sendproof Preview and write guard for MongoDB (spec/data-stores.md "MongoDB in Preview") over the
|
|
8
|
+
# +mongo+ Ruby driver.
|
|
9
|
+
#
|
|
10
|
+
# On a replica set or sharded cluster a Preview runs every command in one transaction that is aborted
|
|
11
|
+
# after the handler, and reports document changes. On a standalone server MongoDB can't run
|
|
12
|
+
# transactions, so the store runs in +guard+ mode like Redis: reads run and the first write blocks the
|
|
13
|
+
# Preview.
|
|
14
|
+
#
|
|
15
|
+
# - Mongoid 9: {Sendproof::Mongoid::Store}, which makes the Preview session ambient for Mongoid.
|
|
16
|
+
# - The plain driver: {Sendproof::Mongo::Store}, whose +client+ is a guarded client with the driver's API.
|
|
17
|
+
#
|
|
18
|
+
# Require it explicitly: +require "sendproof/mongo"+ (or +"sendproof/mongoid"+).
|
|
19
|
+
#
|
|
20
|
+
# *Coverage.* Only hooked clients are covered. Command monitoring backs both up and refuses, before
|
|
21
|
+
# the command is sent, any write during a Preview that doesn't carry the Preview's session and
|
|
22
|
+
# transaction, so a rollback is never claimed for a write that escaped.
|
|
23
|
+
module Mongo
|
|
24
|
+
autoload :Engine, "sendproof/mongo/engine"
|
|
25
|
+
autoload :Guarded, "sendproof/mongo/guarded"
|
|
26
|
+
autoload :Plain, "sendproof/mongo/plain"
|
|
27
|
+
autoload :Store, "sendproof/mongo/store"
|
|
28
|
+
autoload :Subscriber, "sendproof/mongo/subscriber"
|
|
29
|
+
|
|
30
|
+
# The reason a standalone server, or any reply the binding doesn't recognize, runs in +guard+ mode.
|
|
31
|
+
STANDALONE_REASON = "Preview unavailable: this MongoDB server doesn't support transactions"
|
|
32
|
+
|
|
33
|
+
# The reason when the detection +hello+ couldn't be sent or answered: whether the server supports
|
|
34
|
+
# transactions is unknown, so the store runs in +guard+ mode for now and asks again next time.
|
|
35
|
+
UNREACHABLE_REASON = "Preview unavailable: couldn't reach MongoDB to check whether it supports transactions, " \
|
|
36
|
+
"so this Preview runs in guard mode"
|
|
37
|
+
|
|
38
|
+
# The reason for a command MongoDB refuses inside a transaction.
|
|
39
|
+
def self.refused_reason(operation) = "Preview unavailable: MongoDB can't run #{operation} inside a transaction"
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Sendproof
|
|
4
|
+
module Mongoid
|
|
5
|
+
# The MongoDB data store for Mongoid. See {Sendproof::Mongo::Store} for the plain driver.
|
|
6
|
+
#
|
|
7
|
+
# On a replica set or sharded cluster a Preview runs every Mongoid operation in one transaction that is
|
|
8
|
+
# aborted afterwards, and reports document changes. On a standalone server the store runs in +guard+
|
|
9
|
+
# mode: reads run and the first write blocks the Preview.
|
|
10
|
+
#
|
|
11
|
+
# Mongoid operations that bypass the model layer, such as +Model.collection.insert_one+, carry no
|
|
12
|
+
# session, so the engine refuses them and the Preview answers 500 rather than claim a rollback.
|
|
13
|
+
class Store < Sendproof::DataStore
|
|
14
|
+
# @return [Sendproof::Mongo::Engine]
|
|
15
|
+
attr_reader :engine
|
|
16
|
+
|
|
17
|
+
# @param client [Symbol, String] the Mongoid client name to hook (+:default+)
|
|
18
|
+
# @param name [String, nil] display label for discovery, such as +catalog+; never a connection string or host
|
|
19
|
+
# @param treat_as_read [Array<String>, nil] commands you state never write, by exact name
|
|
20
|
+
# @param preview_timeout [Numeric] seconds a Preview may hold its transaction (default 50)
|
|
21
|
+
# @param hello [#call, nil] test seam: returns the +hello+ document instead of asking the server
|
|
22
|
+
# @param logger [#warn, nil] where a Preview transaction the store couldn't end after a lost abort is
|
|
23
|
+
# reported, with its session id; standard error when nil
|
|
24
|
+
def initialize(client: :default, name: nil, treat_as_read: nil, preview_timeout: Sendproof::Mongo::Engine::DEFAULT_TIMEOUT, hello: nil,
|
|
25
|
+
logger: nil)
|
|
26
|
+
super()
|
|
27
|
+
@name = Stores::Guard.store_label(name)
|
|
28
|
+
reads = Stores::Guard.read_set(treat_as_read, &:downcase)
|
|
29
|
+
@client_name = client
|
|
30
|
+
@engine = Sendproof::Mongo::Engine.new(::Mongoid::Clients.with_name(client), reads: reads, preview_timeout: preview_timeout,
|
|
31
|
+
hello: hello, logger: logger)
|
|
32
|
+
Sendproof::Mongoid.install!
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def system = "mongodb"
|
|
36
|
+
|
|
37
|
+
def describe
|
|
38
|
+
{ system: "mongodb", name: @name, **@engine.detect }
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Refuses to start when Mongoid now uses a client the store never hooked (one it built after
|
|
42
|
+
# +Mongoid::Clients.clear+): that client's writes would be neither guarded nor rolled back.
|
|
43
|
+
def begin_preview(_context, recorder)
|
|
44
|
+
if @engine.active? && !@engine.hooked?(::Mongoid::Clients.with_name(@client_name))
|
|
45
|
+
raise ConfigurationError, "Mongoid rebuilt its #{@client_name.inspect} client after Sendproof hooked it; " \
|
|
46
|
+
"create the Sendproof::Mongoid::Store after Mongoid's clients are configured."
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
@engine.begin_preview(recorder)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def finish_preview(recorder) = @engine.finish_preview(recorder)
|
|
53
|
+
|
|
54
|
+
# Turns the hook off: nothing is guarded or logged afterwards.
|
|
55
|
+
def unhook! = @engine.unhook!
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "mongoid"
|
|
4
|
+
require "sendproof/mongo"
|
|
5
|
+
|
|
6
|
+
module Sendproof
|
|
7
|
+
# Sendproof Preview and write guard for Mongoid 9 (spec/data-stores.md "MongoDB in Preview").
|
|
8
|
+
#
|
|
9
|
+
# config.sendproof.data_store Sendproof::Mongoid::Store.new(name: "catalog")
|
|
10
|
+
#
|
|
11
|
+
# A Preview makes its transaction session ambient for Mongoid, so models, queries and +save+ run in it
|
|
12
|
+
# without any change to the application. Require it explicitly: +require "sendproof/mongoid"+.
|
|
13
|
+
module Mongoid
|
|
14
|
+
autoload :Store, "sendproof/mongoid/store"
|
|
15
|
+
|
|
16
|
+
# Makes the Preview session the ambient Mongoid session for every client of a hooked cluster. Mongoid
|
|
17
|
+
# asks +Threaded.get_session+ before each operation, so a model operation, including one on a client
|
|
18
|
+
# derived with +with+ or +use+, carries the Preview's session.
|
|
19
|
+
module AmbientSession
|
|
20
|
+
def get_session(client: nil)
|
|
21
|
+
super || Sendproof::Mongoid.preview_session(client)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Makes +Model.with_session+ and +Model.transaction+ join the Preview transaction instead of
|
|
26
|
+
# starting a nested one (Mongoid refuses nesting). A +Mongoid::Errors::Rollback+ ends the block, but the
|
|
27
|
+
# writes made so far stay in the Preview transaction, which is rolled back at the end anyway.
|
|
28
|
+
module Join
|
|
29
|
+
def with_session(options = {}, &block)
|
|
30
|
+
session = Sendproof::Mongoid.preview_session(persistence_context.client)
|
|
31
|
+
session ? block.call(session) : super
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def transaction(options = {}, session_options: {}, &block)
|
|
35
|
+
return super unless Sendproof::Mongoid.preview_session(persistence_context.client)
|
|
36
|
+
|
|
37
|
+
begin
|
|
38
|
+
block.call
|
|
39
|
+
rescue ::Mongoid::Errors::Rollback
|
|
40
|
+
nil
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
class << self
|
|
46
|
+
# @return [Mongo::Session, nil] the Preview session for +client+, when this thread runs a Preview
|
|
47
|
+
def preview_session(client)
|
|
48
|
+
Sendproof::Mongo::Engine.registry.each do |engine|
|
|
49
|
+
session = engine.preview_session_for(client)
|
|
50
|
+
return session if session
|
|
51
|
+
end
|
|
52
|
+
nil
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Adds the two patches above to Mongoid. Idempotent.
|
|
56
|
+
def install!
|
|
57
|
+
return if @installed
|
|
58
|
+
|
|
59
|
+
@installed = true
|
|
60
|
+
::Mongoid::Threaded.singleton_class.prepend(AmbientSession)
|
|
61
|
+
::Mongoid::Clients::Sessions::ClassMethods.prepend(Join)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Sendproof
|
|
4
|
+
# State of one observed send (+Prefer: trace+): an ordinary request that also records a trace.
|
|
5
|
+
#
|
|
6
|
+
# Capture starts only at the endpoint handler, after the host authenticated and authorized the
|
|
7
|
+
# request, and only for an authenticated subject admitted to the workbench.
|
|
8
|
+
class ObservedRequest
|
|
9
|
+
attr_reader :clock
|
|
10
|
+
|
|
11
|
+
def initialize(config:, started: Clock.now)
|
|
12
|
+
@config = config
|
|
13
|
+
@clock = Clock::Request.new(started)
|
|
14
|
+
@started = started
|
|
15
|
+
@invoked = false
|
|
16
|
+
@completed = false
|
|
17
|
+
@reservation = nil
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Wraps the endpoint handler. See {Handler.around}.
|
|
21
|
+
def around_handler(request:, eligible:, &block)
|
|
22
|
+
return yield if @invoked || !eligible
|
|
23
|
+
|
|
24
|
+
@invoked = true
|
|
25
|
+
owner = owner_for(request)
|
|
26
|
+
observation = owner && begin_observation(request)
|
|
27
|
+
return yield unless observation
|
|
28
|
+
|
|
29
|
+
@owner = owner
|
|
30
|
+
@observation = observation
|
|
31
|
+
@request = request
|
|
32
|
+
@recorder = Recorder.new(rollback: false, started: @started, include_sql_values: @config.sql_parameter_values?)
|
|
33
|
+
@reservation = @config.trace_store.reserve(owner)
|
|
34
|
+
@clock.start_handler
|
|
35
|
+
result = Capture.with(@recorder, &block)
|
|
36
|
+
@completed = true
|
|
37
|
+
result
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Completes the trace after the response is produced and adds the trace headers.
|
|
41
|
+
#
|
|
42
|
+
# @param headers [Hash, nil] Rack response headers to extend (nil when the app raised)
|
|
43
|
+
# @param failed [Boolean] whether the application raised
|
|
44
|
+
def finish(headers, failed: false)
|
|
45
|
+
return unless @reservation
|
|
46
|
+
|
|
47
|
+
@clock.end_handler
|
|
48
|
+
completed = false
|
|
49
|
+
begin
|
|
50
|
+
completed = !failed && @completed && owner_for(@request) == @owner
|
|
51
|
+
capture = Capture.with(@recorder) do
|
|
52
|
+
@observation.finalize_capture(@recorder) if @observation.respond_to?(:finalize_capture)
|
|
53
|
+
@recorder.finish
|
|
54
|
+
end
|
|
55
|
+
if completed
|
|
56
|
+
started = Clock.now
|
|
57
|
+
changes, truncated = Limits.snapshot_changes(@observation.changes + capture.changes)
|
|
58
|
+
timing = @recorder.timing(@clock, Clock.elapsed_ms(started))
|
|
59
|
+
trace = { changes:, effects: capture.effects, logs: capture.logs }
|
|
60
|
+
trace[:changesTruncated] = true if truncated || @observation.changes_truncated? || capture.changes_truncated
|
|
61
|
+
trace[:effectsTruncated] = true if capture.effects_truncated
|
|
62
|
+
trace[:logsTruncated] = true if capture.logs_truncated
|
|
63
|
+
trace[:timing] = timing
|
|
64
|
+
@reservation.complete(trace)
|
|
65
|
+
decorate(headers, timing) if headers
|
|
66
|
+
end
|
|
67
|
+
rescue StandardError => error
|
|
68
|
+
completed = false
|
|
69
|
+
warn_about("Sendproof could not collect the observed send", error)
|
|
70
|
+
ensure
|
|
71
|
+
@config.trace_store.discard(@reservation) unless completed
|
|
72
|
+
release
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
private
|
|
77
|
+
|
|
78
|
+
def decorate(headers, timing)
|
|
79
|
+
Protocol.set_header(headers, Protocol::TRACE_HEADER, @reservation.id)
|
|
80
|
+
Protocol.set_header(headers, "cache-control", "no-store")
|
|
81
|
+
Protocol.append_header(headers, "server-timing", Clock.server_timing(timing))
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def owner_for(request)
|
|
85
|
+
@config.identity_for(request)&.owner(request.script_name)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def begin_observation(request)
|
|
89
|
+
return nil unless @config.authorized?(request)
|
|
90
|
+
|
|
91
|
+
provider = @config.preview_provider or return nil
|
|
92
|
+
provider.begin_observation(ProviderContext.new(env: request.env, request:, config: @config))
|
|
93
|
+
rescue StandardError => error
|
|
94
|
+
warn_about("Sendproof could not start observing the send", error)
|
|
95
|
+
nil
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def release
|
|
99
|
+
@observation&.close
|
|
100
|
+
rescue StandardError => error
|
|
101
|
+
warn_about("Sendproof could not release the send observation", error)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def warn_about(message, error)
|
|
105
|
+
@config.logger&.warn("#{message}: #{error.class}: #{error.message}")
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Sendproof
|
|
4
|
+
module OpenSearch
|
|
5
|
+
# The OpenSearch data store: a Faraday middleware on every connection of +OpenSearch::Transport+,
|
|
6
|
+
# the HTTP layer of the +opensearch-ruby+ gem. See {Sendproof::Search::Store} for the behavior,
|
|
7
|
+
# coverage and options. The store id is +opensearch+.
|
|
8
|
+
#
|
|
9
|
+
# @example
|
|
10
|
+
# config.sendproof.data_store Sendproof::OpenSearch::Store.new(name: "search")
|
|
11
|
+
class Store < Sendproof::Search::Store
|
|
12
|
+
def system = "opensearch"
|
|
13
|
+
|
|
14
|
+
# @return [Class] the transport client class whose instances this store guards
|
|
15
|
+
def client_class = ::OpenSearch::Transport::Client
|
|
16
|
+
|
|
17
|
+
# @return [Class] the HTTP transport class that builds the Faraday connections
|
|
18
|
+
def transport_class = ::OpenSearch::Transport::Transport::HTTP::Faraday
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "opensearch"
|
|
4
|
+
require "sendproof/search"
|
|
5
|
+
|
|
6
|
+
module Sendproof
|
|
7
|
+
# Sendproof write guard for OpenSearch (spec/data-stores.md, +guard+ mode), for the +opensearch-ruby+
|
|
8
|
+
# 3 gem and everything built on its client.
|
|
9
|
+
#
|
|
10
|
+
# store = Sendproof::OpenSearch::Store.new(name: "search")
|
|
11
|
+
# config.sendproof.data_store store
|
|
12
|
+
#
|
|
13
|
+
# Require it explicitly: +require "sendproof/opensearch"+.
|
|
14
|
+
module OpenSearch
|
|
15
|
+
autoload :Store, "sendproof/opensearch/store"
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Sendproof
|
|
4
|
+
# Matches a request to an OpenAPI-style operation such as +POST /items/{id}+.
|
|
5
|
+
class OperationMatcher
|
|
6
|
+
Operation = Struct.new(:method, :path, :pattern)
|
|
7
|
+
|
|
8
|
+
def initialize(operations)
|
|
9
|
+
@operations = operations.map do |method, path|
|
|
10
|
+
Operation.new(method.to_s.upcase, path, self.class.pattern(path))
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# @return [Operation, nil]
|
|
15
|
+
def resolve(method, path)
|
|
16
|
+
method = method.to_s.upcase
|
|
17
|
+
@operations.find { |operation| operation.method == method && operation.pattern.match?(path) }
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Converts +/items/{id}+ into an anchored, case-insensitive pattern.
|
|
21
|
+
def self.pattern(template)
|
|
22
|
+
parts = template.split(/(\{[^}\/]+\})/).map do |part|
|
|
23
|
+
part.start_with?("{") ? "[^/]+" : Regexp.escape(part)
|
|
24
|
+
end
|
|
25
|
+
Regexp.new("\\A#{parts.join}/?\\z", Regexp::IGNORECASE)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Sendproof
|
|
4
|
+
# Makes a Preview's inherited mark (see {Capture.inherited_preview}) follow the task, not the pool thread.
|
|
5
|
+
#
|
|
6
|
+
# A thread inherits the fiber storage of the thread that creates it, so a pool worker that happens to be
|
|
7
|
+
# created while a Preview's handler posts to the pool would carry that Preview for its whole life and have
|
|
8
|
+
# every later write refused. Each hook below runs every task with the inherited mark cleared, so a task
|
|
9
|
+
# posted during a Preview counts as unattributed (refused while a Preview is open) and later tasks run
|
|
10
|
+
# normally (spec/data-stores.md, "Writes the binding can't attribute to a request").
|
|
11
|
+
#
|
|
12
|
+
# Hooked: concurrent-ruby's thread pools (+ThreadPoolExecutor+, +CachedThreadPool+, +FixedThreadPool+,
|
|
13
|
+
# +SingleThreadExecutor+ and the global executors behind +Concurrent::Promises+, +Future+, +TimerSet+ and
|
|
14
|
+
# +ScheduledTask+), its +SimpleExecutorService+, and work wrapped in the Rails executor
|
|
15
|
+
# (+ActiveSupport::Executor.wrap+), which covers ActiveJob's +:async+ adapter, job execution and Action Cable.
|
|
16
|
+
# {install} runs as each Preview starts, so a library loaded later is hooked before its first Preview.
|
|
17
|
+
module PoolHooks
|
|
18
|
+
# Runs a task with the inherited Preview mark cleared.
|
|
19
|
+
module ClearedTask
|
|
20
|
+
def run_task(...)
|
|
21
|
+
Sendproof::Capture.without_inherited { super }
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# +SimpleExecutorService+ starts a new thread per task, on the posting thread.
|
|
26
|
+
module ClearedPost
|
|
27
|
+
def post(*args, &task)
|
|
28
|
+
return super unless task
|
|
29
|
+
|
|
30
|
+
super(*args) { |*inner| Sendproof::Capture.without_inherited { task.call(*inner) } }
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# The outermost +ActiveSupport::ExecutionWrapper.wrap+ starts a unit of work of its own.
|
|
35
|
+
module ClearedWrap
|
|
36
|
+
def wrap(...)
|
|
37
|
+
return super if active?
|
|
38
|
+
|
|
39
|
+
Sendproof::Capture.without_inherited { super }
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
@lock = Mutex.new
|
|
44
|
+
@installed = {}
|
|
45
|
+
|
|
46
|
+
class << self
|
|
47
|
+
# Prepends each hook whose library is loaded, once.
|
|
48
|
+
def install
|
|
49
|
+
return if @installed.length == 3
|
|
50
|
+
|
|
51
|
+
@lock.synchronize do
|
|
52
|
+
hook(:pool) { pool_worker_class&.prepend(ClearedTask) }
|
|
53
|
+
hook(:simple) do
|
|
54
|
+
if defined?(::Concurrent::SimpleExecutorService)
|
|
55
|
+
::Concurrent::SimpleExecutorService.prepend(ClearedPost)
|
|
56
|
+
::Concurrent::SimpleExecutorService.singleton_class.prepend(ClearedPost)
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
hook(:rails) { ::ActiveSupport::ExecutionWrapper.singleton_class.prepend(ClearedWrap) if defined?(::ActiveSupport::ExecutionWrapper) }
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
private
|
|
64
|
+
|
|
65
|
+
def hook(name)
|
|
66
|
+
@installed[name] = true if !@installed[name] && yield
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# The private worker class of concurrent-ruby's Ruby thread pool, the one every MRI pool uses.
|
|
70
|
+
def pool_worker_class
|
|
71
|
+
return nil unless defined?(::Concurrent::RubyThreadPoolExecutor)
|
|
72
|
+
|
|
73
|
+
worker = ::Concurrent::RubyThreadPoolExecutor.const_get(:Worker, false)
|
|
74
|
+
worker if worker.private_method_defined?(:run_task) || worker.method_defined?(:run_task)
|
|
75
|
+
rescue NameError
|
|
76
|
+
nil
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|