| @@ -224,6 +224,11 @@ pub unsafe extern "C" fn tl_net_accept(ctx: *mut Ctx, listener: Value) -> Value |
| 224 | 224 | let seen = io_events(ctx); |
| 225 | 225 | match l.accept() { |
| 226 | 226 | Ok((stream, _)) => { |
| 227 | + // A server answers a request with one write, and a |
| 228 | + // streamed response flushes a write per chunk (ADR |
| 229 | + // 0053): Nagle would hold each back for a delayed ACK |
| 230 | + // (40 ms stalls). A failure keeps the connection. |
| 231 | + let _ = stream.set_nodelay(true); |
| 227 | 232 | return match wrap(ctx, Socket::Stream(Arc::new(stream))) { |
| 228 | 233 | Ok(r) => ok(ctx, r), |
| 229 | 234 | Err(e) => error(ctx, &e), |
| @@ -279,6 +284,9 @@ pub unsafe extern "C" fn tl_net_connect(ctx: *mut Ctx, host: Value, port: Value) |
| 279 | 284 | /// `ctx` must be the running actor's context. |
| 280 | 285 | unsafe fn connect_to(ctx: *mut Ctx, addr: SocketAddr) -> Result<Value, Option<String>> { |
| 281 | 286 | let stream = TcpStream::connect(addr).map_err(|e| Some(e.to_string()))?; |
| 287 | + // As on accept: clients write a request and wait, so Nagle only adds |
| 288 | + // a delayed-ACK stall. A failure keeps the connection. |
| 289 | + let _ = stream.set_nodelay(true); |
| 282 | 290 | // SAFETY: forwarded caller guarantee. |
| 283 | 291 | unsafe { |
| 284 | 292 | let r = wrap(ctx, Socket::Stream(Arc::new(stream))).map_err(Some)?; |
| @@ -428,3 +436,50 @@ pub(crate) unsafe fn upgrade(conn: Value) -> Result<(Arc<TcpStream>, Token), Str |
| 428 | 436 | } |
| 429 | 437 | Ok((s.clone(), h.token)) |
| 430 | 438 | } |
| 439 | + |
| 440 | +#[cfg(test)] |
| 441 | +mod tests { |
| 442 | + use super::*; |
| 443 | + use crate::abi::{Block, UNIT, int}; |
| 444 | + use crate::values::string; |
| 445 | + |
| 446 | + /// The one field of the `Ok` cell an extern returned. |
| 447 | + unsafe fn ok0(v: Value) -> Value { |
| 448 | + // SAFETY: the test reads the cell the extern just made. |
| 449 | + unsafe { *Block::fields_ptr(v as *mut Block) } |
| 450 | + } |
| 451 | + |
| 452 | + /// Accepted and connected sockets both have Nagle off: a response is |
| 453 | + /// one flushed write per chunk (ADR 0053), which Nagle would hold |
| 454 | + /// back for a delayed ACK. |
| 455 | + unsafe extern "C" fn accepted_sockets_disable_nagle(ctx: *mut Ctx) -> Value { |
| 456 | + // SAFETY: the test follows the generated-code convention; every |
| 457 | + // value it makes is dropped below. Loopback: the connect is |
| 458 | + // established at once, so the accept never parks. |
| 459 | + unsafe { |
| 460 | + let host = string(ctx, "127.0.0.1"); |
| 461 | + let listener = tl_net_listen(ctx, host, int(0)); |
| 462 | + let port = tl_net_port(ctx, ok0(listener)); |
| 463 | + let conn = tl_net_connect(ctx, host, port); |
| 464 | + let accepted = tl_net_accept(ctx, ok0(listener)); |
| 465 | + for end in [ok0(conn), ok0(accepted)] { |
| 466 | + let Socket::Stream(s) = &handle(end).socket else { |
| 467 | + panic!("a connection"); |
| 468 | + }; |
| 469 | + assert!(s.nodelay().expect("nodelay is readable")); |
| 470 | + } |
| 471 | + for v in [host, listener, conn, accepted] { |
| 472 | + drop_value(ctx, v); |
| 473 | + } |
| 474 | + } |
| 475 | + assert_eq!(crate::heap::stats::tl_stats_live_blocks(), 0); |
| 476 | + UNIT |
| 477 | + } |
| 478 | + |
| 479 | + #[test] |
| 480 | + fn sockets_disable_nagle() { |
| 481 | + // SAFETY: the test follows the generated-code convention. |
| 482 | + let code = unsafe { crate::sched::run_test_actor(accepted_sockets_disable_nagle) }; |
| 483 | + assert_eq!(code, 0); |
| 484 | + } |
| 485 | +} |