From 1f9db0e3c51fe251841539ae7d76211119d2162d Mon Sep 17 00:00:00 2001 From: cxlearning <1525754926@qq.com> Date: Thu, 5 Sep 2024 16:17:08 +0800 Subject: [PATCH] http: optimization method tryDeliver Reduce the scope of locks and make them more standardized --- src/net/http/transport.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/net/http/transport.go b/src/net/http/transport.go index 26900620f1a..8fcb0102954 100644 --- a/src/net/http/transport.go +++ b/src/net/http/transport.go @@ -1296,18 +1296,19 @@ func (w *wantConn) getCtxForDial() context.Context { // tryDeliver attempts to deliver pc, err to w and reports whether it succeeded. func (w *wantConn) tryDeliver(pc *persistConn, err error, idleAt time.Time) bool { + if (pc == nil) == (err == nil) { + panic("net/http: internal error: misuse of tryDeliver") + } + w.mu.Lock() defer w.mu.Unlock() if w.done { return false } - if (pc == nil) == (err == nil) { - panic("net/http: internal error: misuse of tryDeliver") - } + w.ctx = nil w.done = true - w.result <- connOrError{pc: pc, err: err, idleAt: idleAt} close(w.result)