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:

  1. 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.

  2. 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

@@ -73,6 +73,21 @@
7373 return (int)sp_net_term_flag;
7474 }
7575
76+/* Byte count written into the recv buffer by the most recent recv. The FFI
77+ * `:binstr` return mode reads this to build a binary-safe String of exactly
78+ * this many bytes (rather than strlen-truncating at the first NUL), so the same
79+ * recv functions serve both text (`:str`) and binary (`:binstr`) callers.
80+ * Single-threaded (the Fiber model is cooperative), so no locking is needed. */
81+int sp_net_bin_len = 0;
82+
83+/* Disable Nagle on a connection fd. TCP_NODELAY is a per-connection option, so
84+ * it must be set on each accepted fd, not just the listener -- otherwise small
85+ * writes (response headers, WebSocket frames) stall against delayed-ACK. */
86+void sp_net_set_nodelay(int fd) {
87+ int one = 1;
88+ setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
89+}
90+
7691 /* ---------- TCP socket lifecycle ---------- */
7792
7893 int sp_net_listen(int port, int reuseport) {
@@ -128,7 +143,7 @@
128143 }
129144 if (!(pfds[0].revents & POLLIN)) continue; /* woken by the self-pipe alone */
130145 fd = accept(sfd, (struct sockaddr *)&caddr, &clen);
131- if (fd >= 0) return fd;
146+ if (fd >= 0) { sp_net_set_nodelay(fd); return fd; }
132147 if (errno == EINTR) {
133148 if (sp_net_term_flag) return -1;
134149 continue; /* unrelated signal (SIGCHLD, ...) -- retry */
@@ -144,6 +159,7 @@
144159 do {
145160 fd = accept(sfd, (struct sockaddr *)&caddr, &clen);
146161 } while (fd < 0 && errno == EINTR);
162+ if (fd >= 0) sp_net_set_nodelay(fd);
147163 return fd;
148164 }
149165
@@ -226,15 +242,18 @@
226242 for (;;) {
227243 /* Cooperative shutdown: bail rather than retry into the signal. */
228244 if (sp_net_term_flag) {
245+ sp_net_bin_len = 0;
229246 sp_net_recv_buf[0] = '\0';
230247 return sp_net_recv_buf;
231248 }
232249 n = recv(fd, sp_net_recv_buf, (size_t)maxlen, 0);
233250 if (n >= 0) break;
234251 if (errno == EINTR) continue; /* e.g. SIGCHLD in a prefork server */
252+ sp_net_bin_len = 0;
235253 sp_net_recv_buf[0] = '\0';
236254 return sp_net_recv_buf;
237255 }
256+ sp_net_bin_len = (int)n; /* exact byte count for the :binstr path */
238257 sp_net_recv_buf[n] = '\0';
239258 return sp_net_recv_buf;
240259 }
@@ -253,6 +272,7 @@
253272 if (n == 0) break; /* clean EOF */
254273 total += (int)n;
255274 }
275+ sp_net_bin_len = total; /* exact byte count for the :binstr path */
256276 sp_net_recv_all_buf[total] = '\0';
257277 return sp_net_recv_all_buf;
258278 }
@@ -339,6 +359,7 @@
339359 if (n == 0) break;
340360 total += n;
341361 }
362+ sp_net_bin_len = (int)total; /* exact byte count for the :binstr path */
342363 sp_net_shell_buf[total] = '\0';
343364 pclose(fp);
344365 return sp_net_shell_buf;