AlexStocks commented on code in PR #1076:
URL: https://github.com/apache/dubbo-go-pixiu/pull/1076#discussion_r4505143101
##########
pkg/filter/http/proxyrewrite/rewrite.go:
##########
@@ -79,7 +79,8 @@ func (factory *FilterFactory) Apply() error {
if len(factory.cfg.UriRegex) != 2 {
return errors.Errorf("UriRegex len must == 2, %v",
factory.cfg.UriRegex)
}
- return nil
+ _, err := regexp.Compile(factory.cfg.UriRegex[0])
Review Comment:
🟡 **编译结果被丢掉了,`PrepareFilterChain` 仍然每请求 `MustCompile`。**
`Apply()` 现在编译一次、然后扔掉结果;真正构造 filter 的地方(`PrepareFilterChain`,第 97 行)还在
`regexp.MustCompile(cfg.UriRegex[0])`。这意味着:
- 每个 HTTP 请求多一次正则编译(`PrepareFilterChain` 是 per-request
的,`filter_manager.go:82` → `http/manager.go:151`);
- panic 路径仍然存在,只是被 Apply 提前挡住了——`filter_manager_test.go` 这类直接构造 factory
的代码一绕过 Apply 就会 panic。
把编译结果落到 factory 上即可同时解决两件事:
```go
type FilterFactory struct {
cfg *Config
uriRegex *regexp.Regexp // compiled in Apply, reused per request
}
func (factory *FilterFactory) Apply() error {
if len(factory.cfg.UriRegex) != 2 {
return errors.Errorf("UriRegex len must == 2, %v",
factory.cfg.UriRegex)
}
re, err := regexp.Compile(factory.cfg.UriRegex[0])
if err != nil {
return errors.Wrapf(err, "invalid uri_regex %q",
factory.cfg.UriRegex[0])
}
factory.uriRegex = re
return nil
}
func (factory *FilterFactory) PrepareFilterChain(ctx
*contexthttp.HttpContext, chain filter.FilterChain) error {
f := &Filter{uriRegex: factory.uriRegex, replace:
factory.cfg.UriRegex[1], Headers: headers}
chain.AppendDecodeFilters(f)
return nil
}
```
(`FilterFactory` 每次 reload 都是 `CreateFilterFactory` + `Apply`
新建的,所以缓存编译结果是安全的;`fm.reload` 见 `filter_manager.go:145-208`。)
##########
pkg/filter/http/proxyrewrite/rewrite.go:
##########
@@ -79,7 +79,8 @@ func (factory *FilterFactory) Apply() error {
if len(factory.cfg.UriRegex) != 2 {
return errors.Errorf("UriRegex len must == 2, %v",
factory.cfg.UriRegex)
}
- return nil
+ _, err := regexp.Compile(factory.cfg.UriRegex[0])
+ return err
Review Comment:
💭 错误直接 `return err` 会丢掉「哪个 filter、哪个 pattern」的上下文。上层拿到的是 `create fail: error
parsing regexp: missing closing ]: `[``,运维定位时还得回去翻配置。
建议 `errors.Wrapf(err, "invalid uri_regex %q",
factory.cfg.UriRegex[0])`,让日志里直接带出非法表达式。
另外确认一点:**只编译 `UriRegex[0]` 是对的**。`UriRegex[1]` 是 `ReplaceAllString`
的替换串而不是正则,里面出现 `$1`/`$name` 是合法的,不应该一起编译。这一点 PR
处理正确,建议在代码里补一句注释说明,避免后来人「对称地」把第二个也编译掉。
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]