-
Notifications
You must be signed in to change notification settings - Fork 542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add try-paths plugins #1598
Open
haifzhu
wants to merge
4
commits into
alibaba:main
Choose a base branch
from
haifzhu:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- | ||
title: 请求重试 | ||
keywords: [higress,try-paths] | ||
description: 请求重试 | ||
--- | ||
|
||
# 功能说明 | ||
`try-paths`插件支持请求基于不同的路径进行重试,直到请求到正确返回的请求,功能类似nginx的try files指令。 | ||
|
||
# 配置字段 | ||
|
||
| 名称 | 数据类型 | 填写要求 | 默认值 | 描述 | | ||
| -------- | -------- | -------- | -------- | -------- | | ||
| host | string | 必填 | - | FQDN格式的主机名称,比如<bucket name>.oss-cn-hangzhou.aliyuncs.com | | ||
| servicePort | string | 必填 | - | 服务端口 | | ||
| tryPaths | array of string | 必填 | - | 重试路径,比如`index.html`,`$uri`, `index.html` | | ||
| tryCodes | array of int | 非必填 | [403, 404] | 重试状态码,可自定义 | | ||
| timeout | int | 非必填 | 1000 | 重试请求的超时时间,单位ms | | ||
|
||
|
||
# 配置示例 | ||
|
||
## 配置了try-paths插件的场景 | ||
|
||
```yaml | ||
servicePort: 80 | ||
host: "<bucket name>.oss-cn-hangzhou.aliyuncs.com" | ||
tryPaths: | ||
- "$uri/" | ||
- "$uri.html" | ||
- "/index.html" | ||
|
||
``` | ||
|
||
基于该配置开启插件,触发插件的请求curl "http://a.com/a", 会依次请求 | ||
- http://<bucket name>.oss-cn-hangzhou.aliyuncs.com/a/ | ||
- http://<bucket name>.oss-cn-hangzhou.aliyuncs.com/a.html | ||
- http://<bucket name>.oss-cn-hangzhou.aliyuncs.com/index.html | ||
如果请求返回码不是重试状态码,会直接返回该请求体,否则继续重试下一个请求,所有请求都不是重试状态码,会继续请求默认后端服务。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- | ||
title: request retry | ||
keywords: [higress,try-paths] | ||
description: request retry | ||
--- | ||
|
||
# Function Description | ||
`try-paths` plugin supports retrying requests based on different paths until a correct response is received, similar to the try files instruction in nginx. | ||
|
||
# Configuration Fields | ||
|
||
| Name | Data Type | Fill Requirement | Default Value | Description | | ||
| -------- | -------- | -------- | -------- | -------- | | ||
| host | string | Required | - | the FQDN format of the host, i.e. <bucket name>.oss-cn-hangzhou.aliyuncs.com | | ||
| servicePort | string | Required | - | Service Port | | ||
| tryPaths | array of string | Required | - | Try path list,`index.html`,`$uri/`, `index.html` for example | | ||
| tryCodes | array of int | Optional | [403, 404] | Try response code,can be customized | | ||
| timeout | int | Optional | 1000 | The timeout for try request,unit is ms | | ||
|
||
# Configuration Example | ||
|
||
## scene with try-paths plugin configured | ||
|
||
```yaml | ||
servicePort: 80 | ||
host: "<bucket name>.oss-cn-hangzhou.aliyuncs.com" | ||
tryPaths: | ||
- "$uri/" | ||
- "$uri.html" | ||
- "/index.html" | ||
|
||
``` | ||
|
||
From the above configuration, the `try-paths` plugin is enabled. The request "curl http://a.com/a" will be tried with the following paths: | ||
- http://<bucket name>.oss-cn-hangzhou.aliyuncs.com/a/ | ||
- http://<bucket name>.oss-cn-hangzhou.aliyuncs.com/a.html | ||
- http://<bucket name>.oss-cn-hangzhou.aliyuncs.com/index.html | ||
If the response code is not the retry status code, the response will be returned directly, otherwise the next request will be tried. If all requests are not retry status codes, the default backend service will be requested. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module try-paths | ||
|
||
go 1.19 | ||
|
||
require ( | ||
github.com/alibaba/higress/plugins/wasm-go v1.4.0 | ||
github.com/higress-group/proxy-wasm-go-sdk v0.0.0-20240327114451-d6b7174a84fc | ||
github.com/stretchr/testify v1.8.4 | ||
github.com/tidwall/gjson v1.17.1 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/google/uuid v1.3.0 // indirect | ||
github.com/higress-group/nottinygc v0.0.0-20231101025119-e93c4c2f8520 // indirect | ||
github.com/magefile/mage v1.14.0 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/tidwall/match v1.1.1 // indirect | ||
github.com/tidwall/pretty v1.2.0 // indirect | ||
github.com/tidwall/resp v0.1.1 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
github.com/alibaba/higress/plugins/wasm-go v1.4.0 h1:uFf+mbZ2iuRXJzRbmWBuxiHvNDMGf3PCBJ6TI86bopY= | ||
github.com/alibaba/higress/plugins/wasm-go v1.4.0/go.mod h1:10jQXKsYFUF7djs+Oy7t82f4dbie9pISfP9FJwpPLuk= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= | ||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= | ||
github.com/higress-group/nottinygc v0.0.0-20231101025119-e93c4c2f8520 h1:IHDghbGQ2DTIXHBHxWfqCYQW1fKjyJ/I7W1pMyUDeEA= | ||
github.com/higress-group/nottinygc v0.0.0-20231101025119-e93c4c2f8520/go.mod h1:Nz8ORLaFiLWotg6GeKlJMhv8cci8mM43uEnLA5t8iew= | ||
github.com/higress-group/proxy-wasm-go-sdk v0.0.0-20240327114451-d6b7174a84fc h1:t2AT8zb6N/59Y78lyRWedVoVWHNRSCBh0oWCC+bluTQ= | ||
github.com/higress-group/proxy-wasm-go-sdk v0.0.0-20240327114451-d6b7174a84fc/go.mod h1:hNFjhrLUIq+kJ9bOcs8QtiplSQ61GZXtd2xHKx4BYRo= | ||
github.com/magefile/mage v1.14.0 h1:6QDX3g6z1YvJ4olPhT1wksUcSa/V0a1B+pJb73fBjyo= | ||
github.com/magefile/mage v1.14.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= | ||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= | ||
github.com/tidwall/gjson v1.17.1 h1:wlYEnwqAHgzmhNUFfw7Xalt2JzQvsMx2Se4PcoFCT/U= | ||
github.com/tidwall/gjson v1.17.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= | ||
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= | ||
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= | ||
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= | ||
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= | ||
github.com/tidwall/resp v0.1.1 h1:Ly20wkhqKTmDUPlyM1S7pWo5kk0tDu8OoC/vFArXmwE= | ||
github.com/tidwall/resp v0.1.1/go.mod h1:3/FrruOBAxPTPtundW0VXgmsQ4ZBA0Aw714lVYgwFa0= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper" | ||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm" | ||
"github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types" | ||
"github.com/tidwall/gjson" | ||
) | ||
|
||
// 默认超时时间为1s | ||
var defaultTimeout uint32 = 1000 | ||
|
||
const VariableReplacedStr = "$uri" | ||
|
||
func main() { | ||
wrapper.SetCtx( | ||
"try-paths", | ||
wrapper.ParseConfigBy(parseConfig), | ||
wrapper.ProcessRequestHeadersBy(onHttpRequestHeaders), | ||
) | ||
} | ||
|
||
type TryPathsConfig struct { | ||
tryPaths []string | ||
timeout uint32 | ||
tryCodes []int // 支持多个返回码 | ||
client wrapper.HttpClient | ||
} | ||
|
||
func parseConfig(json gjson.Result, config *TryPathsConfig, log wrapper.Log) error { | ||
for _, result := range json.Get("tryPaths").Array() { | ||
config.tryPaths = append(config.tryPaths, result.String()) | ||
} | ||
|
||
if json.Get("tryCodes").String() == "" { | ||
// tryCodes默认值为["404", "403"] | ||
config.tryCodes = []int{http.StatusNotFound, http.StatusForbidden} | ||
} else { | ||
for _, result := range json.Get("code").Array() { | ||
config.tryCodes = append(config.tryCodes, int(result.Int())) | ||
} | ||
} | ||
|
||
timeout := json.Get("timeout").Int() | ||
if timeout == 0 { | ||
// tryPaths的timeout默认值为1s | ||
config.timeout = defaultTimeout | ||
} else { | ||
config.timeout = uint32(timeout) | ||
} | ||
|
||
client, err := Client(json) | ||
if err != nil { | ||
return err | ||
} | ||
config.client = client | ||
return nil | ||
} | ||
|
||
func tryHttpCall(config TryPathsConfig, index int, path string, log wrapper.Log) { | ||
if index >= len(config.tryPaths) { | ||
proxywasm.ResumeHttpRequest() | ||
return | ||
} | ||
|
||
requestPath := strings.Replace(config.tryPaths[index], VariableReplacedStr, path, -1) | ||
johnlanni marked this conversation as resolved.
Show resolved
Hide resolved
|
||
log.Debugf("try path start, path: %s", requestPath) | ||
err := config.client.Get(requestPath, nil, | ||
func(statusCode int, responseHeaders http.Header, responseBody []byte) { | ||
if !contains(config.tryCodes, statusCode) { | ||
proxywasm.SendHttpResponse(uint32(statusCode), convertHttpHeadersToStruct(responseHeaders), responseBody, -1) | ||
return | ||
} | ||
tryHttpCall(config, index+1, path, log) | ||
}, config.timeout) | ||
|
||
if err != nil { | ||
log.Errorf("try path failed, path %s, error: %s", requestPath, err.Error()) | ||
tryHttpCall(config, index+1, path, log) | ||
} | ||
} | ||
|
||
func onHttpRequestHeaders(ctx wrapper.HttpContext, config TryPathsConfig, log wrapper.Log) types.Action { | ||
log.Debugf("try path config: %+v", config) | ||
path := ctx.Path() | ||
if len(config.tryPaths) == 0 { | ||
return types.ActionContinue | ||
} | ||
|
||
tryHttpCall(config, 0, path, log) | ||
return types.ActionPause | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper" | ||
"github.com/tidwall/gjson" | ||
) | ||
|
||
func convertHttpHeadersToStruct(responseHeaders http.Header) [][2]string { | ||
headerStruct := make([][2]string, len(responseHeaders)) | ||
i := 0 | ||
for key, values := range responseHeaders { | ||
headerStruct[i][0] = key | ||
headerStruct[i][1] = values[0] | ||
i++ | ||
} | ||
return headerStruct | ||
} | ||
|
||
func contains(array []int, value int) bool { | ||
for _, v := range array { | ||
if v == value { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func Client(json gjson.Result) (wrapper.HttpClient, error) { | ||
host := json.Get("host").String() | ||
servicePort := json.Get("servicePort").Int() | ||
return wrapper.NewClusterClient(wrapper.FQDNCluster{ | ||
FQDN: host, | ||
Port: servicePort, | ||
}), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestConvertHttpHeadersToStruct(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
httpHeaders map[string][]string | ||
expected [][2]string | ||
}{ | ||
{ | ||
name: "empty header", | ||
httpHeaders: map[string][]string{}, | ||
expected: [][2]string{}, | ||
}, | ||
{ | ||
name: "headers with content type", | ||
httpHeaders: map[string][]string{ | ||
"Content-Type": []string{"application/json", "application/xml"}, | ||
}, | ||
expected: [][2]string{ | ||
{"Content-Type", "application/json"}, | ||
{"Content-Type", "application/xml"}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.name, func(t *testing.T) { | ||
result := convertHttpHeadersToStruct(c.httpHeaders) | ||
require.Equal(t, c.expected, result) | ||
}) | ||
} | ||
} | ||
|
||
func TestContains(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
array []int | ||
value int | ||
expected bool | ||
}{ | ||
{ | ||
name: "multi value, contains", | ||
array: []int{404, 304}, | ||
value: 404, | ||
expected: true, | ||
}, | ||
{ | ||
name: "multi value, no contains", | ||
array: []int{404, 304}, | ||
value: 200, | ||
expected: false, | ||
}, | ||
{ | ||
name: "one value contains", | ||
array: []int{200}, | ||
value: 200, | ||
expected: true, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.name, func(t *testing.T) { | ||
result := contains(c.array, c.value) | ||
require.Equal(t, c.expected, result) | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
建议两个配置:
serviceHost 配 oss 域名
serviceName 配FQDN格式的服务名称(控制台服务列表看到的名字)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可以参考 ext-auth 里 endpoint 配置项里的 service_host 和 service_name 的概念:https://higress.cn/docs/latest/plugins/authentication/ext-auth/