Alanxtl commented on code in PR #26:
URL:
https://github.com/apache/dubbo-go-extensions/pull/26#discussion_r3793545294
##########
filter/adaptivesvc/README_CN.md:
##########
@@ -0,0 +1,90 @@
+# 自适应服务限流
+
+[English](./README.md) | 简体中文
+
+dubbo-go 自适应服务限流扩展。该扩展包含提供者侧 adaptive service filter、消费者侧 adaptive service
cluster、P2C 负载均衡,以及 P2C 使用的本地方法级指标。
+
+该扩展保留当前 dubbo-go 内置实现的 key 和行为,同时允许应用显式依赖
`github.com/apache/dubbo-go-extensions`。
+
+## 安装
+
+```bash
+go get github.com/apache/dubbo-go-extensions/imports/adaptivesvc
Review Comment:
真的有这个路径吗 难道不是
```
github.com/apache/dubbo-go-extensions/filter/adaptivesvc
```
##########
internal/adaptivesvc/adaptivesvc.go:
##########
Review Comment:
为什么要引入internal这个路径
##########
filter/adaptivesvc/README_CN.md:
##########
@@ -0,0 +1,90 @@
+# 自适应服务限流
+
+[English](./README.md) | 简体中文
+
+dubbo-go 自适应服务限流扩展。该扩展包含提供者侧 adaptive service filter、消费者侧 adaptive service
cluster、P2C 负载均衡,以及 P2C 使用的本地方法级指标。
+
+该扩展保留当前 dubbo-go 内置实现的 key 和行为,同时允许应用显式依赖
`github.com/apache/dubbo-go-extensions`。
+
+## 安装
+
+```bash
+go get github.com/apache/dubbo-go-extensions/imports/adaptivesvc
+```
+
+导入 `github.com/apache/dubbo-go-extensions/imports/adaptivesvc`,即可同时注册
+provider filter、consumer cluster 和 P2C loadbalance。
+
+## 提供者侧
+
+聚合导入会注册 provider filter;也可以单独通过副作用导入注册:
+
+```go
+import (
+ _ "github.com/apache/dubbo-go-extensions/filter/adaptivesvc"
+)
+```
+
+配置 provider filter key:
+
+```text
+padasvc
+```
+
+provider filter 只在 invocation 携带 `adaptive-service.enabled=1` 时执行限流。启用后,它会使用
hill-climbing limiter,并通过响应 attachment 返回 provider 状态:
+
+- `adaptive-service.remaining`
+- `adaptive-service.inflight`
+
+## 消费者侧
+
+聚合导入会注册 adaptive service cluster 和 P2C loadbalance;也可以单独通过副作用导入注册:
+
+```go
+import (
+ _ "github.com/apache/dubbo-go-extensions/cluster/cluster/adaptivesvc"
+ _ "github.com/apache/dubbo-go-extensions/cluster/loadbalance/p2c"
+)
+```
Review Comment:
这几个路径也不存在啊 整个文档你再确认一下
##########
filter/adaptivesvc/README_CN.md:
##########
@@ -0,0 +1,90 @@
+# 自适应服务限流
+
+[English](./README.md) | 简体中文
+
+dubbo-go 自适应服务限流扩展。该扩展包含提供者侧 adaptive service filter、消费者侧 adaptive service
cluster、P2C 负载均衡,以及 P2C 使用的本地方法级指标。
+
+该扩展保留当前 dubbo-go 内置实现的 key 和行为,同时允许应用显式依赖
`github.com/apache/dubbo-go-extensions`。
+
+## 安装
+
+```bash
+go get github.com/apache/dubbo-go-extensions/imports/adaptivesvc
+```
+
+导入 `github.com/apache/dubbo-go-extensions/imports/adaptivesvc`,即可同时注册
+provider filter、consumer cluster 和 P2C loadbalance。
+
+## 提供者侧
+
+聚合导入会注册 provider filter;也可以单独通过副作用导入注册:
+
+```go
+import (
+ _ "github.com/apache/dubbo-go-extensions/filter/adaptivesvc"
+)
+```
+
+配置 provider filter key:
+
+```text
+padasvc
+```
+
Review Comment:
这是啥
##########
go.mod:
##########
@@ -3,10 +3,13 @@ module github.com/apache/dubbo-go-extensions
go 1.25.0
require (
- dubbo.apache.org/dubbo-go/v3 v3.3.1
+ dubbo.apache.org/dubbo-go/v3 v3.3.2
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5
github.com/dubbogo/gost v1.14.3
+ github.com/golang/mock v1.6.0
+ github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.11.1
+ go.uber.org/atomic v1.10.0
Review Comment:
尽量都更新到最新的版本
indirect的也是
##########
filter/adaptivesvc/limiter/hill_climbing.go:
##########
@@ -0,0 +1,332 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package limiter
+
+import (
+ "math"
+ "sync"
+ "time"
+)
+
+import (
+ "go.uber.org/atomic"
+)
+
+var (
+ _ Limiter = (*HillClimbing)(nil)
+ _ Updater = (*HillClimbingUpdater)(nil)
+)
+
+type HillClimbingOption int64
+
+const (
+ HillClimbingOptionShrinkPlus HillClimbingOption = -2
+ HillClimbingOptionShrink HillClimbingOption = -1
+ HillClimbingOptionDoNothing HillClimbingOption = 0
+ HillClimbingOptionExtend HillClimbingOption = 1
+ HillClimbingOptionExtendPlus HillClimbingOption = 2
+)
Review Comment:
为什么不用iota
##########
filter/adaptivesvc/README_CN.md:
##########
@@ -0,0 +1,90 @@
+# 自适应服务限流
+
+[English](./README.md) | 简体中文
+
+dubbo-go 自适应服务限流扩展。该扩展包含提供者侧 adaptive service filter、消费者侧 adaptive service
cluster、P2C 负载均衡,以及 P2C 使用的本地方法级指标。
+
+该扩展保留当前 dubbo-go 内置实现的 key 和行为,同时允许应用显式依赖
`github.com/apache/dubbo-go-extensions`。
+
+## 安装
+
+```bash
+go get github.com/apache/dubbo-go-extensions/imports/adaptivesvc
+```
+
+导入 `github.com/apache/dubbo-go-extensions/imports/adaptivesvc`,即可同时注册
+provider filter、consumer cluster 和 P2C loadbalance。
+
+## 提供者侧
+
+聚合导入会注册 provider filter;也可以单独通过副作用导入注册:
+
+```go
+import (
+ _ "github.com/apache/dubbo-go-extensions/filter/adaptivesvc"
+)
+```
+
+配置 provider filter key:
+
+```text
+padasvc
+```
+
+provider filter 只在 invocation 携带 `adaptive-service.enabled=1` 时执行限流。启用后,它会使用
hill-climbing limiter,并通过响应 attachment 返回 provider 状态:
+
+- `adaptive-service.remaining`
+- `adaptive-service.inflight`
+
+## 消费者侧
+
+聚合导入会注册 adaptive service cluster 和 P2C loadbalance;也可以单独通过副作用导入注册:
+
+```go
+import (
+ _ "github.com/apache/dubbo-go-extensions/cluster/cluster/adaptivesvc"
+ _ "github.com/apache/dubbo-go-extensions/cluster/loadbalance/p2c"
+)
+```
+
+消费者侧配置:
+
+- cluster: `adaptiveService`
+- loadbalance: `p2c`
+
+consumer cluster 会在发出的 invocation 上设置 `adaptive-service.enabled=1`。provider
filter 根据该 attachment 判断是否限流并回传容量。consumer cluster 读取 provider 返回的
`adaptive-service.remaining` attachment,写入本地方法级 metrics。P2C loadbalance 随后在两个候选
provider 中选择 hill-climbing remaining capacity 更高的节点。
+
+adaptive service cluster 当前只支持 `p2c` loadbalance。
+
+## Key 列表
+
+- Provider filter: `padasvc`
+- Cluster: `adaptiveService`
+- Loadbalance: `p2c`
+- Metrics key: `hill-climbing`
+- 启用 attachment: `adaptive-service.enabled`
+- 启用值: `1`
+- Remaining attachment: `adaptive-service.remaining`
+- Inflight attachment: `adaptive-service.inflight`
+
+## Verbose 日志
+
+limiter 暴露了包级别的 `Verbose` 开关,可用于开启 debug 日志:
+
+```go
+import "github.com/apache/dubbo-go-extensions/filter/adaptivesvc/limiter"
+
+func init() {
+ limiter.Verbose = true
+}
+```
+
+## 兼容性
+
+迁移后 adaptive service 的实现由本 extensions 模块注册。使用迁移后能力的应用应导入上面的聚合包。注册的 key 为:
Review Comment:
怎么注册这个事情有待商榷
##########
filter/adaptivesvc/README_CN.md:
##########
Review Comment:
限流的效果也在readme里面简要展示一下
##########
filter/adaptivesvc/limiter/hill_climbing.go:
##########
@@ -0,0 +1,332 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package limiter
+
+import (
+ "math"
+ "sync"
+ "time"
+)
+
+import (
+ "go.uber.org/atomic"
+)
+
+var (
+ _ Limiter = (*HillClimbing)(nil)
+ _ Updater = (*HillClimbingUpdater)(nil)
+)
+
+type HillClimbingOption int64
+
+const (
+ HillClimbingOptionShrinkPlus HillClimbingOption = -2
+ HillClimbingOptionShrink HillClimbingOption = -1
+ HillClimbingOptionDoNothing HillClimbingOption = 0
+ HillClimbingOptionExtend HillClimbingOption = 1
+ HillClimbingOptionExtendPlus HillClimbingOption = 2
+)
Review Comment:
所有的const都放到dubbo-go-extensions/filter/adaptivesvc里面新建一个const.go文件吧
比如internal/adaptivesvc/adaptivesvc.go定义的const就可以移进去
##########
filter/adaptivesvc/limiter/hill_climbing.go:
##########
@@ -0,0 +1,332 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package limiter
+
+import (
+ "math"
+ "sync"
+ "time"
+)
+
+import (
+ "go.uber.org/atomic"
+)
+
+var (
+ _ Limiter = (*HillClimbing)(nil)
+ _ Updater = (*HillClimbingUpdater)(nil)
+)
+
+type HillClimbingOption int64
+
+const (
+ HillClimbingOptionShrinkPlus HillClimbingOption = -2
+ HillClimbingOptionShrink HillClimbingOption = -1
+ HillClimbingOptionDoNothing HillClimbingOption = 0
+ HillClimbingOptionExtend HillClimbingOption = 1
+ HillClimbingOptionExtendPlus HillClimbingOption = 2
+)
+
+var (
+ initialLimitation uint64 = 50
+ maxLimitation uint64 = 500
+
+ radicalPeriod = 1000 * time.Millisecond
+ stablePeriod = 32000 * time.Millisecond
+)
+
+// HillClimbing is a limiter using HillClimbing algorithm
+type HillClimbing struct {
+ seq *atomic.Uint64
+ round *atomic.Uint64
+
+ inflight *atomic.Uint64
+ limitation *atomic.Uint64
+
+ mutex *sync.Mutex
+ // nextUpdateTime = lastUpdatedTime + updateInterval
+ updateInterval *atomic.Duration
+ lastUpdatedTime *atomic.Time
+
+ // metrics of the current round
+ transactionNum *atomic.Uint64
+ rttAvg *atomic.Float64
+
+ // best metrics in the history
+ bestMaxCapacity *atomic.Float64
+ bestRTTAvg *atomic.Float64
+ bestLimitation *atomic.Uint64
+ bestTPS *atomic.Uint64
+}
+
+func NewHillClimbing() Limiter {
+ l := &HillClimbing{
+ seq: new(atomic.Uint64),
+ round: new(atomic.Uint64),
+ inflight: new(atomic.Uint64),
+ limitation: atomic.NewUint64(initialLimitation),
+ mutex: new(sync.Mutex),
+ updateInterval: atomic.NewDuration(radicalPeriod),
+ lastUpdatedTime: atomic.NewTime(time.Now()),
+ transactionNum: new(atomic.Uint64),
+ rttAvg: new(atomic.Float64),
+ bestMaxCapacity: new(atomic.Float64),
+ bestRTTAvg: atomic.NewFloat64(math.MaxFloat64),
+ bestLimitation: new(atomic.Uint64),
+ bestTPS: new(atomic.Uint64),
+ }
+
+ return l
+}
+
+func (l *HillClimbing) Inflight() uint64 {
+ return l.inflight.Load()
+}
+
+func (l *HillClimbing) Remaining() uint64 {
+ limitation := l.limitation.Load()
+ inflight := l.Inflight()
+ if limitation < inflight {
+ return 0
+ }
+ return limitation - inflight
+}
+
+func (l *HillClimbing) Acquire() (Updater, error) {
+ if l.Remaining() == 0 {
+ return nil, ErrReachLimitation
+ }
+ return NewHillClimbingUpdater(l), nil
+}
+
+type HillClimbingUpdater struct {
+ startTime time.Time
+ limiter *HillClimbing
+
+ // for debug purposes
+ seq uint64
+}
+
+func NewHillClimbingUpdater(limiter *HillClimbing) *HillClimbingUpdater {
+ inflight := limiter.inflight.Add(1)
+ u := &HillClimbingUpdater{
+ startTime: time.Now(),
+ limiter: limiter,
+ seq: limiter.seq.Add(1) - 1,
+ }
+ VerboseDebugf("[NewHillClimbingUpdater] A new request arrived, seq: %d,
inflight: %d, time: %s.",
Review Comment:
为什么要用Verbose 为什么不直接用我们gost的logger
--
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]