MCPcopy Create free account
hub / github.com/MaterializeInc/materialize / append_webhook

Function append_webhook

src/environmentd/src/http/webhook.rs:109–181  ·  view source on GitHub ↗

Append the provided `body` and `headers` to the webhook source identified via `database`, `schema`, and `name`.

(
    adapter_client: &mz_adapter::Client,
    webhook_cache: &WebhookAppenderCache,
    database: &str,
    schema: &str,
    name: &str,
    body: &Bytes,
    headers: &Arc<BTreeMap<String, String>>

Source from the content-addressed store, hash-verified

107/// Append the provided `body` and `headers` to the webhook source identified via `database`,
108/// `schema`, and `name`.
109async fn append_webhook(
110 adapter_client: &mz_adapter::Client,
111 webhook_cache: &WebhookAppenderCache,
112 database: &str,
113 schema: &str,
114 name: &str,
115 body: &Bytes,
116 headers: &Arc<BTreeMap<String, String>>,
117) -> Result<(), AppendWebhookError> {
118 // Shenanigans to get the types working for the async retry.
119 let (database, schema, name) = (database.to_string(), schema.to_string(), name.to_string());
120
121 // Record the time we receive the request, for use if validation checks the current timestamp.
122 let received_at = adapter_client.now();
123
124 // Get an appender for the provided object, if that object exists.
125 let AppendWebhookResponse {
126 tx,
127 body_format,
128 header_tys,
129 validator,
130 } = async {
131 let mut guard = webhook_cache.entries.lock().await;
132
133 // Remove the appender from our map, only re-insert it, if it's valid.
134 match guard.remove(&(database.clone(), schema.clone(), name.clone())) {
135 Some(appender) if !appender.tx.is_closed() => {
136 guard.insert((database, schema, name), appender.clone());
137 Ok::<_, AppendWebhookError>(appender)
138 }
139 // We don't have a valid appender, so we need to get one.
140 //
141 // Note: we hold the lock while we acquire and appender to prevent a dogpile.
142 _ => {
143 tracing::info!(?database, ?schema, ?name, "fetching webhook appender");
144 adapter_client.metrics().webhook_get_appender.inc();
145
146 // Acquire and cache a new appender.
147 let appender = adapter_client
148 .get_webhook_appender(database.clone(), schema.clone(), name.clone())
149 .await?;
150
151 guard.insert((database, schema, name), appender.clone());
152
153 Ok(appender)
154 }
155 }
156 }
157 .await?;
158
159 // These must happen before validation as we do not know if validation or
160 // packing will succeed and appending will begin
161 tx.increment_messages_received(1);
162 tx.increment_bytes_received(u64::cast_from(body.len()));
163
164 // If this source requires validation, then validate!
165 if let Some(validator) = validator {
166 let valid = validator

Callers 1

handle_webhookFunction · 0.85

Calls 15

cloneFunction · 0.85
pack_rowsFunction · 0.85
lockMethod · 0.80
is_closedMethod · 0.80
get_webhook_appenderMethod · 0.80
to_stringMethod · 0.45
nowMethod · 0.45
removeMethod · 0.45
cloneMethod · 0.45
insertMethod · 0.45

Tested by

no test coverage detected