Network read/write timeouts in Go vs Rust stdlib
stdlib IO timeout APIs and design philosophy.
Go uses SetReadDeadline/SetWriteDeadline as absolute times; Rust uses set_read_timeout/set_write_timeout as durations on TcpStream.
WHAT THIS TESTS Whether you know the precise standard-library timeout mechanisms in both languages and can articulate the contrasting design philosophies, absolute deadlines versus relative durations.
A GOOD ANSWER COVERS In Go, net.Conn provides SetDeadline, SetReadDeadline, and SetWriteDeadline, each taking an absolute time.Time. You typically call conn.SetReadDeadline(time.Now().Add(5*time.Second)) before a Read; if the deadline passes, the call returns an error whose Timeout method reports true. Passing the zero time disables the deadline. Deadlines are absolute so they compose naturally with an overall request budget. In Rust, std::net::TcpStream exposes set_read_timeout and set_write_timeout, each taking Option<Duration>; Some(dur) arms a relative timeout and None disables it. A timed-out read returns an Err with kind WouldBlock or TimedOut depending on platform. The philosophical contrast: Go's deadline model pairs with context.Context for end-to-end budgets, while Rust's std offers minimal, explicit per-socket durations and defers richer cancellation to async runtimes.
COMMON WRONG ANSWERS Believing context.Context can interrupt a blocked socket Read by itself; it cannot, you still need a deadline on the conn. Confusing Go's absolute time.Time with a duration. Assuming Rust std supports cancellation tokens; it does not.
LIKELY FOLLOW-UPS How do you implement an overall request timeout across multiple reads in Go? Reset the deadline before each operation or set one absolute deadline. How does this change under Tokio, where you wrap futures in timeout? What error kind signals a timeout in each?
ONE CONCRETE EXAMPLE Go: conn.SetReadDeadline(time.Now().Add(2*time.Second)); n, err := conn.Read(buf); err.(net.Error).Timeout() distinguishes timeout from other failures. Rust: stream.set_read_timeout(Some(Duration::from_secs(2)))?; match stream.read(&mut buf) handles Err where e.kind() is WouldBlock or TimedOut. The absolute-versus-relative difference is the crux of the comparison.
Read the original → pkg.go.dev
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.