From d2d4f48815f139aae9065cd48915332980535464 Mon Sep 17 00:00:00 2001 From: Darhwa <65339668+darhwa@users.noreply.github.com> Date: Sun, 13 Sep 2020 02:01:41 +0800 Subject: [PATCH] Fix the coarse-grained mutex in http outbound fix v2ray#2731 --- proxy/http/client.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/proxy/http/client.go b/proxy/http/client.go index dd30ff360..745ddcfd9 100644 --- a/proxy/http/client.go +++ b/proxy/http/client.go @@ -210,9 +210,10 @@ func setUpHTTPTunnel(ctx context.Context, dest net.Destination, target string, u } cachedH2Mutex.Lock() - defer cachedH2Mutex.Unlock() + cachedConn, cachedConnFound := cachedH2Conns[dest] + cachedH2Mutex.Unlock() - if cachedConn, found := cachedH2Conns[dest]; found { + if cachedConnFound { rc, cc := cachedConn.rawConn, cachedConn.h2Conn if cc.CanTakeNewRequest() { proxyConn, err := connectHTTP2(rc, cc) @@ -260,6 +261,7 @@ func setUpHTTPTunnel(ctx context.Context, dest net.Destination, target string, u return nil, err } + cachedH2Mutex.Lock() if cachedH2Conns == nil { cachedH2Conns = make(map[net.Destination]h2Conn) } @@ -268,6 +270,7 @@ func setUpHTTPTunnel(ctx context.Context, dest net.Destination, target string, u rawConn: rawConn, h2Conn: h2clientConn, } + cachedH2Mutex.Unlock() return proxyConn, err default: