From af4e34b7edae36abdf18cef5f3c03429f5c494cb Mon Sep 17 00:00:00 2001 From: mamba <371510756@qq.com> Date: Thu, 26 Sep 2024 09:16:00 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=F0=9F=A4=96=20[frontend-gray]?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=85=B3=E4=BA=8E=E5=A4=84=E7=90=86index=20p?= =?UTF-8?q?age=20=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=20(#1345)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Kent Dong --- .../extensions/frontend-gray/README.md | 22 ++++----- .../extensions/frontend-gray/envoy.yaml | 19 ++++---- .../wasm-go/extensions/frontend-gray/main.go | 48 ++++++++++--------- 3 files changed, 45 insertions(+), 44 deletions(-) diff --git a/plugins/wasm-go/extensions/frontend-gray/README.md b/plugins/wasm-go/extensions/frontend-gray/README.md index 1711969c9..8dee32008 100644 --- a/plugins/wasm-go/extensions/frontend-gray/README.md +++ b/plugins/wasm-go/extensions/frontend-gray/README.md @@ -130,7 +130,7 @@ grayDeployments: - name: beta-user version: gray enabled: true - weight: 80 + weight: 80 ``` 总的灰度规则为100%,其中灰度版本的权重为`80%`,基线版本为`20%`。一旦用户命中了灰度规则,会根据IP固定这个用户的灰度版本(否则会在下次请求时随机选择一个灰度版本)。 @@ -229,16 +229,16 @@ grayDeployments: - name: beta-user version: gray enabled: true - weight: 80 + weight: 80 injection: - head: - - - body: - first: - - - - - last: - - - - + head: + - + body: + first: + - + - + last: + - + - ``` 通过 `injection`往HTML首页注入代码,可以在`head`标签注入代码,也可以在`body`标签的`first`和`last`位置注入代码。 \ No newline at end of file diff --git a/plugins/wasm-go/extensions/frontend-gray/envoy.yaml b/plugins/wasm-go/extensions/frontend-gray/envoy.yaml index f454a7f07..e859c29a5 100644 --- a/plugins/wasm-go/extensions/frontend-gray/envoy.yaml +++ b/plugins/wasm-go/extensions/frontend-gray/envoy.yaml @@ -73,25 +73,24 @@ static_resources: ], "rewrite": { "host": "frontend-gray-cn-shanghai.oss-cn-shanghai-internal.aliyuncs.com", - "notFoundUri": "/mfe/app1/{version}/333.html", + "notFoundUri": "/cygtapi/{version}/333.html", "indexRouting": { - "/app1": "/mfe/app1/{version}/index.html", - "/": "/mfe/app1/{version}/index.html" + "/app1": "/cygtapi/{version}/index.html", + "/": "/cygtapi/{version}/index.html" }, "fileRouting": { - "/": "/mfe/app1/{version}", - "/app1": "/mfe/app1/{version}" + "/": "/cygtapi/{version}", + "/app1": "/cygtapi/{version}" } }, "baseDeployment": { - "version": "dev" + "version": "base" }, "grayDeployments": [ { "name": "beta-user", - "version": "0.0.1", - "enabled": true, - "weight": 50 + "version": "gray", + "enabled": true } ], "injection": { @@ -128,4 +127,4 @@ static_resources: address: socket_address: address: frontend-gray-cn-shanghai.oss-cn-shanghai.aliyuncs.com - port_value: 80 \ No newline at end of file + port_value: 80 diff --git a/plugins/wasm-go/extensions/frontend-gray/main.go b/plugins/wasm-go/extensions/frontend-gray/main.go index e1fc7a8b8..148751cc3 100644 --- a/plugins/wasm-go/extensions/frontend-gray/main.go +++ b/plugins/wasm-go/extensions/frontend-gray/main.go @@ -108,19 +108,26 @@ func onHttpResponseHeader(ctx wrapper.HttpContext, grayConfig config.GrayConfig, if !util.IsGrayEnabled(grayConfig) { return types.ActionContinue } - status, err := proxywasm.GetHttpResponseHeader(":status") - contentType, _ := proxywasm.GetHttpResponseHeader("Content-Type") + isPageRequest, ok := ctx.GetContext(config.IsPageRequest).(bool) + if !ok { + isPageRequest = false // 默认值 + } + // response 不处理非首页的请求 + if !isPageRequest { + ctx.DontReadResponseBody() + return types.ActionContinue + } + status, err := proxywasm.GetHttpResponseHeader(":status") if grayConfig.Rewrite != nil && grayConfig.Rewrite.Host != "" { // 删除Content-Disposition,避免自动下载文件 proxywasm.RemoveHttpResponseHeader("Content-Disposition") } - isPageRequest, ok := ctx.GetContext(config.IsPageRequest).(bool) - if !ok { - isPageRequest = false // 默认值 - } + // 删除content-length,可能要修改Response返回值 + proxywasm.RemoveHttpResponseHeader("Content-Length") + // 处理code为 200的情况 if err != nil || status != "200" { if status == "404" { if grayConfig.Rewrite.NotFound != "" && isPageRequest { @@ -143,6 +150,7 @@ func onHttpResponseHeader(ctx wrapper.HttpContext, grayConfig config.GrayConfig, ctx.BufferResponseBody() return types.ActionContinue } else { + // 直接返回400 ctx.DontReadResponseBody() } } @@ -150,25 +158,19 @@ func onHttpResponseHeader(ctx wrapper.HttpContext, grayConfig config.GrayConfig, return types.ActionContinue } - // 删除content-length,可能要修改Response返回值 - proxywasm.RemoveHttpResponseHeader("Content-Length") + // 不会进去Streaming 的Body处理 + ctx.BufferResponseBody() + proxywasm.ReplaceHttpResponseHeader("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate") - if strings.HasPrefix(contentType, "text/html") || isPageRequest { - // 不会进去Streaming 的Body处理 - ctx.BufferResponseBody() + frontendVersion := ctx.GetContext(config.XPreHigressTag).(string) + xUniqueClient := ctx.GetContext(config.XUniqueClientId).(string) - proxywasm.ReplaceHttpResponseHeader("Cache-Control", "no-cache, no-store") - - frontendVersion := ctx.GetContext(config.XPreHigressTag).(string) - xUniqueClient := ctx.GetContext(config.XUniqueClientId).(string) - - // 设置前端的版本 - proxywasm.AddHttpResponseHeader("Set-Cookie", fmt.Sprintf("%s=%s,%s; Max-Age=%s; Path=/;", config.XPreHigressTag, frontendVersion, xUniqueClient, grayConfig.UserStickyMaxAge)) - // 设置后端的版本 - if util.IsBackendGrayEnabled(grayConfig) { - backendVersion := ctx.GetContext(grayConfig.BackendGrayTag).(string) - proxywasm.AddHttpResponseHeader("Set-Cookie", fmt.Sprintf("%s=%s; Max-Age=%s; Path=/;", grayConfig.BackendGrayTag, backendVersion, grayConfig.UserStickyMaxAge)) - } + // 设置前端的版本 + proxywasm.AddHttpResponseHeader("Set-Cookie", fmt.Sprintf("%s=%s,%s; Max-Age=%s; Path=/;", config.XPreHigressTag, frontendVersion, xUniqueClient, grayConfig.UserStickyMaxAge)) + // 设置后端的版本 + if util.IsBackendGrayEnabled(grayConfig) { + backendVersion := ctx.GetContext(grayConfig.BackendGrayTag).(string) + proxywasm.AddHttpResponseHeader("Set-Cookie", fmt.Sprintf("%s=%s; Max-Age=%s; Path=/;", grayConfig.BackendGrayTag, backendVersion, grayConfig.UserStickyMaxAge)) } return types.ActionContinue }