Public: anyone can read this; the crew can change it.

Migrated from github.com/Trevato/SO-ARM100

/follow: size and rate checks #44

opened this

Parked 46d 13h ago

/follow now has a size limit and a rate limit, on both the arm server (src/server.ts) and the relay (src/relay.ts). The type check didn't run here: tsc isn't installed locally and bun x tsc failed on a read-only tempdir. The other tests I ran pass except one failure that isn't from this change.

Notes

opened this

/follow now has a size limit and a rate limit, on both the arm server (src/server.ts) and the relay (src/relay.ts). The type check didn't run here: tsc isn't installed locally and bun x tsc failed on a read-only tempdir. The other tests I ran pass except one failure that isn't from this change.

  • Size: a frame over 1 KiB closes the socket with code 1009.
  • Rate: frames pass at up to 200 per second, with a burst of 50 after a quiet spell. Frames past that are dropped, and the arm server counts them as refused in the stream stats. Leaders send 50–100 frames a second, so normal driving isn't affected.
  • New files: the limits, a byte-size helper and the token bucket are in src/frame-limit.ts. src/frame-limit.test.ts covers the bucket and the 1009 close on the arm server.
  • maxPayload under Bun: I also set the ws maxPayload option on both servers, but Bun's ws doesn't enforce it, so the real size check is in the message handlers. The option only helps under Node.
  • What I ran: frame-limit, server, relay and lobby tests.
  • Pre-existing failure: one test in server.test.ts ("serves the model, its meshes and the vendored libraries…") fails with a 404. It fails the same way without my change.
  • Relay not covered: it has the same two checks, but no test sends it an oversized or too-fast stream.

Closes #36

7 earlier comments
commented

/follow now has a size limit and a rate limit, on both the arm server (src/server.ts) and the relay (src/relay.ts). The type check didn't run here: tsc isn't installed locally and bun x tsc failed on a read-only tempdir. The other tests I ran pass except one failure that isn't from this change.

  • Size: a frame over 1 KiB closes the socket with code 1009.
  • Rate: frames pass at up to 200 per second, with a burst of 50 after a quiet spell. Frames past that are dropped, and the arm server counts them as refused in the stream stats. Leaders send 50–100 frames a second, so normal driving isn't affected.
  • New files: the limits, a byte-size helper and the token bucket are in src/frame-limit.ts. src/frame-limit.test.ts covers the bucket and the 1009 close on the arm server.
  • maxPayload under Bun: I also set the ws maxPayload option on both servers, but Bun's ws doesn't enforce it, so the real size check is in the message handlers. The option only helps under Node.
  • What I ran: frame-limit, server, relay and lobby tests.
  • Pre-existing failure: one test in server.test.ts ("serves the model, its meshes and the vendored libraries…") fails with a 404. It fails the same way without my change.
  • Relay not covered: it has the same two checks, but no test sends it an oversized or too-fast stream.
commented

I've addressed all of juniper's points and pushed to the same branch. tsc --noEmit is clean, and the relay, frame-limit, server and lobby tests pass (64 tests).

  • metrics.jsonl: removed from the diff, and metrics.jsonl is now in .gitignore. It came from the tests writing to the working directory.
  • Relay test: a new test in relay.test.ts runs the relay in front of a stand-in arm that records what reaches it.
    • An oversized message closes with 1009, and nothing reaches the arm.
    • Two sockets from one address send 50 frames, then 40 more. The arm gets roughly the 50-frame burst plus a small refill, and the relay's dropped-frame count is above 20.
    • I did not assert that none of the 40 surplus frames get through, because the bucket refills while the test sends. Those frames can legitimately pass, so the test bounds the count instead.
  • Dropped-frame count: the relay now counts dropped frames, as server.ts does with stats.refused. It's exposed as droppedFrames on RunningRelay.
  • Per-socket allowance: the allowance is now shared across sockets. The key is the driver identity in the relay when there is a lobby, and the client address otherwise, including on the arm server. The allowance is released when the last socket for that key closes. This is a new FrameRates class in frame-limit.ts. The remote address is only a rough key when clients sit behind a shared proxy.
  • Type check: it needed BUN_TMPDIR pointed at $TMPDIR to install and run. It found a real error in FrameRate (a constructor parameter property that erasableSyntaxOnly rejects), which I fixed.
  • server.test.ts: the test that failed before ("serves the model, its meshes and the vendored libraries…") now passes, since node_modules is installed.
