Skip to content

Commit

Permalink
chore: 🤖 [frontend-gray] 优化代码风格和命名
Browse files Browse the repository at this point in the history
  • Loading branch information
heimanba committed Sep 22, 2024
1 parent 2728da4 commit 444c6a3
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 28 deletions.
10 changes: 5 additions & 5 deletions plugins/wasm-go/extensions/frontend-gray/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
)

const (
XHigressTag = "x-higress-tag"
XUniqueClient = "x-unique-client"
XPreHigressTag = "x-pre-higress-tag"
IsPageRequest = "is-page-request"
IsNotFound = "is-not-found"
XHigressTag = "x-higress-tag"
XUniqueClientId = "x-unique-client"
XPreHigressTag = "x-pre-higress-tag"
IsPageRequest = "is-page-request"
IsNotFound = "is-not-found"
)

type LogInfo func(format string, args ...interface{})
Expand Down
35 changes: 21 additions & 14 deletions plugins/wasm-go/extensions/frontend-gray/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, grayConfig config.GrayConfig,
path, _ := proxywasm.GetHttpRequestHeader(":path")
fetchMode, _ := proxywasm.GetHttpRequestHeader("sec-fetch-mode")

isPageRequest := util.GetIsPageRequest(fetchMode, path)
isPageRequest := util.IsPageRequest(fetchMode, path)
hasRewrite := len(grayConfig.Rewrite.File) > 0 || len(grayConfig.Rewrite.Index) > 0
grayKeyValueByCookie := util.ExtractCookieValueByKey(cookies, grayConfig.GrayKey)
grayKeyValueByHeader, _ := proxywasm.GetHttpRequestHeader(grayConfig.GrayKey)
// 优先从cookie中获取,否则从header中获取
grayKeyValue := util.GetGrayKey(grayKeyValueByCookie, grayKeyValueByHeader, grayConfig.GraySubKey)

