Advanced

Streaming a large response

A response can be larger than memory. Streaming writes it in pieces, and the pieces make the error a shape that has no clean answer.

What you will see

client detects. The count is what turns a silent wrong answer into a caught one.

About 1 min of reading

Try It

End a stream in the middle

1

Write the pieces

printf 'piece\npiece\npiece\n' > /tmp/s.txt; wc -l < /tmp/s.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
3. Three pieces written, and the status was already sent before them.
2

Fail after the second piece

printf 'sent 2 total 5 failed yes\n' | awk '{print ($4=="yes") ? "partial" : "complete"}'

Before you run it, say out loud what it will print. Then open the answer.

What it prints
partial. A partial body with a 200 is the shape, and the shape is the problem.
3

Add the count both sides check

printf 'sent 2 total 5 failed yes check=total\n' | awk '{print ($2!=$3) ? "client detects" : "ok"}'

Before you run it, say out loud what it will print. Then open the answer.

What it prints
client detects. The count is what turns a silent wrong answer into a caught one.

Data Flow

A large result to pieces
1 The status is written first
↓
2 Pieces are produced and written
↓
3 A failure ends the stream
↓
4 The count is written when done
Why this works
Why the count is written last

The count is only known when the work is done. Writing it last is the only honest position for it.

The test for a stream

Interrupt the stream in the middle. The test is the middle, and the middle is where the shape lives.

Examples

The status is already sent

The first piece wrote a 200. An error after that cannot change the status, only end the body.

A partial body is a valid response

It is well formed and it is wrong. The only way to tell is a count that both sides can check.

The client decides

A client that stops reading is a client that disconnected. The disconnect is not an error, it is a request cancelled.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Middleware is a chain
Previous Lesson
Middleware is a chain
Next Lesson
Shutdown under load
Shutdown under load →