mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-13 15:00:24 +08:00
优化代码
This commit is contained in:
@@ -22,15 +22,17 @@ var SharedHTTPClientPool = NewHTTPClientPool()
|
|||||||
|
|
||||||
// HTTPClientPool 客户端池
|
// HTTPClientPool 客户端池
|
||||||
type HTTPClientPool struct {
|
type HTTPClientPool struct {
|
||||||
clientExpiredDuration time.Duration
|
|
||||||
clientsMap map[string]*HTTPClient // backend key => client
|
clientsMap map[string]*HTTPClient // backend key => client
|
||||||
locker sync.Mutex
|
|
||||||
|
cleanTicker *time.Ticker
|
||||||
|
|
||||||
|
locker sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewHTTPClientPool 获取新对象
|
// NewHTTPClientPool 获取新对象
|
||||||
func NewHTTPClientPool() *HTTPClientPool {
|
func NewHTTPClientPool() *HTTPClientPool {
|
||||||
var pool = &HTTPClientPool{
|
var pool = &HTTPClientPool{
|
||||||
clientExpiredDuration: 3600 * time.Second,
|
cleanTicker: time.NewTicker(1 * time.Hour),
|
||||||
clientsMap: map[string]*HTTPClient{},
|
clientsMap: map[string]*HTTPClient{},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,10 +55,20 @@ func (this *HTTPClientPool) Client(req *HTTPRequest,
|
|||||||
|
|
||||||
var key = origin.UniqueKey() + "@" + originAddr
|
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()
|
this.locker.Lock()
|
||||||
defer this.locker.Unlock()
|
defer this.locker.Unlock()
|
||||||
|
|
||||||
client, found := this.clientsMap[key]
|
// 再次查找
|
||||||
|
client, found = this.clientsMap[key]
|
||||||
if found {
|
if found {
|
||||||
client.UpdateAccessTime()
|
client.UpdateAccessTime()
|
||||||
return client.RawClient(), nil
|
return client.RawClient(), nil
|
||||||
@@ -135,7 +147,7 @@ func (this *HTTPClientPool) Client(req *HTTPRequest,
|
|||||||
MaxConnsPerHost: maxConnections,
|
MaxConnsPerHost: maxConnections,
|
||||||
IdleConnTimeout: idleTimeout,
|
IdleConnTimeout: idleTimeout,
|
||||||
ExpectContinueTimeout: 1 * time.Second,
|
ExpectContinueTimeout: 1 * time.Second,
|
||||||
TLSHandshakeTimeout: 3 * time.Second,
|
TLSHandshakeTimeout: 5 * time.Second,
|
||||||
TLSClientConfig: tlsConfig,
|
TLSClientConfig: tlsConfig,
|
||||||
Proxy: nil,
|
Proxy: nil,
|
||||||
},
|
},
|
||||||
@@ -170,13 +182,12 @@ func (this *HTTPClientPool) Client(req *HTTPRequest,
|
|||||||
|
|
||||||
// 清理不使用的Client
|
// 清理不使用的Client
|
||||||
func (this *HTTPClientPool) cleanClients() {
|
func (this *HTTPClientPool) cleanClients() {
|
||||||
var ticker = time.NewTicker(this.clientExpiredDuration)
|
for range this.cleanTicker.C {
|
||||||
for range ticker.C {
|
var nowTime = time.Now().Unix()
|
||||||
currentAt := time.Now().Unix()
|
|
||||||
|
|
||||||
this.locker.Lock()
|
this.locker.Lock()
|
||||||
for k, client := range this.clientsMap {
|
for k, client := range this.clientsMap {
|
||||||
if client.AccessTime() < currentAt+86400 { // 超过 N 秒没有调用就关闭
|
if client.AccessTime() < nowTime+86400 { // 超过 N 秒没有调用就关闭
|
||||||
delete(this.clientsMap, k)
|
delete(this.clientsMap, k)
|
||||||
client.Close()
|
client.Close()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -210,7 +210,7 @@ func (this *HTTPRequest) doReverseProxy() {
|
|||||||
// 获取请求客户端
|
// 获取请求客户端
|
||||||
client, err := SharedHTTPClientPool.Client(this, origin, originAddr, this.reverseProxy.ProxyProtocol, this.reverseProxy.FollowRedirects)
|
client, err := SharedHTTPClientPool.Client(this, origin, originAddr, this.reverseProxy.ProxyProtocol, this.reverseProxy.FollowRedirects)
|
||||||
if err != nil {
|
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)
|
this.write50x(err, http.StatusBadGateway, true)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -231,7 +231,7 @@ func (this *HTTPRequest) doReverseProxy() {
|
|||||||
this.reverseProxy.ResetScheduling()
|
this.reverseProxy.ResetScheduling()
|
||||||
})
|
})
|
||||||
this.write50x(err, http.StatusBadGateway, true)
|
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 {
|
} else if httpErr.Err != context.Canceled {
|
||||||
SharedOriginStateManager.Fail(origin, requestHost, this.reverseProxy, func() {
|
SharedOriginStateManager.Fail(origin, requestHost, this.reverseProxy, func() {
|
||||||
this.reverseProxy.ResetScheduling()
|
this.reverseProxy.ResetScheduling()
|
||||||
@@ -244,7 +244,7 @@ func (this *HTTPRequest) doReverseProxy() {
|
|||||||
this.write50x(err, http.StatusBadGateway, true)
|
this.write50x(err, http.StatusBadGateway, true)
|
||||||
}
|
}
|
||||||
if httpErr.Err != io.EOF {
|
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 {
|
} else {
|
||||||
// 是否为客户端方面的错误
|
// 是否为客户端方面的错误
|
||||||
@@ -395,13 +395,13 @@ func (this *HTTPRequest) doReverseProxy() {
|
|||||||
var closeErr = resp.Body.Close()
|
var closeErr = resp.Body.Close()
|
||||||
if closeErr != nil {
|
if closeErr != nil {
|
||||||
if !this.canIgnore(closeErr) {
|
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 err != nil && err != io.EOF {
|
||||||
if !this.canIgnore(err) {
|
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)
|
this.addError(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user