From 1d2160122822ad5f784a615c606922b275703c16 Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Fri, 16 Sep 2022 09:37:49 +0800 Subject: [PATCH] =?UTF-8?q?Websocket=E4=B9=9F=E6=94=AF=E6=8C=81=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E8=87=AA=E5=8A=A8=E9=87=8D=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/nodes/http_request_reverse_proxy.go | 2 +- internal/nodes/http_request_websocket.go | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/internal/nodes/http_request_reverse_proxy.go b/internal/nodes/http_request_reverse_proxy.go index e321ba1..57914cb 100644 --- a/internal/nodes/http_request_reverse_proxy.go +++ b/internal/nodes/http_request_reverse_proxy.go @@ -240,7 +240,7 @@ func (this *HTTPRequest) doOriginRequest(failedOriginIds []int64, failedLnNodeId // 判断是否为Websocket请求 if this.RawReq.Header.Get("Upgrade") == "websocket" { - this.doWebsocket(requestHost) + shouldRetry = this.doWebsocket(requestHost, isLastRetry) return } diff --git a/internal/nodes/http_request_websocket.go b/internal/nodes/http_request_websocket.go index 12da4b4..2f3c69d 100644 --- a/internal/nodes/http_request_websocket.go +++ b/internal/nodes/http_request_websocket.go @@ -9,7 +9,7 @@ import ( ) // 处理Websocket请求 -func (this *HTTPRequest) doWebsocket(requestHost string) { +func (this *HTTPRequest) doWebsocket(requestHost string, isLastRetry bool) (shouldRetry bool) { if this.web.WebsocketRef == nil || !this.web.WebsocketRef.IsOn || this.web.Websocket == nil || !this.web.Websocket.IsOn { this.writer.WriteHeader(http.StatusForbidden) this.addError(errors.New("websocket have not been enabled yet")) @@ -43,13 +43,16 @@ func (this *HTTPRequest) doWebsocket(requestHost string) { // TODO 增加N次错误重试,重试的时候需要尝试不同的源站 originConn, _, err := OriginConnect(this.origin, this.requestServerPort(), this.RawReq.RemoteAddr, requestHost) if err != nil { - this.write50x(err, http.StatusBadGateway, "Failed to connect origin site", "源站连接失败", false) + if isLastRetry { + this.write50x(err, http.StatusBadGateway, "Failed to connect origin site", "源站连接失败", false) + } // 增加失败次数 SharedOriginStateManager.Fail(this.origin, requestHost, this.reverseProxy, func() { this.reverseProxy.ResetScheduling() }) + shouldRetry = true return } @@ -98,4 +101,6 @@ func (this *HTTPRequest) doWebsocket(requestHost string) { _ = originConn.Close() }() _, _ = io.Copy(originConn, clientConn) + + return }