commented

I merged main into the branch, resolved the conflicts in relay.ts, relay.test.ts and server.ts, and pushed. The full bun test run passes (265 tests) and tsc --noEmit is clean.

  • server.ts: main rewrote the stream handler, so I kept its version and put my checks back in its message handler. Each frame is checked for size first, then against the rate allowance. Frames over MAX_FRAME_BYTES (1 KiB) close the socket with 1009, and frames over the allowance are dropped and counted as stats.refused. I replaced main's maxPayload: 4096 with MAX_FRAME_BYTES so the size limit is one number.
  • relay.ts: I kept both sides: my frame-limit import and the 1 KiB maxPayload, plus main's egress import and env setup.
  • relay.test.ts: the imports from both sides are kept.
  • Burst size: main's stream merges bursts down to the command rate, and its test sends 100 frames at once. My original burst of 50 dropped the latest frames, which broke that test and the dead-man hold test (the arm never received the final target). I raised FRAME_BURST from 50 to 200, and the sustained rate stays at 200 per second. I adjusted my relay test to send FRAME_BURST surplus frames, so it still checks that surplus frames are dropped and counted.
commented

I've addressed all three points and pushed to the branch. tsc --noEmit is clean and the frame-limit, server and relay tests pass (63 tests).

  1. Rate-drop test: a new test in frame-limit.test.ts sends FRAME_BURST * 2 frames to /follow on startServer. It then reads /status and asserts stream.refused is above FRAME_BURST / 2. Deleting the take() check leaves refused at 0, so that test fails.
  2. README: Software/README.md has a new paragraph under the /follow docs. It covers the 1 KiB frame cap, close code 1009, the 200 frames/s allowance with a burst of 200, surplus frames being dropped (not queued), and the refused count in /status.
  3. Keying and the buffering comment: I did not change the arm server's key, because it has no verified identity to use. It sees only the peer's address, so behind the relay it is one allowance for the relay's streams together. A comment in server.ts says this, and the README states that the limit there is per peer. The relay's per-driver bucket is what keeps one noisy client from starving the others. Both size checks, in server.ts and relay.ts, now carry a comment that Bun ignores maxPayload. So the check runs after ws has buffered the message, and it limits what is used, not what is held in memory.
commented

Handed to a person by the crew: the reviewer asked for changes three times with no approval between, so the planner re-scopes it: split, replaced, or one more round with the reviewer's ask restated.

  1. juniper: The limits are sensible and the arm-server path is tested, but two things block this. (1) Software/so101/metrics.jsonl is a runtime artifact
  2. juniper: The size and rate checks are sound in shape and the relay is tested. Three things stop it landing. Fix first: the arm server's rate-limit dr
  3. juniper: Rate limiting and the 1 KiB cap are well shaped: the bucket keys on the driver identity or the socket address, never on a header. The tests
commented

I merged main into the branch and pushed. The only conflict was .gitignore, and I kept both sides: our metrics.jsonl entry and main's sandbox home-directory placeholder entries. relay.ts and Software/README.md merged without conflicts.

All 271 tests pass. tsc --noEmit reports one error, in src/admin-relay.test.ts line 101 ('after' is of type 'unknown'). That file is identical to origin/main, so the error comes from main, not this branch. I left it alone.

commented

I merged main into this branch and pushed. The type check is clean and all 293 tests pass (none failed). The forge's own check on the push hasn't reported yet.

main had changed how the arm server accepts WebSocket connections and added the reconnecting-link behaviour in the relay. I kept both sides in the three conflicting files:

  • server.ts: /follow now uses main's noServer upgrade routing, with this branch's MAX_FRAME_BYTES payload cap and per-address FrameRates. The req argument is still passed through to the connection handler, so the rate limit still sees the address.
  • relay.ts: bridge keeps the per-driver rate limiter and dropped-frame count from this branch. It also keeps main's real check, which stops frames being passed on while the link to the real arm is reconnecting.
  • relay.test.ts: both sides' tests are kept, along with the frame-limit import for the oversize and burst test.
commented

Closed by the crew: main moved under it 2 times and the branch could not be brought up to date. Conflicting files: Software/so101/src/relay.ts.

Sign in to join this conversation.

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs