Electric.Plug.ServeShapePlug (electric v1.6.2)

Copy Markdown View Source

Plug pipeline for serving shape requests.

call/2 is overridden to wrap the whole pipeline in a telemetry span, an explicit try/catch that invokes handle_errors/2 on uncaught errors, and a try/after that always releases the admission control permit and ends the OTEL span.

Error handling is inspired by Plug.ErrorHandler's pattern but adapts to a subtle limitation: Plug.Builder does not wrap individual plugs in error-capturing frames, so the conn visible from the catch clause is the one passed into call/2 — not the accumulated conn at the point of raise. This means we cannot rely on a register_before_send callback on the error-handler's conn for admission release. Instead, check_admission stashes the acquired permit in the process dictionary and the after clause in call/2 releases it — firing for success, halt, and exception paths alike.

Using after (rather than register_before_send) is also what makes the streaming path correct: before_send fires when send_chunked starts streaming, not when it finishes, which would end the telemetry span before chunk reduction completes and lose duration + streaming_bytes_sent. The after clause runs only once super(conn, opts) returns, i.e. after Api.Response.send_stream/2 has synchronously drained the body.

Summary

Functions

Callback implementation for Plug.init/1.

Functions

init(opts)

Callback implementation for Plug.init/1.