diff --git a/internal/nodes/http_client_pool.go b/internal/nodes/http_client_pool.go index 15b89fb..b0c1d23 100644 --- a/internal/nodes/http_client_pool.go +++ b/internal/nodes/http_client_pool.go @@ -22,16 +22,18 @@ var SharedHTTPClientPool = NewHTTPClientPool() // HTTPClientPool 客户端池 type HTTPClientPool struct { - clientExpiredDuration time.Duration - clientsMap map[string]*HTTPClient // backend key => client - locker sync.Mutex + clientsMap map[string]*HTTPClient // backend key => client + + cleanTicker *time.Ticker + + locker sync.RWMutex } // NewHTTPClientPool 获取新对象 func NewHTTPClientPool() *HTTPClientPool { var pool = &HTTPClientPool{ - clientExpiredDuration: 3600 * time.Second, - clientsMap: map[string]*HTTPClient{}, + cleanTicker: time.NewTicker(1 * time.Hour), + clientsMap: map[string]*HTTPClient{}, } goman.New(func() { @@ -53,10 +55,20 @@ func (this *HTTPClientPool) Client(req *HTTPRequest, var key = origin.UniqueKey() + "@" + originAddr + this.locker.RLock() + client, found := this.clientsMap[key] + this.locker.RUnlock() + if found { + client.UpdateAccessTime() + return client.RawClient(), nil + } + + // 这里不能使用RLock,避免因为并发生成多个同样的client实例 this.locker.Lock() defer this.locker.Unlock() - client, found := this.clientsMap[key] + // 再次查找 + client, found = this.clientsMap[key] if found { client.UpdateAccessTime() return client.RawClient(), nil @@ -135,7 +147,7 @@ func (this *HTTPClientPool) Client(req *HTTPRequest, MaxConnsPerHost: maxConnections, IdleConnTimeout: idleTimeout, ExpectContinueTimeout: 1 * time.Second, - TLSHandshakeTimeout: 3 * time.Second, + TLSHandshakeTimeout: 5 * time.Second, TLSClientConfig: tlsConfig, Proxy: nil, }, @@ -170,13 +182,12 @@ func (this *HTTPClientPool) Client(req *HTTPRequest, // 清理不使用的Client func (this *HTTPClientPool) cleanClients() { - var ticker = time.NewTicker(this.clientExpiredDuration) - for range ticker.C { - currentAt := time.Now().Unix() + for range this.cleanTicker.C { + var nowTime = time.Now().Unix() this.locker.Lock() for k, client := range this.clientsMap { - if client.AccessTime() < currentAt+86400 { // 超过 N 秒没有调用就关闭 + if client.AccessTime() < nowTime+86400 { // 超过 N 秒没有调用就关闭 delete(this.clientsMap, k) client.Close() } diff --git a/internal/nodes/http_request_reverse_proxy.go b/internal/nodes/http_request_reverse_proxy.go index 94d9421..9c6796c 100644 --- a/internal/nodes/http_request_reverse_proxy.go +++ b/internal/nodes/http_request_reverse_proxy.go @@ -210,7 +210,7 @@ func (this *HTTPRequest) doReverseProxy() { // 获取请求客户端 client, err := SharedHTTPClientPool.Client(this, origin, originAddr, this.reverseProxy.ProxyProtocol, this.reverseProxy.FollowRedirects) if err != nil { - remotelogs.Error("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": "+err.Error()) + remotelogs.Error("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": get client failed: "+err.Error()) this.write50x(err, http.StatusBadGateway, true) return } @@ -231,7 +231,7 @@ func (this *HTTPRequest) doReverseProxy() { this.reverseProxy.ResetScheduling() }) this.write50x(err, http.StatusBadGateway, true) - remotelogs.Warn("HTTP_REQUEST_REVERSE_PROXY", this.RawReq.URL.String()+"': "+err.Error()) + remotelogs.Warn("HTTP_REQUEST_REVERSE_PROXY", this.RawReq.URL.String()+"': request failed: "+err.Error()) } else if httpErr.Err != context.Canceled { SharedOriginStateManager.Fail(origin, requestHost, this.reverseProxy, func() { this.reverseProxy.ResetScheduling() @@ -244,7 +244,7 @@ func (this *HTTPRequest) doReverseProxy() { this.write50x(err, http.StatusBadGateway, true) } if httpErr.Err != io.EOF { - remotelogs.Warn("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": "+err.Error()) + remotelogs.Warn("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": request failed: "+err.Error()) } } else { // 是否为客户端方面的错误 @@ -395,13 +395,13 @@ func (this *HTTPRequest) doReverseProxy() { var closeErr = resp.Body.Close() if closeErr != nil { if !this.canIgnore(closeErr) { - remotelogs.Warn("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": "+closeErr.Error()) + remotelogs.Warn("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": closing error: "+closeErr.Error()) } } if err != nil && err != io.EOF { if !this.canIgnore(err) { - remotelogs.Warn("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": "+err.Error()) + remotelogs.Warn("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": writing error: "+err.Error()) this.addError(err) } }