Go 1.27版本引入了HTTP/1连接复用的自动优化机制,显著提升了重复HTTP请求的执行效率。1在性能表现上,调用HTTP请求函数100次的耗时从Go 1.26的约710毫秒降低至Go 1.27的约150毫秒,性能提升接近5倍。1
这一改进针对的是Go 1.26中存在的一个限制:当HTTP响应体未被完全读取就关闭时,net/http无法复用连接,导致每次请求都需重新建立TCP连接。1由于TCP握手需要1个往返时间(RTT),TLS 1.2握手需要2个RTT,建立新连接总共耗时3个RTT,频繁重建连接产生了显著的性能开销。1
Go 1.27的net/http.Transport现在采用自动异步处理未读响应体的方案,在50毫秒或256KB的限制内完成处理,从而实现连接复用。1这种优化对HTTP/1特别有意义,因为HTTP/2采用多路复用设计,本身不存在这类问题。1
Go 1.27 introduces automatic optimization for HTTP/1 connection reuse, delivering approximately a fivefold performance improvement for repeated HTTP requests 1. In the previous version, Go 1.26 prevented connection reuse when response bodies were not fully read before being closed, forcing each request to establish a new connection 1. The net/http.Transport in Go 1.27 now automatically handles unread response bodies asynchronously, completing the process within either 50 milliseconds or 256 kilobytes, whichever limit is reached first, thereby enabling connection reuse 1.
Performance benchmarks demonstrate the practical impact of this enhancement 1. Calling a getStatus function 100 times takes approximately 710 milliseconds in Go 1.26 but only around 150 milliseconds in Go 1.27, reflecting the significant efficiency gains 1. This improvement addresses a fundamental limitation of HTTP/1: when response bodies remain partially unread, connections cannot be reused, whereas HTTP/2 eliminates this constraint through its multiplexing architecture 1. The cost of establishing new connections is substantial, requiring three round-trip times—one for TCP handshake and two for TLS 1.2 handshake—making connection reuse critical for performance 1.
评论
还没有评论,欢迎留下第一条。