-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_runtime_power.exs
More file actions
235 lines (196 loc) · 6.25 KB
/
async_runtime_power.exs
File metadata and controls
235 lines (196 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
defmodule AsyncRuntimePowerExample do
@moduledoc """
Demonstrates invisible async context propagation across multipart parsing,
file streaming, compression tasks, logger metadata, console grouping, and
ambient abort signals.
Usage:
mix run examples/async_runtime_power.exs
"""
use Web
alias Web.AsyncContext
alias Web.File
def run do
user = AsyncContext.Variable.new("user")
controller = AbortController.new()
boundary = "runtime-power-boundary"
request_id = "req-" <> Integer.to_string(System.unique_integer([:positive]))
AsyncContext.Variable.run(user, "ada", fn ->
:logger.update_process_metadata(%{
request_id: request_id,
user: AsyncContext.Variable.get(user),
web_runtime_observe: true
})
Console.group("Secure Compressed Proxy")
try do
form = FormData.parse(upload_stream(boundary), boundary, signal: controller.signal)
parent = self()
snapshot = AsyncContext.Snapshot.take()
proxy_task =
Task.async(fn ->
AsyncContext.Snapshot.run(snapshot, fn ->
try do
Enum.each(form, fn
["meta", value] ->
Console.info("metadata field received: #{value}")
[_name, %File{} = file] ->
compression = CompressionStream.new("gzip")
send(parent, {:compression_pid, compression.readable.controller_pid})
file
|> File.stream()
|> ReadableStream.pipe_through(compression)
|> Enum.each(fn chunk ->
send(parent, {:compressed_chunk, byte_size(chunk)})
Process.sleep(10)
end)
end)
:ok
rescue
error in Web.TypeError ->
{:aborted, error.message}
catch
:exit, reason ->
{:exit, reason}
end
end)
end)
compression_pid =
receive do
{:compression_pid, pid} -> pid
after
1_000 -> raise "timed out waiting for compression stream startup"
end
receive do
{:compressed_chunk, bytes} ->
Console.debug("compressed chunk observed: #{bytes}B")
after
1_000 ->
raise "timed out waiting for the first compressed chunk"
end
Console.warn("Triggering abort controller kill switch")
:ok = AbortController.abort(controller, :operator_shutdown)
result =
case Task.yield(proxy_task, 2_000) || Task.shutdown(proxy_task, :brutal_kill) do
{:ok, value} -> value
nil -> :timed_out
end
wait_until(
fn ->
if Process.alive?(compression_pid) do
ReadableStream.__get_slots__(compression_pid).state in [:closed, :errored]
else
true
end
end,
500
)
compression_state =
if Process.alive?(compression_pid) do
ReadableStream.__get_slots__(compression_pid).state
else
:terminated
end
Console.info("proxy task result: #{inspect(result)}")
Console.info("form coordinator alive?: #{inspect(Process.alive?(form.coordinator))}")
Console.info("compression terminal state: #{inspect(compression_state)}")
receive_drain()
after
Console.group_end()
:logger.set_process_metadata(%{})
end
end)
end
defp upload_stream(boundary) do
payload =
multipart_payload(boundary, [
%{name: "meta", value: "confidential"},
%{
name: "upload",
filename: "secret.txt",
content_type: "text/plain",
value: String.duplicate("classified-", 2_000)
}
])
chunks = chunk_binary(payload, 256)
{:ok, cursor} = Agent.start_link(fn -> chunks end)
ReadableStream.new(%{
pull: fn controller ->
Process.sleep(15)
Agent.get_and_update(cursor, fn
[chunk | rest] ->
{chunk, rest}
[] ->
{:done, []}
end)
|> case do
:done ->
ReadableStreamDefaultController.close(controller)
chunk ->
ReadableStreamDefaultController.enqueue(controller, chunk)
end
end,
cancel: fn reason ->
Console.warn("upload source cancelled: #{inspect(reason)}")
if Process.alive?(cursor), do: Agent.stop(cursor, :normal)
end
})
end
defp multipart_payload(boundary, parts) do
Enum.map_join(parts, "", fn part ->
disposition = ~s(Content-Disposition: form-data; name="#{part.name}")
disposition =
case Map.get(part, :filename) do
nil -> disposition
filename -> disposition <> ~s(; filename="#{filename}")
end
headers = [disposition]
headers =
case Map.get(part, :content_type) do
nil -> headers
type -> headers ++ ["Content-Type: #{type}"]
end
[
"--#{boundary}\r\n",
Enum.join(headers, "\r\n"),
"\r\n\r\n",
part.value,
"\r\n"
]
|> IO.iodata_to_binary()
end) <> "--#{boundary}--\r\n"
end
defp chunk_binary(binary, size) do
do_chunk_binary(binary, size, [])
end
defp do_chunk_binary(<<>>, _size, acc), do: Enum.reverse(acc)
defp do_chunk_binary(binary, size, acc) do
take = min(size, byte_size(binary))
<<chunk::binary-size(take), rest::binary>> = binary
do_chunk_binary(rest, size, [chunk | acc])
end
defp receive_drain do
receive do
{:compressed_chunk, bytes} ->
Console.debug("compressed chunk observed: #{bytes}B")
receive_drain()
after
100 ->
:ok
end
end
defp wait_until(fun, timeout_ms) do
deadline = System.monotonic_time(:millisecond) + timeout_ms
do_wait_until(fun, deadline)
end
defp do_wait_until(fun, deadline) do
cond do
fun.() ->
:ok
System.monotonic_time(:millisecond) >= deadline ->
:timeout
true ->
Process.sleep(10)
do_wait_until(fun, deadline)
end
end
end
AsyncRuntimePowerExample.run()