Skip to content

Fast first clock sync and TCP keepalive - #10

Draft
rednayan wants to merge 3 commits into
fix/network-robustnessfrom
perf/quick-wins
Draft

rednayan wants to merge 3 commits into
fix/network-robustnessfrom
perf/quick-wins

Conversation

@rednayan

Copy link
Copy Markdown
Owner

Stacked on #9. The base is fix/network-robustness, so this diff shows only the three new commits. After #9 merges, change the base to main.

Summary

Two wire-compatible changes for 0.1.4. A liblsl peer sees no difference.

  1. Fast first clock sync. Before the first offset exists, the inlet sends its clock probes 4 ms apart (clock::FIRST_PROBE_INTERVAL). The burst stops when each probe has its answer. A burst that finds no offset tries again after 250 ms (clock::FIRST_RETRY). A reset or a new target clears the offset, so a recovery also gets a fast burst.
    • Measured on loopback: the first offset arrives in about 41 ms. Before, it took about 372 ms.
    • Each read in a probe slot now waits only for the time that is left in the slot. Before, a fixed 40 ms read timeout ended a 64 ms slot early. The regular bursts now use the configured probe interval.
  2. TCP keepalive on each data connection (src/keepalive.rs). Both ends send a probe after 2 s without traffic, then one each second. After 3 probes without an answer, the kernel ends the connection. On Linux, TCP_USER_TIMEOUT of 6 s also ends a connection whose sent data has no acknowledgement.
    • On Unix, a read that gives ETIMEDOUT now ends the connection, so a recovering inlet connects again. A read timeout gives EAGAIN there. On Windows, both still count as a quiet connection.
    • socket2 now uses its all feature, for the probe count and the user timeout.

Not in this PR

  • Encode once per push. Measured (release, 200 000 × 64-channel float32): the copy into each consumer queue costs about 1.9 µs per sample, and the encode about 0.6 µs. Encode once would save about 0.24% of one core for each extra consumer at 4 kHz × 64 channels. The larger cost is the copy. Sharing the samples between queues belongs with a flat typed sample API.

Deliberate differences from liblsl

  • liblsl sends every burst at the configured cadence. labstream sends the first burst (and the first after a recovery) with probes 4 ms apart. SPEC.md 3 gives the cadence as observed, not normative.
  • liblsl sets no keepalive. labstream ends a dead connection in about 5 s.

Tests

  • New: the_first_offset_arrives_quickly (tests/clock_live.rs, bound 250 ms), a_connected_stream_takes_the_short_timers (reads the options back from the socket), a_kernel_timeout_is_a_lost_connection_on_unix.
  • fmt check, clippy -D warnings, doc -D warnings and the full workspace tests pass. cargo check --target x86_64-pc-windows-gnu passes.
  • Conformance is the same as main: plain, --zero-stamps, --desc, --postproc 7 and selftest 28/28. --chunk 16 26/28 and --sync 24/28 (liblsl itself refuses the failing string cells). refuse100, pylsl_check 5/5 and examples pass.

Not verified

  • A half-open connection ends in about 5 s. This needs tc netem or two machines.
  • macOS build (no Apple target on the build machine) and MSRV 1.75.
  • CI: the account is locked for a billing problem, so no job runs at the moment.

🤖 Generated with Claude Code

Without an offset, the probe thread sends its probes 4 ms apart and
stops the burst when each probe has its answer. A burst that finds no
offset tries again after 250 ms. The first offset on this machine now
arrives in about 40 ms (it was about 370 ms).

Each read in a probe slot now waits only for the time that is left in
the slot. Before, a fixed 40 ms read timeout ended a 64 ms slot early.
The regular bursts now use the configured probe interval.
Both ends of a data connection now send a keepalive probe after 2 s
without traffic, then one each second. After 3 probes without an
answer, the kernel ends the connection. On Linux, sent data that has
no acknowledgement for 6 s also ends the connection.

A connection that died with no close (a pulled cable, a lost Wi-Fi
link) now ends in about 5 s. Before, an inlet waited for the 15 s
watchdog, and kept the dead socket when the outlet was still visible.

On Unix, a read that gives ETIMEDOUT now counts as a lost connection,
so the inlet connects again. A read timeout gives EAGAIN there. On
Windows, both still count as a quiet connection.

The socket2 dependency now uses its all feature, for the probe count
and the TCP user timeout.
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.

1 participant