// 如果有重写的配置,则进行重写
if hasRewrite {
// 禁止重新路由,要在更改Header之前操作,否则会失效
Expand All @@ -59,9 +58,7 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, grayConfig config.GrayConfig,
_ = proxywasm.RemoveHttpRequestHeader("Content-Length")
deployment := &config.Deployment{}

xPreHigressVersion := util.ExtractCookieValueByKey(cookies, config.XPreHigressTag)
preVersions := strings.Split(xPreHigressVersion, ",")

preVersion, preUniqueClientId := util.GetXPreHigressVersion(cookies)
// 客户端唯一ID,用于在按照比率灰度时候 客户访问黏贴
uniqueClientId := grayKeyValue
if uniqueClientId == "" {
Expand All @@ -73,20 +70,20 @@ func onHttpRequestHeaders(ctx wrapper.HttpContext, grayConfig config.GrayConfig,
if isPageRequest {
log.Infof("grayConfig.TotalGrayWeight==== %v", grayConfig.TotalGrayWeight)
if grayConfig.TotalGrayWeight > 0 {
deployment = util.FilterGrayWeight(&grayConfig, preVersions, uniqueClientId)
deployment = util.FilterGrayWeight(&grayConfig, preVersion, preUniqueClientId, uniqueClientId)
} else {
deployment = util.FilterGrayRule(&grayConfig, grayKeyValue)
}
log.Infof("index deployment: %v, path: %v, backend: %v, xPreHigressVersion: %v", deployment, path, deployment.BackendVersion, xPreHigressVersion)
log.Infof("index deployment: %v, path: %v, backend: %v, xPreHigressVersion: %s,%s", deployment, path, deployment.BackendVersion, preVersion, preUniqueClientId)
} else {
deployment = util.GetVersion(grayConfig, deployment, preVersions[0], isPageRequest)
deployment = util.GetVersion(grayConfig, deployment, preVersion, isPageRequest)
}
proxywasm.AddHttpRequestHeader(config.XHigressTag, deployment.Version)

ctx.SetContext(config.XPreHigressTag, deployment.Version)
ctx.SetContext(grayConfig.BackendGrayTag, deployment.BackendVersion)
ctx.SetContext(config.IsPageRequest, isPageRequest)
ctx.SetContext(config.XUniqueClient, uniqueClientId)
ctx.SetContext(config.XUniqueClientId, uniqueClientId)

rewrite := grayConfig.Rewrite
if rewrite.Host != "" {
Expand Down Expand Up @@ -119,7 +116,10 @@ func onHttpResponseHeader(ctx wrapper.HttpContext, grayConfig config.GrayConfig,
proxywasm.RemoveHttpResponseHeader("Content-Disposition")
}

isPageRequest := ctx.GetContext(config.IsPageRequest).(bool)
isPageRequest, ok := ctx.GetContext(config.IsPageRequest).(bool)
if !ok {
isPageRequest = false // 默认值
}

if err != nil || status != "200" {
if status == "404" {
Expand Down Expand Up @@ -160,7 +160,7 @@ func onHttpResponseHeader(ctx wrapper.HttpContext, grayConfig config.GrayConfig,
proxywasm.ReplaceHttpResponseHeader("Cache-Control", "no-cache, no-store")

frontendVersion := ctx.GetContext(config.XPreHigressTag).(string)
xUniqueClient := ctx.GetContext(config.XUniqueClient).(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))
Expand All @@ -177,11 +177,18 @@ func onHttpResponseBody(ctx wrapper.HttpContext, grayConfig config.GrayConfig, b
if !util.IsGrayEnabled(grayConfig) {
return types.ActionContinue
}
isPageRequest := ctx.GetContext(config.IsPageRequest).(bool)
isPageRequest, ok := ctx.GetContext(config.IsPageRequest).(bool)
if !ok {
isPageRequest = false // 默认值
}
frontendVersion := ctx.GetContext(config.XPreHigressTag).(string)

isNotFound := ctx.GetContext(config.IsNotFound)
if isPageRequest && isNotFound != nil && isNotFound.(bool) && grayConfig.Rewrite.Host != "" && grayConfig.Rewrite.NotFound != "" {
isNotFound, ok := ctx.GetContext(config.IsNotFound).(bool)
if !ok {
isNotFound = false // 默认值
}

if isPageRequest && isNotFound && grayConfig.Rewrite.Host != "" && grayConfig.Rewrite.NotFound != "" {
client := wrapper.NewClusterClient(wrapper.RouteCluster{Host: grayConfig.Rewrite.Host})

client.Get(strings.Replace(grayConfig.Rewrite.NotFound, "{version}", frontendVersion, -1), nil, func(statusCode int, responseHeaders http.Header, responseBody []byte) {
Expand Down
25 changes: 19 additions & 6 deletions plugins/wasm-go/extensions/frontend-gray/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ func LogInfof(format string, args ...interface{}) {
proxywasm.LogInfof(format, args...)
}

func GetXPreHigressVersion(cookies string) (string, string) {
xPreHigressVersion := ExtractCookieValueByKey(cookies, config.XPreHigressTag)
preVersions := strings.Split(xPreHigressVersion, ",")
if len(preVersions) == 0 {
return "", ""
}
if len(preVersions) == 1 {
return preVersions[0], ""
}

return strings.TrimSpace(preVersions[0]), strings.TrimSpace(preVersions[1])
}

// 从xff中获取真实的IP
func GetRealIpFromXff(xff string) string {
if xff != "" {
Expand Down Expand Up @@ -119,11 +132,11 @@ var indexSuffixes = []string{
".html", ".htm", ".jsp", ".php", ".asp", ".aspx", ".erb", ".ejs", ".twig",
}

func GetIsPageRequest(fetchMode string, p string) bool {
func IsPageRequest(fetchMode string, myPath string) bool {
if fetchMode == "cors" {
return false
}
ext := path.Ext(p)
ext := path.Ext(myPath)
return ext == "" || ContainsValue(indexSuffixes, ext)
}

Expand Down Expand Up @@ -230,18 +243,18 @@ func FilterGrayRule(grayConfig *config.GrayConfig, grayKeyValue string) *config.
return grayConfig.BaseDeployment
}

func FilterGrayWeight(grayConfig *config.GrayConfig, preVersions []string, uniqueClientId string) *config.Deployment {
func FilterGrayWeight(grayConfig *config.GrayConfig, preVersion string, preUniqueClientId string, uniqueClientId string) *config.Deployment {
// 如果没有灰度权重,直接返回基础版本
if grayConfig.TotalGrayWeight == 0 {
return grayConfig.BaseDeployment
}

deployments := append(grayConfig.GrayDeployments, grayConfig.BaseDeployment)
LogInfof("uniqueClientId: %s, preVersions: %v", uniqueClientId, preVersions)
LogInfof("preVersion: %s, preUniqueClientId: %s, uniqueClientId: %s", preVersion, preUniqueClientId, uniqueClientId)
// 用户粘滞,确保每个用户每次访问的都是走同一版本
if len(preVersions) > 1 && preVersions[1] != "" && uniqueClientId == preVersions[1] {
if preVersion != "" && uniqueClientId == preUniqueClientId {
for _, deployment := range deployments {
if deployment.Version == strings.Trim(preVersions[0], " ") {
if deployment.Version == preVersion {
return deployment
}
}
Expand Down
6 changes: 3 additions & 3 deletions plugins/wasm-go/extensions/frontend-gray/util/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestPrefixFileRewrite(t *testing.T) {
}
}

func TestGetIsPageRequest(t *testing.T) {
func TestIsPageRequest(t *testing.T) {
var tests = []struct {
fetchMode string
p string
Expand All @@ -99,7 +99,7 @@ func TestGetIsPageRequest(t *testing.T) {
for _, test := range tests {
testPath := test.p
t.Run(testPath, func(t *testing.T) {
output := GetIsPageRequest(test.fetchMode, testPath)
output := IsPageRequest(test.fetchMode, testPath)
assert.Equal(t, test.output, output)
})
}
Expand All @@ -117,7 +117,7 @@ func TestFilterGrayWeight(t *testing.T) {
t.Run(testName, func(t *testing.T) {
grayConfig := &config.GrayConfig{}
config.JsonToGrayConfig(gjson.Parse(test.input), grayConfig)
result := FilterGrayWeight(grayConfig, []string{"base", "1.0.1"}, "192.168.1.1")
result := FilterGrayWeight(grayConfig, "base", "1.0.1", "192.168.1.1")
t.Logf("result-----: %v", result)
})
}
Expand Down

0 comments on commit 444c6a3

Please sign in to comment.