Skip to content

perf: stop TLS retry storm, stream-parse JSON with a filter - #94

Merged
MatixYo merged 1 commit into
MatixYo:mainfrom
dreamiurg:perf/tls-timeout-and-streaming-json
Sep 24, 2026
Merged

MatixYo merged 1 commit into
MatixYo:mainfrom
dreamiurg:perf/tls-timeout-and-streaming-json

Conversation

@dreamiurg

Copy link
Copy Markdown
Contributor

Two problems in the ADS-B fetch path, both visible as UI stalls on an ESP32-C3.

TLS retry storm. The connect timeout was 200 ms — far too short for a TLS handshake to opendata.adsb.fi. performGetWithPoll therefore almost always failed and retried in a tight loop until the 10 s request deadline, opening a fresh handshake each time. Every poll cycle burned seconds of CPU and heap on handshakes that were killed just before they could finish. Now: 5000 ms connect timeout and a single attempt (kConnectAttempts = 1). If a connect stalls, the poll cycle gives up and the next one retries 3 s later, rather than blocking the display. The pollNetwork() call before the GET is kept.

Whole response buffered as a String. readResponseBodyWithPoll accumulated the entire JSON body into a heap String, which was then parsed into a JsonDocument — the payload existed twice in RAM, and a busy sector's response is large. Replaced with deserializeJson(doc, http.getStream(), DeserializationOption::Filter(filter)): ArduinoJson parses straight off the socket and the filter keeps only the fields actually rendered (lat, lon, headings, speeds, altitudes, flight, hex, t, category). No intermediate String, and the document holds a small fraction of the response.

Also: request timeout 10000 ms to 6000 ms, and the now-unused performGetWithPoll/readResponseBodyWithPoll are deleted. The two paths are consolidated into one httpGetJson helper.

User impact: the radar stops freezing for seconds at a time on flaky WiFi, and peak heap use during a fetch drops substantially — the difference between working and heap-exhausting when many aircraft are in range.

Tested: builds for env:supermini; ran on an ESP32-C3 Super Mini + GC9A01 (combined with other local changes).

🤖 Generated with Claude Code

https://claude.ai/code/session_017qjXYQiTkbgyVV7ZzvKzA3

The 200 ms connect timeout was too short for a TLS handshake, so every
poll retried handshakes in a loop until the 10 s deadline. Use a 5000 ms
connect timeout and a single attempt; a stalled connect is retried by the
next poll instead of blocking the UI.

Parse the response straight off the socket with a DeserializationOption
Filter that keeps only the rendered fields, instead of buffering the whole
body into a String and parsing that. Drop performGetWithPoll and
readResponseBodyWithPoll in favor of one httpGetJson helper.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017qjXYQiTkbgyVV7ZzvKzA3
@MatixYo
MatixYo merged commit 9d85778 into MatixYo:main Sep 24, 2026
2 checks passed
lmoiseichuk added a commit to lmoiseichuk/ESP32-Plane-Radar that referenced this pull request Oct 8, 2026
MatixYo#94 (e23b82c) stopped a TLS retry storm by replacing performGetWithPoll
with a single GET and a 5000 ms connect timeout. When main was merged
into MatixYo#90's branch (dc969f6, "Merge branch 'main' into
feature/adsb-pipeline"), the conflict was resolved by keeping
performGetWithPoll, and MatixYo#90 took it back to main (78958a5). It calls
GET() again every 5 ms until a 6000 ms deadline whenever the result is
CONNECTION_REFUSED or NOT_CONNECTED.

A TLS setup that cannot allocate its record buffers comes back that way.
mbedtls_ssl_setup fails with -32512 (SSL - Memory allocation failed),
WiFiClientSecure::connect returns 0, and HTTPClient reports a refused
connection. Nothing frees heap between attempts, so the same allocation
fails every 5 ms until the deadline. The storm fires whenever the TLS
setup cannot get its heap, or a connect is refused. On the 240x240 board
the heap is there and it stays latent; it was seen on a fork driving a
360x360 panel, whose larger frame buffer leaves less heap. There each
fetch repeated the failed allocation about 170 times, gave up after 6 s
with "adsb: HTTP -11", and logged about 900 allocation failures a
minute.

Go back to MatixYo#94's behaviour: one GET with the 5000 ms connect timeout. A
failed connect is retried by the next fetch, kAdsbFetchIntervalMs later.
The background task, streaming parse and chunked framing from MatixYo#90 are
unchanged.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DL3fMjXoiVk9VGMHmQkfyW
lmoiseichuk added a commit to lmoiseichuk/ESP32-Plane-Radar that referenced this pull request Oct 8, 2026
MatixYo#94 (e23b82c) stopped a TLS retry storm by replacing performGetWithPoll
with a single GET and a 5000 ms connect timeout. When main was merged
into MatixYo#90's branch (dc969f6, "Merge branch 'main' into
feature/adsb-pipeline"), the conflict was resolved by keeping
performGetWithPoll, and MatixYo#90 took it back to main (78958a5). It calls
GET() again every 5 ms until a 6000 ms deadline whenever the result is
CONNECTION_REFUSED or NOT_CONNECTED.

A TLS setup that cannot allocate its record buffers comes back that way.
mbedtls_ssl_setup fails with -32512 (SSL - Memory allocation failed),
WiFiClientSecure::connect returns 0, and HTTPClient reports a refused
connection. Nothing frees heap between attempts, so the same allocation
fails every 5 ms until the deadline. The storm fires whenever the TLS
setup cannot get its heap, or a connect is refused. On the 240x240 board
the heap is there and it stays latent; it was seen on a fork driving a
360x360 panel, whose larger frame buffer leaves less heap. There each
fetch repeated the failed allocation about 170 times, gave up after 6 s
with "adsb: HTTP -11", and logged about 900 allocation failures a
minute.

Go back to MatixYo#94's behaviour: one GET with the 5000 ms connect timeout. A
failed connect is retried by the next fetch, kAdsbFetchIntervalMs later.
The background task, streaming parse and chunked framing from MatixYo#90 are
unchanged.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DL3fMjXoiVk9VGMHmQkfyW
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants