Commit 55f6d49d
55f6d49d53e5Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 8 files · +2 added · ~6 modified
Description
Set TCP_NODELAY on accepted fds and add an FFI :binstr return mode
sp_net was almost a turnkey server transport but two socket-layer gaps forced callers to keep a hand-written C shim:
-
TCP_NODELAY was only set on the listen fd. Nagle is a per-connection option, so accepted connections were left Nagle-enabled and small writes (response headers, WebSocket frames) stalled against delayed-ACK. sp_net_accept / sp_net_accept_nb now set TCP_NODELAY on the returned fd; sp_net_set_nodelay is also exposed for fds obtained elsewhere.
-
recv had no binary-safe counterpart to write_bytes: the FFI :str return lowers to sp_str_dup_external (strlen), truncating at the first embedded NUL -- fatal for binary protocols whose framing carries 0x00. A new FFI :binstr return mode builds the String from the exact byte count the callee publishes in sp_net_bin_len, so embedded NULs survive. sp_net_recv_some / recv_all / shell_capture publish their byte count, so a caller flips :str to :binstr with no other change.
Refs #1466
Details
- Commit
- 55f6d49d53e5
- Parent
- ef0dd7102c46
lib/sp_net.cmodifiedView file
return (int)sp_net_term_flag;}/* Byte count written into the recv buffer by the most recent recv. The FFI * `:binstr` return mode reads this to build a binary-safe String of exactly * this many bytes (rather than strlen-truncating at the first NUL), so the same * recv functions serve both text (`:str`) and binary (`:binstr`) callers. * Single-threaded (the Fiber model is cooperative), so no locking is needed. */int sp_net_bin_len = 0;/* Disable Nagle on a connection fd. TCP_NODELAY is a per-connection option, so * it must be set on each accepted fd, not just the listener -- otherwise small * writes (response headers, WebSocket frames) stall against delayed-ACK. */void sp_net_set_nodelay(int fd) { int one = 1; setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));}/* ---------- TCP socket lifecycle ---------- */int sp_net_listen(int port, int reuseport) { } if (!(pfds[0].revents & POLLIN)) continue; /* woken by the self-pipe alone */ fd = accept(sfd, (struct sockaddr *)&caddr, &clen); if (fd >= 0) return fd; if (fd >= 0) { sp_net_set_nodelay(fd); return fd; } if (errno == EINTR) { if (sp_net_term_flag) return -1; continue; /* unrelated signal (SIGCHLD, ...) -- retry */ do { fd = accept(sfd, (struct sockaddr *)&caddr, &clen); } while (fd < 0 && errno == EINTR); if (fd >= 0) sp_net_set_nodelay(fd); return fd;} for (;;) { /* Cooperative shutdown: bail rather than retry into the signal. */ if (sp_net_term_flag) { sp_net_bin_len = 0; sp_net_recv_buf[0] = '\0'; return sp_net_recv_buf; } n = recv(fd, sp_net_recv_buf, (size_t)maxlen, 0); if (n >= 0) break; if (errno == EINTR) continue; /* e.g. SIGCHLD in a prefork server */ sp_net_bin_len = 0; sp_net_recv_buf[0] = '\0'; return sp_net_recv_buf; } sp_net_bin_len = (int)n; /* exact byte count for the :binstr path */ sp_net_recv_buf[n] = '\0'; return sp_net_recv_buf;} if (n == 0) break; /* clean EOF */ total += (int)n; } sp_net_bin_len = total; /* exact byte count for the :binstr path */ sp_net_recv_all_buf[total] = '\0'; return sp_net_recv_all_buf;} if (n == 0) break; total += n; } sp_net_bin_len = (int)total; /* exact byte count for the :binstr path */ sp_net_shell_buf[total] = '\0'; pclose(fp); return sp_net_shell_buf;lib/sp_net.hmodifiedView file
Loading diff…
src/analyze_scope.cmodifiedView file
Loading diff…
src/codegen.cmodifiedView file
Loading diff…
src/codegen_call.cmodifiedView file
Loading diff…
src/codegen_util.cmodifiedView file
Loading diff…
test/ffi_binstr_recv.rbaddedView file
Loading diff…
test/ffi_binstr_recv.rb.expectedaddedView file
Loading diff…