You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by "Aetherance (via GitHub)" <gi...@apache.org> on 2025/11/23 11:59:14 UTC
[PR] feat(triple): implement TriClientPool [dubbo-go]
Aetherance opened a new pull request, #3086:
URL: https://github.com/apache/dubbo-go/pull/3086
为 triple client 实现了一个 triple client pool
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] refactor(triple): upgrade triple client to service-level abstraction [dubbo-go]
Posted by "AlexStocks (via GitHub)" <gi...@apache.org>.
AlexStocks commented on code in PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#discussion_r2704421621
##########
protocol/triple/triple_protocol/client.go:
##########
@@ -36,7 +36,7 @@ type TimeoutKey struct{}
// use the [WithTriple] options.
type Client struct {
config *clientConfig
- callUnary func(context.Context, *Request, *Response) error
+ callUnary func(context.Context, *Request, *Response, string) error
Review Comment:
两个问题:
1 string method 能否放进 Request?酱紫就可以不用添加参数了
2 如果不能添加进 Request, string method 放在 Request 后面比较合适,不适宜放在最后。
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] refactor(triple): upgrade triple client to service-level abstraction [dubbo-go]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3768234912
## [](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086) **Quality Gate passed**
Issues
 [0 New issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=ACCEPTED)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_coverage&view=list)
 [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_duplicated_lines_density&view=list)
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086)
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "Aetherance (via GitHub)" <gi...@apache.org>.
Aetherance commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3569346420
This PR is still in progress. I’ll update it once it's ready for review.
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3619713699
## [](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086) **Quality Gate failed**
Failed conditions
 [D Security Rating on New Code](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086) (required ≥ A)
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086)
##
 Catch issues before they fail your Quality Gate with our IDE extension  [SonarQube for IDE](https://www.sonarsource.com/products/sonarlint/features/connected-mode/?referrer=pull-request)
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): Implement TriClientPool and Delay triple client Method Binding [dubbo-go]
Posted by "AlexStocks (via GitHub)" <gi...@apache.org>.
AlexStocks commented on code in PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#discussion_r2616839780
##########
protocol/triple/client.go:
##########
@@ -57,30 +57,57 @@ const (
// callUnary, callClientStream, callServerStream, callBidiStream.
// A Reference has a clientManager.
type clientManager struct {
- isIDL bool
- // triple_protocol clients, key is method name
- triClients map[string]*tri.Client
+ isIDL bool
+ triGuard *sync.RWMutex
+ // triple_protocol clients
+ triClients *TriClientPool
}
// TODO: code a triple client between clientManager and triple_protocol client
// TODO: write a NewClient for triple client
func (cm *clientManager) getClient(method string) (*tri.Client, error) {
- triClient, ok := cm.triClients[method]
- if !ok {
+ cm.triGuard.RLock()
+ if cm.triClients == nil {
+ cm.triGuard.RUnlock()
+ return nil, fmt.Errorf("no client pool found for method: %s", method)
+ }
+ triClient, err := cm.triClients.Get(constant.DefaultClientGetTimeout)
+ cm.triGuard.RUnlock()
+ if triClient == nil {
+ return nil, fmt.Errorf("missing triple client for method: %s", method)
+ }
+ // Wait up to defaultClientGetTimeout for a pooled client; if none becomes available we return the
+ // fallback client and record a timeout which is used to drive autoscaling.
+ if err != nil {
return nil, fmt.Errorf("missing triple client for method: %s", method)
}
return triClient, nil
}
+func (cm *clientManager) putClient(method string, c *tri.Client) {
+ if c == nil {
+ return
+ }
+
+ if cm.triClients == nil {
Review Comment:
没看明白,上面 line 71 处,cm.triClients 在 lock scope 之内,为何这里不在 lock scope 之内呢?
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3584272417
## [](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086) **Quality Gate passed**
Issues
 [1 New issue](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=ACCEPTED)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_coverage&view=list)
 [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_duplicated_lines_density&view=list)
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086)
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "Aetherance (via GitHub)" <gi...@apache.org>.
Aetherance commented on code in PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#discussion_r2603655442
##########
tools/dubbogo-cli/generator/sample/hessian/generator.go:
##########
@@ -99,6 +99,13 @@ func scanFile(filePath string) (file *fileInfo, err error) {
stack := make([][]byte, 0)
var line []byte
var lineSize int
+ var (
+ packageRegexp = regexp.MustCompile(PackageRegexp)
+ initFunctionRegexp = regexp.MustCompile(InitFunctionRegexp)
+ hessianImportRegexp = regexp.MustCompile(HessianImportRegexp)
+ lineCommentRegexp = regexp.MustCompile(LineCommentRegexp)
+ hessianPOJORegexp = regexp.MustCompile(HessianPOJORegexp)
+ )
Review Comment:
fixed
##########
protocol/triple/client.go:
##########
@@ -59,25 +63,40 @@ const (
type clientManager struct {
isIDL bool
// triple_protocol clients, key is method name
Review Comment:
done
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): Implement TriClientPool and Delay triple client Method Binding [dubbo-go]
Posted by "Aetherance (via GitHub)" <gi...@apache.org>.
Aetherance commented on code in PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#discussion_r2634713460
##########
protocol/triple/client_pool.go:
##########
@@ -0,0 +1,312 @@
+/*
+ * 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 triple
+
+import (
+ "errors"
+ "sync"
+ "time"
+)
+
+import (
+ "dubbo.apache.org/dubbo-go/v3/common/constant"
+ tri "dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol"
+)
+
+type ClientPool interface {
+ Get(timeout time.Duration) (*tri.Client, error)
+ Put(client *tri.Client)
+ Close()
+
+ MaxSize() int
+ Closed() bool
+}
+
+var (
+ ErrTriClientPoolClosed = errors.New("tri client pool is closed")
+ ErrTriClientPoolCloseWhenEmpty = errors.New("empty tri client pool close")
+)
+
+type TriClientPool struct {
+ clients chan *tri.Client
+ factory func() *tri.Client
+ mu sync.Mutex
+ maxSize int
+ curSize int
+ closed bool
+ getTimeouts int // recent timeout count, used to trigger expansion
+ lastScaleTime time.Time
+
+ fallback *tri.Client
+ consecutiveHighIdle int
+}
+
+// NewTriClientPool creates a new TriClientPool with optional warm-up.
+// warmUpSize specifies how many clients to pre-create and add to the pool.
+// If warmUpSize is 0, no warm-up is performed.
+// If warmUpSize > maxSize, it will be capped to maxSize.
+func NewTriClientPool(warmUpSize, maxSize int, factory func() *tri.Client) *TriClientPool {
+ if warmUpSize > maxSize {
+ warmUpSize = maxSize
+ }
+ if warmUpSize < 0 {
+ warmUpSize = 0
+ }
+
+ pool := &TriClientPool{
+ clients: make(chan *tri.Client, maxSize),
+ factory: factory,
+ maxSize: maxSize,
+ curSize: warmUpSize,
+ }
+
+ for i := 0; i < warmUpSize; i++ {
+ cli := factory()
+ select {
+ case pool.clients <- cli:
+ default:
+ pool.curSize--
+ }
+ }
+
+ return pool
+}
+
+// TriClient Get method
+// timeout means how long to wait for an available client.
+// TriClientPool keeps a fallback client pointer. If Get() times out,
+// and the pool cannot expand at that moment, the pool will return fallback.
+// This ensures there is at least one usable client.
+// Get tries a non-blocking receive first. If that fails, it tries to expand.
+// If expansion is not allowed, it waits for a client up to timeout.
+// After timeout, if still no client, Get() returns ErrTimeout with fallback.
+func (p *TriClientPool) Get(timeout time.Duration) (*tri.Client, error) {
+ now := time.Now()
+ if now.Sub(p.lastScaleTime) > constant.AutoScalerPeriod {
+ p.autoScaler()
+ }
+
+ select {
+ case cli := <-p.clients:
+ return cli, nil
+ default:
+ }
+
+ p.mu.Lock()
+ if p.closed {
+ p.mu.Unlock()
+ return nil, ErrTriClientPoolClosed
+ }
+ // try to expand
+ if p.curSize < p.maxSize {
+ p.curSize++
+ p.mu.Unlock()
+ cli := p.factory()
+ return cli, nil
+ }
+ p.mu.Unlock()
+
+ select {
+ case cli, ok := <-p.clients:
+ if !ok {
+ return nil, ErrTriClientPoolClosed
+ }
+ return cli, nil
+ case <-time.After(timeout):
+ p.recordTimeout()
+ p.mu.Lock()
+ if p.fallback == nil {
+ p.fallback = p.factory()
+ }
+ p.mu.Unlock()
+ return p.fallback, nil
+ }
+}
+
+// TriClient Put method
+// Put tries to put a tri.Client back into the pool.
+// If it fails, Put will drop the client and notify the pool.
+// Dropping a client is part of shrinking.
+func (p *TriClientPool) Put(c *tri.Client) {
+ now := time.Now()
+ if now.Sub(p.lastScaleTime) > constant.AutoScalerPeriod {
+ p.autoScaler()
+ }
+
+ if c == nil {
+ return
+ }
+
+ p.mu.Lock()
+ closed := p.closed
+ p.mu.Unlock()
+ if closed {
+ return
+ }
+
+ select {
+ case p.clients <- c:
+ default:
+ p.mu.Lock()
+ p.curSize--
+ p.mu.Unlock()
+ }
+}
+
+// close removes all clients from the channel and then closes it.
+func (p *TriClientPool) Close() {
+ p.mu.Lock()
+ if p.closed {
+ p.mu.Unlock()
+ return
+ }
+ p.closed = true
+
+ for len(p.clients) > 0 {
+ <-p.clients
+ p.curSize--
+ }
+
+ close(p.clients)
+ p.mu.Unlock()
+}
+
+func (p *TriClientPool) MaxSize() int {
+ return p.maxSize
+}
+
+func (p *TriClientPool) Closed() bool {
+ return p.closed
+}
+
+// autoScaler performs scaling check.
+// It is triggered by Get() or Put()
+// If the timeout count is high, autoScaler tends to expand.
+// If the idle client count is often high, autoScaler tends to shrink.
+func (p *TriClientPool) autoScaler() {
+ p.mu.Lock()
+
+ now := time.Now()
+ if now.Sub(p.lastScaleTime) < constant.AutoScalerPeriod {
+ p.mu.Unlock()
+ return
+ }
+
+ if p.closed {
+ p.mu.Unlock()
+ return
+ }
+
+ p.lastScaleTime = now
+
+ curSize := p.curSize
+ idle := len(p.clients)
+ timeouts := p.getTimeouts
+ p.getTimeouts = 0
+
+ needExpand := checkExpand(curSize, idle, timeouts)
+ if needExpand != 0 {
+ p.consecutiveHighIdle = 0
+ p.mu.Unlock()
+ p.expand(needExpand)
+ return
+ }
+
+ needShrink := checkShrink(curSize, idle, &p.consecutiveHighIdle)
+ p.mu.Unlock()
+ if needShrink != 0 {
+ p.shrink(needShrink)
+ }
+}
Review Comment:
这里 expand 和 shrink 内部都要加锁,使用 defer 可能会让锁的管理变得更加混乱
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): Implement TriClientPool and Delay triple client Method Binding [dubbo-go]
Posted by "Alanxtl (via GitHub)" <gi...@apache.org>.
Alanxtl commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3688504173
benchmark 能不能加 大流量 和 并发量高一些 的场景,看一下这两种场景中修改前后的benchmark结果
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "Aetherance (via GitHub)" <gi...@apache.org>.
Aetherance commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3589415032
@marsevilspirit done
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): Implement TriClientPool and Delay triple client Method Binding [dubbo-go]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3733430946
## [](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086) **Quality Gate passed**
Issues
 [0 New issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=ACCEPTED)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_coverage&view=list)
 [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_duplicated_lines_density&view=list)
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086)
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3630653484
## [](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086) **Quality Gate passed**
Issues
 [0 New issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=ACCEPTED)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_coverage&view=list)
 [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_duplicated_lines_density&view=list)
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086)
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): Implement TriClientPool and Delay triple client Method Binding [dubbo-go]
Posted by "Aetherance (via GitHub)" <gi...@apache.org>.
Aetherance commented on code in PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#discussion_r2632072972
##########
protocol/triple/client_pool.go:
##########
@@ -0,0 +1,312 @@
+/*
+ * 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 triple
+
+import (
+ "errors"
+ "sync"
+ "time"
+)
+
+import (
+ "dubbo.apache.org/dubbo-go/v3/common/constant"
+ tri "dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol"
+)
+
+type ClientPool interface {
+ Get(timeout time.Duration) (*tri.Client, error)
+ Put(client *tri.Client)
+ Close()
+
+ MaxSize() int
+ Closed() bool
+}
+
+var (
+ ErrTriClientPoolClosed = errors.New("tri client pool is closed")
+ ErrTriClientPoolCloseWhenEmpty = errors.New("empty tri client pool close")
+)
+
+type TriClientPool struct {
+ clients chan *tri.Client
+ factory func() *tri.Client
+ mu sync.Mutex
+ maxSize int
+ curSize int
+ closed bool
+ getTimeouts int // recent timeout count, used to trigger expansion
+ lastScaleTime time.Time
+
+ fallback *tri.Client
+ consecutiveHighIdle int
+}
+
+// NewTriClientPool creates a new TriClientPool with optional warm-up.
+// warmUpSize specifies how many clients to pre-create and add to the pool.
+// If warmUpSize is 0, no warm-up is performed.
+// If warmUpSize > maxSize, it will be capped to maxSize.
+func NewTriClientPool(warmUpSize, maxSize int, factory func() *tri.Client) *TriClientPool {
+ if warmUpSize > maxSize {
+ warmUpSize = maxSize
+ }
+ if warmUpSize < 0 {
+ warmUpSize = 0
+ }
+
+ pool := &TriClientPool{
+ clients: make(chan *tri.Client, maxSize),
+ factory: factory,
+ maxSize: maxSize,
+ curSize: warmUpSize,
+ }
+
+ for i := 0; i < warmUpSize; i++ {
+ cli := factory()
+ select {
+ case pool.clients <- cli:
+ default:
+ pool.curSize--
+ }
+ }
+
+ return pool
+}
+
+// TriClient Get method
+// timeout means how long to wait for an available client.
+// TriClientPool keeps a fallback client pointer. If Get() times out,
+// and the pool cannot expand at that moment, the pool will return fallback.
+// This ensures there is at least one usable client.
+// Get tries a non-blocking receive first. If that fails, it tries to expand.
+// If expansion is not allowed, it waits for a client up to timeout.
+// After timeout, if still no client, Get() returns ErrTimeout with fallback.
+func (p *TriClientPool) Get(timeout time.Duration) (*tri.Client, error) {
+ now := time.Now()
+ if now.Sub(p.lastScaleTime) > constant.AutoScalerPeriod {
+ p.autoScaler()
+ }
+
+ select {
+ case cli := <-p.clients:
+ return cli, nil
+ default:
+ }
+
+ p.mu.Lock()
+ if p.closed {
+ p.mu.Unlock()
+ return nil, ErrTriClientPoolClosed
+ }
+ // try to expand
+ if p.curSize < p.maxSize {
+ p.curSize++
+ p.mu.Unlock()
+ cli := p.factory()
+ return cli, nil
+ }
+ p.mu.Unlock()
+
+ select {
+ case cli, ok := <-p.clients:
+ if !ok {
+ return nil, ErrTriClientPoolClosed
+ }
+ return cli, nil
+ case <-time.After(timeout):
+ p.recordTimeout()
+ p.mu.Lock()
+ if p.fallback == nil {
+ p.fallback = p.factory()
+ }
+ p.mu.Unlock()
+ return p.fallback, nil
+ }
+}
+
+// TriClient Put method
+// Put tries to put a tri.Client back into the pool.
+// If it fails, Put will drop the client and notify the pool.
+// Dropping a client is part of shrinking.
+func (p *TriClientPool) Put(c *tri.Client) {
+ now := time.Now()
+ if now.Sub(p.lastScaleTime) > constant.AutoScalerPeriod {
+ p.autoScaler()
+ }
+
+ if c == nil {
+ return
+ }
+
+ p.mu.Lock()
+ closed := p.closed
+ p.mu.Unlock()
+ if closed {
+ return
+ }
+
+ select {
+ case p.clients <- c:
+ default:
+ p.mu.Lock()
+ p.curSize--
+ p.mu.Unlock()
+ }
Review Comment:
这块看起来逻辑没什么问题 不过确实有很大优化的空间 已按照要求使用 defer 并且优化了代码
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "Alanxtl (via GitHub)" <gi...@apache.org>.
Alanxtl commented on code in PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#discussion_r2587276447
##########
protocol/triple/client_pool.go:
##########
@@ -0,0 +1,296 @@
+/*
+ * 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 triple
+
+import (
+ "errors"
+ "sync"
+ "time"
+)
+
+import (
+ tri "dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol"
+)
+
+type ClientPool interface {
+ Get(timeout time.Duration) (*tri.Client, error)
+ Put(client *tri.Client)
+ Close()
+
+ MaxSize() int
+ Closed() bool
+}
+
+var (
+ ErrTriClientPoolClosed = errors.New("tri client pool is closed")
+ ErrTriClientPoolTimeout = errors.New("tri client pool get timeout")
+ ErrTriClientPoolCloseWhenEmpty = errors.New("empty tri client pool close")
+)
+
+const (
+ autoScalerPeriod = 700 * time.Millisecond // minimum period that autoScaler expand or shrink
+ maxExpandPerCycle = 16 // maximum number of clients to expand per cycle
+ highIdleStreakLimit = 10 // consecutive high idle count to trigger shrinking
+ lowIdleThreshold = 0.2 // treat pool as busy if idle clients < 20% of total
+ idleShrinkThreshold = 0.6 // shrink counter increases when idle ratio > 60%
+ shrinkRatio = 0.125 // shrink by 12.5% of current size when threshold is reached
+)
Review Comment:
same
##########
protocol/triple/client.go:
##########
@@ -51,6 +51,10 @@ import (
const (
httpPrefix string = "http://"
httpsPrefix string = "https://"
+ // triClientPoolMaxSize is the maximum number of tri.Client instances per method in the pool.
+ triClientPoolMaxSize = 32
+ // defaultClientGetTimeout is the timeout for getting a tri.Client from the TriClientPool.
+ defaultClientGetTimeout = 50 * time.Millisecond
Review Comment:
put these in `common/constant` and import here
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "github-advanced-security[bot] (via GitHub)" <gi...@apache.org>.
github-advanced-security[bot] commented on code in PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#discussion_r2594640724
##########
protocol/triple/client_pool_test.go:
##########
@@ -0,0 +1,293 @@
+/*
+ * 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 triple
+
+import (
+ "context"
+ "crypto/tls"
+ "fmt"
+ "io"
+ "log"
+ "net/http"
+ "net/http/httptest"
+ "sync/atomic"
+ "testing"
+ "time"
+)
+
+import (
+ "dubbo.apache.org/dubbo-go/v3/common/constant"
+ tri "dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol"
+)
+
+// TriClientPool Unit Tests
+// Test if TriClientPool can warm up correctly
+func TestNewTriClientPoolWarmUp(t *testing.T) {
+ var created int32
+ factory := func() *tri.Client {
+ atomic.AddInt32(&created, 1)
+ return &tri.Client{}
+ }
+
+ const warmUpSize = 5
+ const maxSize = 10
+
+ pool := NewTriClientPool(warmUpSize, maxSize, factory)
+ defer pool.Close()
+
+ if got := pool.MaxSize(); got != maxSize {
+ t.Fatalf("MaxSize() = %d, want %d", got, maxSize)
+ }
+ if got := len(pool.clients); got != warmUpSize {
+ t.Fatalf("len(pool.clients) = %d, want %d", got, warmUpSize)
+ }
+ if got := atomic.LoadInt32(&created); got != warmUpSize {
+ t.Fatalf("created clients = %d, want %d", got, warmUpSize)
+ }
+}
+
+// Test if TriClientPool can get and put clients correctly
+func TestTriClientPoolGetAndPutBasic(t *testing.T) {
+ var created int32
+ factory := func() *tri.Client {
+ atomic.AddInt32(&created, 1)
+ return &tri.Client{}
+ }
+
+ pool := NewTriClientPool(1, 1, factory)
+ defer pool.Close()
+
+ cli, err := pool.Get(10 * time.Millisecond)
+ if err != nil {
+ t.Fatalf("Get() error = %v, want nil", err)
+ }
+ if cli == nil {
+ t.Fatalf("Get() returned nil client")
+ }
+ if got := atomic.LoadInt32(&created); got != 1 {
+ t.Fatalf("created clients = %d, want 1", got)
+ }
+
+ pool.Put(cli)
+ if got := len(pool.clients); got != 1 {
+ t.Fatalf("len(pool.clients) after Put = %d, want 1", got)
+ }
+}
+
+// Test if TriClientPool can get timeout fallback correctly
+func TestTriClientPoolGetTimeoutFallback(t *testing.T) {
+ var created int32
+ factory := func() *tri.Client {
+ atomic.AddInt32(&created, 1)
+ return &tri.Client{}
+ }
+
+ pool := NewTriClientPool(0, 1, factory)
+ defer pool.Close()
+
+ cli, err := pool.Get(10 * time.Millisecond)
+ if err != nil {
+ t.Fatalf("first Get() error = %v, want nil", err)
+ }
+ if cli == nil {
+ t.Fatalf("first Get() returned nil client")
+ }
+
+ start := time.Now()
+ fallback, err := pool.Get(5 * time.Millisecond)
+ elapsed := time.Since(start)
+ if err != ErrTriClientPoolTimeout {
+ t.Fatalf("second Get() error = %v, want ErrTriClientPoolTimeout", err)
+ }
+ if fallback == nil {
+ t.Fatalf("second Get() returned nil fallback client")
+ }
+ if pool.fallback != fallback {
+ t.Fatalf("fallback client mismatch: Get() returned %p, pool.fallback = %p", fallback, pool.fallback)
+ }
+ if elapsed < 5*time.Millisecond {
+ t.Fatalf("Get() returned before timeout elapsed: got %v, want >= 5ms", elapsed)
+ }
+ if got := atomic.LoadInt32(&created); got != 2 {
+ t.Fatalf("created clients = %d, want 2 (one normal + one fallback)", got)
+ }
+}
+
+// Test if TriClientPool can close correctly
+func TestTriClientPoolClose(t *testing.T) {
+ pool := NewTriClientPool(1, 1, func() *tri.Client {
+ return &tri.Client{}
+ })
+
+ if pool.Closed() {
+ t.Fatalf("pool should not be closed immediately after creation")
+ }
+
+ pool.Close()
+
+ if !pool.Closed() {
+ t.Fatalf("pool should be closed after Close()")
+ }
+
+ cli, err := pool.Get(1 * time.Millisecond)
+ if cli != nil {
+ t.Fatalf("Get() after Close() should return nil client, got %v", cli)
+ }
+ if err != nil && err != ErrTriClientPoolClosed {
+ t.Fatalf("Get() after Close() error = %v, want nil or ErrTriClientPoolClosed", err)
+ }
+}
+
+// Test if TriClientPool can check expand and shrink correctly
+func TestCheckExpandAndShrink(t *testing.T) {
+ if got := checkExpand(10, 0, 1); got != 2 {
+ t.Fatalf("checkExpand(curSize=10,idle=0,timeouts=1) = %d, want 2", got)
+ }
+ if got := checkExpand(10, 0, 10); got != constant.MaxExpandPerCycle {
+ t.Fatalf("checkExpand(..., timeouts=10) = %d, want maxExpandPerCycle(%d)", got, constant.MaxExpandPerCycle)
+ }
+
+ if got := checkExpand(10, 1, 0); got != 1 {
+ t.Fatalf("checkExpand(curSize=10,idle=1,timeouts=0) = %d, want 1", got)
+ }
+
+ var streak int
+ for i := 0; i < constant.HighIdleStreakLimit-1; i++ {
+ if got := checkShrink(16, 16, &streak); got != 0 {
+ t.Fatalf("checkShrink iteration %d returned %d, want 0 before reaching limit", i, got)
+ }
+ }
+ if got := checkShrink(16, 16, &streak); got <= 0 {
+ t.Fatalf("checkShrink at highIdleStreakLimit returned %d, want > 0", got)
+ }
+}
+
+// TriClientPool Benchmark
+// Benchmark if TriClientPool with different delays
+
+var (
+ testDelays = []time.Duration{
+ 0,
+ 100 * time.Microsecond,
+ 1 * time.Millisecond,
+ 100 * time.Millisecond,
+ }
+)
+
+// setupTripleServer creates an HTTP/2 test server with configurable delay for benchmarks
+func setupTripleServer(delay time.Duration) (*httptest.Server, string) {
+ mux := http.NewServeMux()
+
+ mux.HandleFunc("/test/Echo", func(w http.ResponseWriter, r *http.Request) {
+ time.Sleep(delay)
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(http.StatusOK)
+ w.Write([]byte(`{"echo":"ok"}`))
+ })
+
+ server := httptest.NewUnstartedServer(mux)
+ server.EnableHTTP2 = true
+ server.StartTLS()
+ return server, server.URL + "/test/Echo"
+}
+
+func benchmarkSingleClient(b *testing.B, testURL string) {
+ b.SetParallelism(10)
+ transport := &http.Transport{
+ TLSClientConfig: &tls.Config{
Review Comment:
## Server certificates should be verified during SSL/TLS connections
<!--SONAR_ISSUE_KEY:AZryrAHtfR9D5s-EniDP-->Enable server certificate validation on this SSL/TLS connection. <p>See more on <a href="https://sonarcloud.io/project/issues?id=apache_dubbo-go&issues=AZryrAHtfR9D5s-EniDP&open=AZryrAHtfR9D5s-EniDP&pullRequest=3086">SonarQube Cloud</a></p>
[Show more details](https://github.com/apache/dubbo-go/security/code-scanning/65)
##########
protocol/triple/client_pool_test.go:
##########
@@ -0,0 +1,293 @@
+/*
+ * 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 triple
+
+import (
+ "context"
+ "crypto/tls"
+ "fmt"
+ "io"
+ "log"
+ "net/http"
+ "net/http/httptest"
+ "sync/atomic"
+ "testing"
+ "time"
+)
+
+import (
+ "dubbo.apache.org/dubbo-go/v3/common/constant"
+ tri "dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol"
+)
+
+// TriClientPool Unit Tests
+// Test if TriClientPool can warm up correctly
+func TestNewTriClientPoolWarmUp(t *testing.T) {
+ var created int32
+ factory := func() *tri.Client {
+ atomic.AddInt32(&created, 1)
+ return &tri.Client{}
+ }
+
+ const warmUpSize = 5
+ const maxSize = 10
+
+ pool := NewTriClientPool(warmUpSize, maxSize, factory)
+ defer pool.Close()
+
+ if got := pool.MaxSize(); got != maxSize {
+ t.Fatalf("MaxSize() = %d, want %d", got, maxSize)
+ }
+ if got := len(pool.clients); got != warmUpSize {
+ t.Fatalf("len(pool.clients) = %d, want %d", got, warmUpSize)
+ }
+ if got := atomic.LoadInt32(&created); got != warmUpSize {
+ t.Fatalf("created clients = %d, want %d", got, warmUpSize)
+ }
+}
+
+// Test if TriClientPool can get and put clients correctly
+func TestTriClientPoolGetAndPutBasic(t *testing.T) {
+ var created int32
+ factory := func() *tri.Client {
+ atomic.AddInt32(&created, 1)
+ return &tri.Client{}
+ }
+
+ pool := NewTriClientPool(1, 1, factory)
+ defer pool.Close()
+
+ cli, err := pool.Get(10 * time.Millisecond)
+ if err != nil {
+ t.Fatalf("Get() error = %v, want nil", err)
+ }
+ if cli == nil {
+ t.Fatalf("Get() returned nil client")
+ }
+ if got := atomic.LoadInt32(&created); got != 1 {
+ t.Fatalf("created clients = %d, want 1", got)
+ }
+
+ pool.Put(cli)
+ if got := len(pool.clients); got != 1 {
+ t.Fatalf("len(pool.clients) after Put = %d, want 1", got)
+ }
+}
+
+// Test if TriClientPool can get timeout fallback correctly
+func TestTriClientPoolGetTimeoutFallback(t *testing.T) {
+ var created int32
+ factory := func() *tri.Client {
+ atomic.AddInt32(&created, 1)
+ return &tri.Client{}
+ }
+
+ pool := NewTriClientPool(0, 1, factory)
+ defer pool.Close()
+
+ cli, err := pool.Get(10 * time.Millisecond)
+ if err != nil {
+ t.Fatalf("first Get() error = %v, want nil", err)
+ }
+ if cli == nil {
+ t.Fatalf("first Get() returned nil client")
+ }
+
+ start := time.Now()
+ fallback, err := pool.Get(5 * time.Millisecond)
+ elapsed := time.Since(start)
+ if err != ErrTriClientPoolTimeout {
+ t.Fatalf("second Get() error = %v, want ErrTriClientPoolTimeout", err)
+ }
+ if fallback == nil {
+ t.Fatalf("second Get() returned nil fallback client")
+ }
+ if pool.fallback != fallback {
+ t.Fatalf("fallback client mismatch: Get() returned %p, pool.fallback = %p", fallback, pool.fallback)
+ }
+ if elapsed < 5*time.Millisecond {
+ t.Fatalf("Get() returned before timeout elapsed: got %v, want >= 5ms", elapsed)
+ }
+ if got := atomic.LoadInt32(&created); got != 2 {
+ t.Fatalf("created clients = %d, want 2 (one normal + one fallback)", got)
+ }
+}
+
+// Test if TriClientPool can close correctly
+func TestTriClientPoolClose(t *testing.T) {
+ pool := NewTriClientPool(1, 1, func() *tri.Client {
+ return &tri.Client{}
+ })
+
+ if pool.Closed() {
+ t.Fatalf("pool should not be closed immediately after creation")
+ }
+
+ pool.Close()
+
+ if !pool.Closed() {
+ t.Fatalf("pool should be closed after Close()")
+ }
+
+ cli, err := pool.Get(1 * time.Millisecond)
+ if cli != nil {
+ t.Fatalf("Get() after Close() should return nil client, got %v", cli)
+ }
+ if err != nil && err != ErrTriClientPoolClosed {
+ t.Fatalf("Get() after Close() error = %v, want nil or ErrTriClientPoolClosed", err)
+ }
+}
+
+// Test if TriClientPool can check expand and shrink correctly
+func TestCheckExpandAndShrink(t *testing.T) {
+ if got := checkExpand(10, 0, 1); got != 2 {
+ t.Fatalf("checkExpand(curSize=10,idle=0,timeouts=1) = %d, want 2", got)
+ }
+ if got := checkExpand(10, 0, 10); got != constant.MaxExpandPerCycle {
+ t.Fatalf("checkExpand(..., timeouts=10) = %d, want maxExpandPerCycle(%d)", got, constant.MaxExpandPerCycle)
+ }
+
+ if got := checkExpand(10, 1, 0); got != 1 {
+ t.Fatalf("checkExpand(curSize=10,idle=1,timeouts=0) = %d, want 1", got)
+ }
+
+ var streak int
+ for i := 0; i < constant.HighIdleStreakLimit-1; i++ {
+ if got := checkShrink(16, 16, &streak); got != 0 {
+ t.Fatalf("checkShrink iteration %d returned %d, want 0 before reaching limit", i, got)
+ }
+ }
+ if got := checkShrink(16, 16, &streak); got <= 0 {
+ t.Fatalf("checkShrink at highIdleStreakLimit returned %d, want > 0", got)
+ }
+}
+
+// TriClientPool Benchmark
+// Benchmark if TriClientPool with different delays
+
+var (
+ testDelays = []time.Duration{
+ 0,
+ 100 * time.Microsecond,
+ 1 * time.Millisecond,
+ 100 * time.Millisecond,
+ }
+)
+
+// setupTripleServer creates an HTTP/2 test server with configurable delay for benchmarks
+func setupTripleServer(delay time.Duration) (*httptest.Server, string) {
+ mux := http.NewServeMux()
+
+ mux.HandleFunc("/test/Echo", func(w http.ResponseWriter, r *http.Request) {
+ time.Sleep(delay)
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(http.StatusOK)
+ w.Write([]byte(`{"echo":"ok"}`))
+ })
+
+ server := httptest.NewUnstartedServer(mux)
+ server.EnableHTTP2 = true
+ server.StartTLS()
+ return server, server.URL + "/test/Echo"
+}
+
+func benchmarkSingleClient(b *testing.B, testURL string) {
+ b.SetParallelism(10)
+ transport := &http.Transport{
+ TLSClientConfig: &tls.Config{
Review Comment:
## Server hostnames should be verified during SSL/TLS connections
<!--SONAR_ISSUE_KEY:AZryrAHtfR9D5s-EniDQ-->Enable server hostname verification on this SSL/TLS connection. <p>See more on <a href="https://sonarcloud.io/project/issues?id=apache_dubbo-go&issues=AZryrAHtfR9D5s-EniDQ&open=AZryrAHtfR9D5s-EniDQ&pullRequest=3086">SonarQube Cloud</a></p>
[Show more details](https://github.com/apache/dubbo-go/security/code-scanning/66)
##########
protocol/triple/client_pool_test.go:
##########
@@ -0,0 +1,293 @@
+/*
+ * 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 triple
+
+import (
+ "context"
+ "crypto/tls"
+ "fmt"
+ "io"
+ "log"
+ "net/http"
+ "net/http/httptest"
+ "sync/atomic"
+ "testing"
+ "time"
+)
+
+import (
+ "dubbo.apache.org/dubbo-go/v3/common/constant"
+ tri "dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol"
+)
+
+// TriClientPool Unit Tests
+// Test if TriClientPool can warm up correctly
+func TestNewTriClientPoolWarmUp(t *testing.T) {
+ var created int32
+ factory := func() *tri.Client {
+ atomic.AddInt32(&created, 1)
+ return &tri.Client{}
+ }
+
+ const warmUpSize = 5
+ const maxSize = 10
+
+ pool := NewTriClientPool(warmUpSize, maxSize, factory)
+ defer pool.Close()
+
+ if got := pool.MaxSize(); got != maxSize {
+ t.Fatalf("MaxSize() = %d, want %d", got, maxSize)
+ }
+ if got := len(pool.clients); got != warmUpSize {
+ t.Fatalf("len(pool.clients) = %d, want %d", got, warmUpSize)
+ }
+ if got := atomic.LoadInt32(&created); got != warmUpSize {
+ t.Fatalf("created clients = %d, want %d", got, warmUpSize)
+ }
+}
+
+// Test if TriClientPool can get and put clients correctly
+func TestTriClientPoolGetAndPutBasic(t *testing.T) {
+ var created int32
+ factory := func() *tri.Client {
+ atomic.AddInt32(&created, 1)
+ return &tri.Client{}
+ }
+
+ pool := NewTriClientPool(1, 1, factory)
+ defer pool.Close()
+
+ cli, err := pool.Get(10 * time.Millisecond)
+ if err != nil {
+ t.Fatalf("Get() error = %v, want nil", err)
+ }
+ if cli == nil {
+ t.Fatalf("Get() returned nil client")
+ }
+ if got := atomic.LoadInt32(&created); got != 1 {
+ t.Fatalf("created clients = %d, want 1", got)
+ }
+
+ pool.Put(cli)
+ if got := len(pool.clients); got != 1 {
+ t.Fatalf("len(pool.clients) after Put = %d, want 1", got)
+ }
+}
+
+// Test if TriClientPool can get timeout fallback correctly
+func TestTriClientPoolGetTimeoutFallback(t *testing.T) {
+ var created int32
+ factory := func() *tri.Client {
+ atomic.AddInt32(&created, 1)
+ return &tri.Client{}
+ }
+
+ pool := NewTriClientPool(0, 1, factory)
+ defer pool.Close()
+
+ cli, err := pool.Get(10 * time.Millisecond)
+ if err != nil {
+ t.Fatalf("first Get() error = %v, want nil", err)
+ }
+ if cli == nil {
+ t.Fatalf("first Get() returned nil client")
+ }
+
+ start := time.Now()
+ fallback, err := pool.Get(5 * time.Millisecond)
+ elapsed := time.Since(start)
+ if err != ErrTriClientPoolTimeout {
+ t.Fatalf("second Get() error = %v, want ErrTriClientPoolTimeout", err)
+ }
+ if fallback == nil {
+ t.Fatalf("second Get() returned nil fallback client")
+ }
+ if pool.fallback != fallback {
+ t.Fatalf("fallback client mismatch: Get() returned %p, pool.fallback = %p", fallback, pool.fallback)
+ }
+ if elapsed < 5*time.Millisecond {
+ t.Fatalf("Get() returned before timeout elapsed: got %v, want >= 5ms", elapsed)
+ }
+ if got := atomic.LoadInt32(&created); got != 2 {
+ t.Fatalf("created clients = %d, want 2 (one normal + one fallback)", got)
+ }
+}
+
+// Test if TriClientPool can close correctly
+func TestTriClientPoolClose(t *testing.T) {
+ pool := NewTriClientPool(1, 1, func() *tri.Client {
+ return &tri.Client{}
+ })
+
+ if pool.Closed() {
+ t.Fatalf("pool should not be closed immediately after creation")
+ }
+
+ pool.Close()
+
+ if !pool.Closed() {
+ t.Fatalf("pool should be closed after Close()")
+ }
+
+ cli, err := pool.Get(1 * time.Millisecond)
+ if cli != nil {
+ t.Fatalf("Get() after Close() should return nil client, got %v", cli)
+ }
+ if err != nil && err != ErrTriClientPoolClosed {
+ t.Fatalf("Get() after Close() error = %v, want nil or ErrTriClientPoolClosed", err)
+ }
+}
+
+// Test if TriClientPool can check expand and shrink correctly
+func TestCheckExpandAndShrink(t *testing.T) {
+ if got := checkExpand(10, 0, 1); got != 2 {
+ t.Fatalf("checkExpand(curSize=10,idle=0,timeouts=1) = %d, want 2", got)
+ }
+ if got := checkExpand(10, 0, 10); got != constant.MaxExpandPerCycle {
+ t.Fatalf("checkExpand(..., timeouts=10) = %d, want maxExpandPerCycle(%d)", got, constant.MaxExpandPerCycle)
+ }
+
+ if got := checkExpand(10, 1, 0); got != 1 {
+ t.Fatalf("checkExpand(curSize=10,idle=1,timeouts=0) = %d, want 1", got)
+ }
+
+ var streak int
+ for i := 0; i < constant.HighIdleStreakLimit-1; i++ {
+ if got := checkShrink(16, 16, &streak); got != 0 {
+ t.Fatalf("checkShrink iteration %d returned %d, want 0 before reaching limit", i, got)
+ }
+ }
+ if got := checkShrink(16, 16, &streak); got <= 0 {
+ t.Fatalf("checkShrink at highIdleStreakLimit returned %d, want > 0", got)
+ }
+}
+
+// TriClientPool Benchmark
+// Benchmark if TriClientPool with different delays
+
+var (
+ testDelays = []time.Duration{
+ 0,
+ 100 * time.Microsecond,
+ 1 * time.Millisecond,
+ 100 * time.Millisecond,
+ }
+)
+
+// setupTripleServer creates an HTTP/2 test server with configurable delay for benchmarks
+func setupTripleServer(delay time.Duration) (*httptest.Server, string) {
+ mux := http.NewServeMux()
+
+ mux.HandleFunc("/test/Echo", func(w http.ResponseWriter, r *http.Request) {
+ time.Sleep(delay)
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(http.StatusOK)
+ w.Write([]byte(`{"echo":"ok"}`))
+ })
+
+ server := httptest.NewUnstartedServer(mux)
+ server.EnableHTTP2 = true
+ server.StartTLS()
+ return server, server.URL + "/test/Echo"
+}
+
+func benchmarkSingleClient(b *testing.B, testURL string) {
+ b.SetParallelism(10)
+ transport := &http.Transport{
+ TLSClientConfig: &tls.Config{
+ //nolint:gosec //NOSONAR - InsecureSkipVerify is acceptable in test environment with httptest.Server
+ InsecureSkipVerify: true,
+ },
+ MaxIdleConns: 100,
+ MaxIdleConnsPerHost: 100,
+ MaxConnsPerHost: 100,
+ }
+ client := tri.NewClient(
+ &http.Client{
+ Transport: transport,
+ Timeout: 10 * time.Second,
+ },
+ testURL,
+ )
+ b.ResetTimer()
+ b.RunParallel(func(pb *testing.PB) {
+ for pb.Next() {
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ req := tri.NewRequest(map[string]string{"test": "data"})
+ resp := tri.NewResponse(map[string]string{})
+ _ = client.CallUnary(ctx, req, resp)
+ cancel()
+ }
+ })
+}
+
+func benchmarkTriClientPool(b *testing.B, testURL string) {
+ b.SetParallelism(10)
+ poolSize := 128
+ pool := NewTriClientPool(16, poolSize, func() *tri.Client {
+ transport := &http.Transport{
+ TLSClientConfig: &tls.Config{
Review Comment:
## Server hostnames should be verified during SSL/TLS connections
<!--SONAR_ISSUE_KEY:AZryrAHtfR9D5s-EniDS-->Enable server hostname verification on this SSL/TLS connection. <p>See more on <a href="https://sonarcloud.io/project/issues?id=apache_dubbo-go&issues=AZryrAHtfR9D5s-EniDS&open=AZryrAHtfR9D5s-EniDS&pullRequest=3086">SonarQube Cloud</a></p>
[Show more details](https://github.com/apache/dubbo-go/security/code-scanning/68)
##########
protocol/triple/client_pool_test.go:
##########
@@ -0,0 +1,293 @@
+/*
+ * 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 triple
+
+import (
+ "context"
+ "crypto/tls"
+ "fmt"
+ "io"
+ "log"
+ "net/http"
+ "net/http/httptest"
+ "sync/atomic"
+ "testing"
+ "time"
+)
+
+import (
+ "dubbo.apache.org/dubbo-go/v3/common/constant"
+ tri "dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol"
+)
+
+// TriClientPool Unit Tests
+// Test if TriClientPool can warm up correctly
+func TestNewTriClientPoolWarmUp(t *testing.T) {
+ var created int32
+ factory := func() *tri.Client {
+ atomic.AddInt32(&created, 1)
+ return &tri.Client{}
+ }
+
+ const warmUpSize = 5
+ const maxSize = 10
+
+ pool := NewTriClientPool(warmUpSize, maxSize, factory)
+ defer pool.Close()
+
+ if got := pool.MaxSize(); got != maxSize {
+ t.Fatalf("MaxSize() = %d, want %d", got, maxSize)
+ }
+ if got := len(pool.clients); got != warmUpSize {
+ t.Fatalf("len(pool.clients) = %d, want %d", got, warmUpSize)
+ }
+ if got := atomic.LoadInt32(&created); got != warmUpSize {
+ t.Fatalf("created clients = %d, want %d", got, warmUpSize)
+ }
+}
+
+// Test if TriClientPool can get and put clients correctly
+func TestTriClientPoolGetAndPutBasic(t *testing.T) {
+ var created int32
+ factory := func() *tri.Client {
+ atomic.AddInt32(&created, 1)
+ return &tri.Client{}
+ }
+
+ pool := NewTriClientPool(1, 1, factory)
+ defer pool.Close()
+
+ cli, err := pool.Get(10 * time.Millisecond)
+ if err != nil {
+ t.Fatalf("Get() error = %v, want nil", err)
+ }
+ if cli == nil {
+ t.Fatalf("Get() returned nil client")
+ }
+ if got := atomic.LoadInt32(&created); got != 1 {
+ t.Fatalf("created clients = %d, want 1", got)
+ }
+
+ pool.Put(cli)
+ if got := len(pool.clients); got != 1 {
+ t.Fatalf("len(pool.clients) after Put = %d, want 1", got)
+ }
+}
+
+// Test if TriClientPool can get timeout fallback correctly
+func TestTriClientPoolGetTimeoutFallback(t *testing.T) {
+ var created int32
+ factory := func() *tri.Client {
+ atomic.AddInt32(&created, 1)
+ return &tri.Client{}
+ }
+
+ pool := NewTriClientPool(0, 1, factory)
+ defer pool.Close()
+
+ cli, err := pool.Get(10 * time.Millisecond)
+ if err != nil {
+ t.Fatalf("first Get() error = %v, want nil", err)
+ }
+ if cli == nil {
+ t.Fatalf("first Get() returned nil client")
+ }
+
+ start := time.Now()
+ fallback, err := pool.Get(5 * time.Millisecond)
+ elapsed := time.Since(start)
+ if err != ErrTriClientPoolTimeout {
+ t.Fatalf("second Get() error = %v, want ErrTriClientPoolTimeout", err)
+ }
+ if fallback == nil {
+ t.Fatalf("second Get() returned nil fallback client")
+ }
+ if pool.fallback != fallback {
+ t.Fatalf("fallback client mismatch: Get() returned %p, pool.fallback = %p", fallback, pool.fallback)
+ }
+ if elapsed < 5*time.Millisecond {
+ t.Fatalf("Get() returned before timeout elapsed: got %v, want >= 5ms", elapsed)
+ }
+ if got := atomic.LoadInt32(&created); got != 2 {
+ t.Fatalf("created clients = %d, want 2 (one normal + one fallback)", got)
+ }
+}
+
+// Test if TriClientPool can close correctly
+func TestTriClientPoolClose(t *testing.T) {
+ pool := NewTriClientPool(1, 1, func() *tri.Client {
+ return &tri.Client{}
+ })
+
+ if pool.Closed() {
+ t.Fatalf("pool should not be closed immediately after creation")
+ }
+
+ pool.Close()
+
+ if !pool.Closed() {
+ t.Fatalf("pool should be closed after Close()")
+ }
+
+ cli, err := pool.Get(1 * time.Millisecond)
+ if cli != nil {
+ t.Fatalf("Get() after Close() should return nil client, got %v", cli)
+ }
+ if err != nil && err != ErrTriClientPoolClosed {
+ t.Fatalf("Get() after Close() error = %v, want nil or ErrTriClientPoolClosed", err)
+ }
+}
+
+// Test if TriClientPool can check expand and shrink correctly
+func TestCheckExpandAndShrink(t *testing.T) {
+ if got := checkExpand(10, 0, 1); got != 2 {
+ t.Fatalf("checkExpand(curSize=10,idle=0,timeouts=1) = %d, want 2", got)
+ }
+ if got := checkExpand(10, 0, 10); got != constant.MaxExpandPerCycle {
+ t.Fatalf("checkExpand(..., timeouts=10) = %d, want maxExpandPerCycle(%d)", got, constant.MaxExpandPerCycle)
+ }
+
+ if got := checkExpand(10, 1, 0); got != 1 {
+ t.Fatalf("checkExpand(curSize=10,idle=1,timeouts=0) = %d, want 1", got)
+ }
+
+ var streak int
+ for i := 0; i < constant.HighIdleStreakLimit-1; i++ {
+ if got := checkShrink(16, 16, &streak); got != 0 {
+ t.Fatalf("checkShrink iteration %d returned %d, want 0 before reaching limit", i, got)
+ }
+ }
+ if got := checkShrink(16, 16, &streak); got <= 0 {
+ t.Fatalf("checkShrink at highIdleStreakLimit returned %d, want > 0", got)
+ }
+}
+
+// TriClientPool Benchmark
+// Benchmark if TriClientPool with different delays
+
+var (
+ testDelays = []time.Duration{
+ 0,
+ 100 * time.Microsecond,
+ 1 * time.Millisecond,
+ 100 * time.Millisecond,
+ }
+)
+
+// setupTripleServer creates an HTTP/2 test server with configurable delay for benchmarks
+func setupTripleServer(delay time.Duration) (*httptest.Server, string) {
+ mux := http.NewServeMux()
+
+ mux.HandleFunc("/test/Echo", func(w http.ResponseWriter, r *http.Request) {
+ time.Sleep(delay)
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(http.StatusOK)
+ w.Write([]byte(`{"echo":"ok"}`))
+ })
+
+ server := httptest.NewUnstartedServer(mux)
+ server.EnableHTTP2 = true
+ server.StartTLS()
+ return server, server.URL + "/test/Echo"
+}
+
+func benchmarkSingleClient(b *testing.B, testURL string) {
+ b.SetParallelism(10)
+ transport := &http.Transport{
+ TLSClientConfig: &tls.Config{
+ //nolint:gosec //NOSONAR - InsecureSkipVerify is acceptable in test environment with httptest.Server
+ InsecureSkipVerify: true,
+ },
+ MaxIdleConns: 100,
+ MaxIdleConnsPerHost: 100,
+ MaxConnsPerHost: 100,
+ }
+ client := tri.NewClient(
+ &http.Client{
+ Transport: transport,
+ Timeout: 10 * time.Second,
+ },
+ testURL,
+ )
+ b.ResetTimer()
+ b.RunParallel(func(pb *testing.PB) {
+ for pb.Next() {
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ req := tri.NewRequest(map[string]string{"test": "data"})
+ resp := tri.NewResponse(map[string]string{})
+ _ = client.CallUnary(ctx, req, resp)
+ cancel()
+ }
+ })
+}
+
+func benchmarkTriClientPool(b *testing.B, testURL string) {
+ b.SetParallelism(10)
+ poolSize := 128
+ pool := NewTriClientPool(16, poolSize, func() *tri.Client {
+ transport := &http.Transport{
+ TLSClientConfig: &tls.Config{
Review Comment:
## Server certificates should be verified during SSL/TLS connections
<!--SONAR_ISSUE_KEY:AZryrAHtfR9D5s-EniDR-->Enable server certificate validation on this SSL/TLS connection. <p>See more on <a href="https://sonarcloud.io/project/issues?id=apache_dubbo-go&issues=AZryrAHtfR9D5s-EniDR&open=AZryrAHtfR9D5s-EniDR&pullRequest=3086">SonarQube Cloud</a></p>
[Show more details](https://github.com/apache/dubbo-go/security/code-scanning/67)
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "No-SilverBullet (via GitHub)" <gi...@apache.org>.
No-SilverBullet commented on code in PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#discussion_r2591732912
##########
tools/dubbogo-cli/generator/sample/hessian/generator.go:
##########
@@ -99,6 +99,13 @@ func scanFile(filePath string) (file *fileInfo, err error) {
stack := make([][]byte, 0)
var line []byte
var lineSize int
+ var (
+ packageRegexp = regexp.MustCompile(PackageRegexp)
+ initFunctionRegexp = regexp.MustCompile(InitFunctionRegexp)
+ hessianImportRegexp = regexp.MustCompile(HessianImportRegexp)
+ lineCommentRegexp = regexp.MustCompile(LineCommentRegexp)
+ hessianPOJORegexp = regexp.MustCompile(HessianPOJORegexp)
+ )
Review Comment:
These changes appear to duplicate with https://github.com/apache/dubbo-go/pull/3099. Please readjust them, either keeping the PR and changing its description, or removing the duplicate changes from this PR.
##########
protocol/triple/client.go:
##########
@@ -59,25 +63,40 @@ const (
type clientManager struct {
isIDL bool
// triple_protocol clients, key is method name
Review Comment:
add a RW lock here to protect this map
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): Implement TriClientPool and Delay triple client Method Binding [dubbo-go]
Posted by "marsevilspirit (via GitHub)" <gi...@apache.org>.
marsevilspirit commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3688563351
Performance test reference: https://github.com/marsevilspirit/dubbo-go-benchmark
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] refactor(triple): upgrade triple client to service-level abstraction [dubbo-go]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3767759996
## [](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086) **Quality Gate passed**
Issues
 [0 New issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=ACCEPTED)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_coverage&view=list)
 [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_duplicated_lines_density&view=list)
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086)
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] refactor(triple): upgrade triple client to service-level abstraction [dubbo-go]
Posted by "AlexStocks (via GitHub)" <gi...@apache.org>.
AlexStocks commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3766793414
@Aetherance please fix the file confliction
@marsevilspirit please review the latest changes.
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "CAICAIIs (via GitHub)" <gi...@apache.org>.
CAICAIIs commented on code in PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#discussion_r2554641124
##########
protocol/triple/client.go:
##########
@@ -431,3 +426,121 @@ func (dt *dualTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return resp, nil
}
+
+var (
+ ErrPoolClosed = errors.New("tri client pool is closed")
Review Comment:
I think this can be named ErrTriPoolClosed.
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "CAICAIIs (via GitHub)" <gi...@apache.org>.
CAICAIIs commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3568932077
please fix the code format
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "CAICAIIs (via GitHub)" <gi...@apache.org>.
CAICAIIs commented on code in PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#discussion_r2572875268
##########
protocol/triple/client_pool.go:
##########
@@ -0,0 +1,294 @@
+/*
+ * 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 triple
+
+import (
+ "errors"
+ "sync"
+ "time"
+)
+
+import (
+ tri "dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol"
+)
+
+type ClientPool interface {
+ Get(timeout time.Duration) (*tri.Client, error)
+ Put(client *tri.Client)
+ Close()
+
+ MaxSize() int
+ Closed() bool
+}
+
+var (
+ ErrTriClientPoolClosed = errors.New("tri client pool is closed")
+ ErrTriClientPoolTimeout = errors.New("tri client pool get timeout")
+ ErrTriClientPoolCloseWhenEmpty = errors.New("empty tri client pool close")
+)
+
+const (
+ autoScalerPeriod = 700 * time.Millisecond // minimum period that autoScaler expand or shrink
+ maxExpandPerCycle = 16 // maximum number of clients to expand per cycle
+ lowIdleThresholdDivisor = 5 // consider clients insufficient if idle < curSize/5
+ highIdleStreakLimit = 10 // consecutive high idle count to trigger shrinking
+)
+
+type TriClientPool struct {
+ clients chan *tri.Client
+ factory func() *tri.Client
+ mu sync.Mutex
+ maxSize int
+ curSize int
+ closed bool
+ getTimeouts int // recent timeout count, used to trigger expansion
+ lastScaleTime time.Time
+
+ fallback *tri.Client
+ consecutiveHighIdle int
+}
+
+func NewTriClientPool(maxSize int, factory func() *tri.Client) *TriClientPool {
+ pool := &TriClientPool{
+ clients: make(chan *tri.Client, maxSize),
+ factory: factory,
+ maxSize: maxSize,
+ }
+ return pool
+}
+
+// TriClient Get method
+// timeout means how long to wait for an available client.
+// TriClientPool keeps a fallback client pointer. If Get() times out,
+// and the pool cannot expand at that moment, the pool will return fallback.
+// This ensures there is at least one usable client.
+// Get tries a non-blocking receive first. If that fails, it tries to expand.
+// If expansion is not allowed, it waits for a client up to timeout.
+// After timeout, if still no client, Get() returns ErrTimeout with fallback.
+func (p *TriClientPool) Get(timeout time.Duration) (*tri.Client, error) {
+ now := time.Now()
+ if now.Sub(p.lastScaleTime) > autoScalerPeriod {
+ p.autoScaler()
+ }
+
+ select {
+ case cli := <-p.clients:
+ return cli, nil
+ default:
+ }
+
+ p.mu.Lock()
+ if p.closed {
+ p.mu.Unlock()
+ return nil, ErrTriClientPoolClosed
+ }
+ // try to expand
+ if p.curSize < p.maxSize {
+ p.curSize++
+ p.mu.Unlock()
+ cli := p.factory()
+ return cli, nil
+ }
+ p.mu.Unlock()
+
+ select {
+ case cli, ok := <-p.clients:
+ if !ok {
+ return nil, ErrTriClientPoolClosed
+ }
+ return cli, nil
+ case <-time.After(timeout):
+ p.recordTimeout()
+ p.mu.Lock()
+ if p.fallback == nil {
+ p.fallback = p.factory()
+ }
+ p.mu.Unlock()
+ return p.fallback, ErrTriClientPoolTimeout
+ }
+}
+
+// TriClient Put method
+// Put tries to put a tri.Client back into the pool.
+// If it fails, Put will drop the client and notify the pool.
+// Dropping a client is part of shrinking.
+func (p *TriClientPool) Put(c *tri.Client) {
+ now := time.Now()
+ if now.Sub(p.lastScaleTime) > autoScalerPeriod {
+ p.autoScaler()
+ }
+
+ if c == nil {
+ return
+ }
+
+ p.mu.Lock()
+ closed := p.closed
+ p.mu.Unlock()
+ if closed {
+ return
+ }
+
+ select {
+ case p.clients <- c:
+ default:
+ p.mu.Lock()
+ p.curSize--
+ p.mu.Unlock()
+ }
+}
+
+// close removes all clients from the channel and then closes it.
+func (p *TriClientPool) Close() {
+ p.mu.Lock()
+ if p.closed {
+ p.mu.Unlock()
+ return
+ }
+ p.closed = true
+
+ for len(p.clients) > 0 {
+ <-p.clients
+ p.curSize--
+ }
+
+ close(p.clients)
+ p.mu.Unlock()
+}
+
+func (p *TriClientPool) MaxSize() int {
+ return p.maxSize
+}
+
+func (p *TriClientPool) Closed() bool {
+ return p.closed
+}
+
+// autoScaler performs scaling check.
+// It is triggered by Get() or Put()
+// If the timeout count is high, autoScaler tends to expand.
+// If the idle client count is often high, autoScaler tends to shrink.
+func (p *TriClientPool) autoScaler() {
+ p.mu.Lock()
+ defer p.mu.Unlock()
+
+ now := time.Now()
+ if now.Sub(p.lastScaleTime) < autoScalerPeriod {
+ return
+ }
+
+ if p.closed {
+ return
+ }
+
+ p.lastScaleTime = now
+
+ curSize := p.curSize
+ idle := len(p.clients)
+ timeouts := p.getTimeouts
+ p.getTimeouts = 0
+
+ needExpand := checkExpand(curSize, idle, timeouts)
+ if needExpand != 0 {
+ p.expand(needExpand)
+ p.consecutiveHighIdle = 0
+ return
+ }
+
+ needShrink := checkShrink(curSize, idle, &p.consecutiveHighIdle)
+ if needShrink != 0 {
+ p.shrink(needShrink)
+ }
+}
+
+// expand creates n more clients
+func (p *TriClientPool) expand(n int) {
+ for i := 0; i < n; i++ {
+ p.mu.Lock()
+ if p.curSize >= p.maxSize {
+ p.mu.Unlock()
+ return
+ }
+ p.curSize++
+ p.mu.Unlock()
+
+ cli := p.factory()
+ p.Put(cli)
+ }
+}
+
+// shrink removes n clients
+func (p *TriClientPool) shrink(n int) {
+ for i := 0; i < n; i++ {
+ select {
+ case <-p.clients:
+ p.mu.Lock()
+ p.curSize--
+ p.mu.Unlock()
+ default:
+ return
+ }
+ }
+}
+
+// record timeout count
+func (p *TriClientPool) recordTimeout() {
+ p.mu.Lock()
+ p.getTimeouts++
+ p.mu.Unlock()
+}
+
+// compute expansion size
+// expansion size is based on timeout count: 2 ^ timeouts
+// if idle clients are less than 1/lowIdleThresholdDivisor of total, treat as busy and expand slightly
+func checkExpand(curSize, idle, timeouts int) int {
+ if timeouts > 0 {
+ expand := 1 << timeouts
+ if expand > maxExpandPerCycle {
+ expand = maxExpandPerCycle
+ }
+ return expand
+ }
+
+ if idle < curSize/lowIdleThresholdDivisor {
+ return 1
+ }
+
+ return 0
+}
+
+// if more than 60% of clients are idle, increase highIdleStreak
Review Comment:
I think can make the 60% threshold a configurable constant (e.g. IdleHighThreshold = 0.6) rather than hardcoding it
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3574184450
## [](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086) **Quality Gate passed**
Issues
 [0 New issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=ACCEPTED)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_coverage&view=list)
 [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_duplicated_lines_density&view=list)
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086)
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3618039565
## [](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086) **Quality Gate failed**
Failed conditions
 [D Security Rating on New Code](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086) (required ≥ A)
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086)
##
 Catch issues before they fail your Quality Gate with our IDE extension  [SonarQube for IDE](https://www.sonarsource.com/products/sonarlint/features/connected-mode/?referrer=pull-request)
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3618035823
## [](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086) **Quality Gate failed**
Failed conditions
 [D Security Rating on New Code](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086) (required ≥ A)
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086)
##
 Catch issues before they fail your Quality Gate with our IDE extension  [SonarQube for IDE](https://www.sonarsource.com/products/sonarlint/features/connected-mode/?referrer=pull-request)
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3589252797
## [](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086) **Quality Gate passed**
Issues
 [1 New issue](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=ACCEPTED)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_coverage&view=list)
 [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_duplicated_lines_density&view=list)
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086)
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "marsevilspirit (via GitHub)" <gi...@apache.org>.
marsevilspirit commented on code in PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#discussion_r2570660655
##########
protocol/triple/client.go:
##########
@@ -304,19 +336,21 @@ func newClientManager(url *common.URL) (*clientManager, error) {
serviceType := reflect.TypeOf(service)
for i := range serviceType.NumMethod() {
- methodName := serviceType.Method(i).Name
Review Comment:
Keep methodName. Don‘t change.
##########
protocol/triple/client_pool.go:
##########
@@ -0,0 +1,281 @@
+/*
+ * 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 triple
+
+import (
+ "errors"
+ "sync"
+ "time"
+)
+
+import (
+ tri "dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol"
+)
+
+type ClientPool interface {
+ Get(timeout time.Duration) (*tri.Client, error)
+ Put(client *tri.Client)
+ Close()
+
+ MaxSize() int
+ Closed() bool
+}
+
+var (
+ ErrTriClientPoolClosed = errors.New("tri client pool is closed")
+ ErrTriClientPoolTimeout = errors.New("tri client pool get timeout")
+ ErrTriClientPoolCloseWhenEmpty = errors.New("empty tri client pool close")
+)
+
+const (
+ autoScalerPeriod = 700 * time.Millisecond // autoScaler interval
+ maxExpandPerCycle = 16 // maximum number of clients to expand per cycle
+ lowIdleThresholdDivisor = 5 // consider clients insufficient if idle < curSize/5
+ highIdleStreakLimit = 10 // consecutive high idle count to trigger shrinking
+)
+
+type TriClientPool struct {
+ clients chan *tri.Client
+ factory func() *tri.Client
+ mu sync.Mutex
+ maxSize int
+ curSize int
+ closed bool
+ getTimeouts int // recent timeout count, used to trigger expansion
+
+ fallback *tri.Client
+}
+
+func NewTriClientPool(maxSize int, factory func() *tri.Client) *TriClientPool {
+ pool := &TriClientPool{
+ clients: make(chan *tri.Client, maxSize),
+ factory: factory,
+ maxSize: maxSize,
+ }
+ go pool.autoScaler()
Review Comment:
Don’t create a new go routine. Check the idle client when get. If it‘s not enough, expand the capacity.
##########
protocol/triple/client.go:
##########
@@ -282,16 +312,18 @@ func newClientManager(url *common.URL) (*clientManager, error) {
baseTriURL = httpPrefix + baseTriURL
}
- triClients := make(map[string]*tri.Client)
+ triPools := make(map[string]*TriClientPool)
Review Comment:
change to clientPool or triClientPool
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): Implement TriClientPool and Delay triple client Method Binding [dubbo-go]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3671432881
## [](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086) **Quality Gate passed**
Issues
 [0 New issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=ACCEPTED)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_coverage&view=list)
 [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_duplicated_lines_density&view=list)
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086)
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3633255953
## [](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086) **Quality Gate passed**
Issues
 [0 New issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=ACCEPTED)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_coverage&view=list)
 [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_duplicated_lines_density&view=list)
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086)
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] refactor(triple): upgrade triple client to service-level abstraction [dubbo-go]
Posted by "Aetherance (via GitHub)" <gi...@apache.org>.
Aetherance commented on code in PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#discussion_r2704734439
##########
protocol/triple/triple_protocol/client.go:
##########
@@ -36,7 +36,7 @@ type TimeoutKey struct{}
// use the [WithTriple] options.
type Client struct {
config *clientConfig
- callUnary func(context.Context, *Request, *Response) error
+ callUnary func(context.Context, *Request, *Response, string) error
Review Comment:
@AlexStocks 不适合放到 Request 里。
现在已经把 string method 放到 Request 后面。
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): Implement TriClientPool and Delay triple client Method Binding [dubbo-go]
Posted by "Aetherance (via GitHub)" <gi...@apache.org>.
Aetherance commented on code in PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#discussion_r2632175631
##########
protocol/triple/client_pool.go:
##########
@@ -0,0 +1,312 @@
+/*
+ * 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 triple
+
+import (
+ "errors"
+ "sync"
+ "time"
+)
+
+import (
+ "dubbo.apache.org/dubbo-go/v3/common/constant"
+ tri "dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol"
+)
+
+type ClientPool interface {
+ Get(timeout time.Duration) (*tri.Client, error)
+ Put(client *tri.Client)
+ Close()
+
+ MaxSize() int
+ Closed() bool
+}
+
+var (
+ ErrTriClientPoolClosed = errors.New("tri client pool is closed")
+ ErrTriClientPoolCloseWhenEmpty = errors.New("empty tri client pool close")
+)
+
+type TriClientPool struct {
+ clients chan *tri.Client
+ factory func() *tri.Client
+ mu sync.Mutex
+ maxSize int
+ curSize int
+ closed bool
+ getTimeouts int // recent timeout count, used to trigger expansion
+ lastScaleTime time.Time
+
+ fallback *tri.Client
+ consecutiveHighIdle int
+}
+
+// NewTriClientPool creates a new TriClientPool with optional warm-up.
+// warmUpSize specifies how many clients to pre-create and add to the pool.
+// If warmUpSize is 0, no warm-up is performed.
+// If warmUpSize > maxSize, it will be capped to maxSize.
+func NewTriClientPool(warmUpSize, maxSize int, factory func() *tri.Client) *TriClientPool {
+ if warmUpSize > maxSize {
+ warmUpSize = maxSize
+ }
+ if warmUpSize < 0 {
+ warmUpSize = 0
+ }
+
+ pool := &TriClientPool{
+ clients: make(chan *tri.Client, maxSize),
+ factory: factory,
+ maxSize: maxSize,
+ curSize: warmUpSize,
+ }
+
+ for i := 0; i < warmUpSize; i++ {
+ cli := factory()
+ select {
+ case pool.clients <- cli:
+ default:
+ pool.curSize--
+ }
+ }
+
+ return pool
+}
+
+// TriClient Get method
+// timeout means how long to wait for an available client.
+// TriClientPool keeps a fallback client pointer. If Get() times out,
+// and the pool cannot expand at that moment, the pool will return fallback.
+// This ensures there is at least one usable client.
+// Get tries a non-blocking receive first. If that fails, it tries to expand.
+// If expansion is not allowed, it waits for a client up to timeout.
+// After timeout, if still no client, Get() returns ErrTimeout with fallback.
+func (p *TriClientPool) Get(timeout time.Duration) (*tri.Client, error) {
+ now := time.Now()
+ if now.Sub(p.lastScaleTime) > constant.AutoScalerPeriod {
+ p.autoScaler()
+ }
+
+ select {
+ case cli := <-p.clients:
+ return cli, nil
+ default:
+ }
+
+ p.mu.Lock()
+ if p.closed {
+ p.mu.Unlock()
+ return nil, ErrTriClientPoolClosed
+ }
+ // try to expand
+ if p.curSize < p.maxSize {
+ p.curSize++
+ p.mu.Unlock()
+ cli := p.factory()
+ return cli, nil
+ }
+ p.mu.Unlock()
+
+ select {
+ case cli, ok := <-p.clients:
+ if !ok {
+ return nil, ErrTriClientPoolClosed
+ }
+ return cli, nil
+ case <-time.After(timeout):
+ p.recordTimeout()
+ p.mu.Lock()
+ if p.fallback == nil {
+ p.fallback = p.factory()
+ }
+ p.mu.Unlock()
+ return p.fallback, nil
+ }
+}
+
+// TriClient Put method
+// Put tries to put a tri.Client back into the pool.
+// If it fails, Put will drop the client and notify the pool.
+// Dropping a client is part of shrinking.
+func (p *TriClientPool) Put(c *tri.Client) {
+ now := time.Now()
+ if now.Sub(p.lastScaleTime) > constant.AutoScalerPeriod {
+ p.autoScaler()
+ }
+
+ if c == nil {
+ return
+ }
+
+ p.mu.Lock()
+ closed := p.closed
+ p.mu.Unlock()
+ if closed {
+ return
+ }
+
+ select {
+ case p.clients <- c:
+ default:
+ p.mu.Lock()
+ p.curSize--
+ p.mu.Unlock()
+ }
+}
+
+// close removes all clients from the channel and then closes it.
+func (p *TriClientPool) Close() {
+ p.mu.Lock()
+ if p.closed {
+ p.mu.Unlock()
+ return
+ }
+ p.closed = true
+
+ for len(p.clients) > 0 {
+ <-p.clients
+ p.curSize--
+ }
+
+ close(p.clients)
+ p.mu.Unlock()
+}
+
+func (p *TriClientPool) MaxSize() int {
+ return p.maxSize
+}
+
+func (p *TriClientPool) Closed() bool {
+ return p.closed
+}
+
+// autoScaler performs scaling check.
+// It is triggered by Get() or Put()
+// If the timeout count is high, autoScaler tends to expand.
+// If the idle client count is often high, autoScaler tends to shrink.
+func (p *TriClientPool) autoScaler() {
+ p.mu.Lock()
+
+ now := time.Now()
+ if now.Sub(p.lastScaleTime) < constant.AutoScalerPeriod {
+ p.mu.Unlock()
+ return
+ }
+
+ if p.closed {
+ p.mu.Unlock()
+ return
+ }
+
+ p.lastScaleTime = now
+
+ curSize := p.curSize
+ idle := len(p.clients)
+ timeouts := p.getTimeouts
+ p.getTimeouts = 0
+
+ needExpand := checkExpand(curSize, idle, timeouts)
+ if needExpand != 0 {
+ p.consecutiveHighIdle = 0
+ p.mu.Unlock()
+ p.expand(needExpand)
+ return
+ }
+
+ needShrink := checkShrink(curSize, idle, &p.consecutiveHighIdle)
+ p.mu.Unlock()
+ if needShrink != 0 {
+ p.shrink(needShrink)
+ }
+}
+
+// expand creates n more clients
+func (p *TriClientPool) expand(n int) {
+ for i := 0; i < n; i++ {
+ p.mu.Lock()
+ if p.curSize >= p.maxSize {
+ p.mu.Unlock()
+ return
+ }
+ p.curSize++
+ p.mu.Unlock()
+
+ cli := p.factory()
+ p.Put(cli)
+ }
+}
+
+// shrink removes n clients
+func (p *TriClientPool) shrink(n int) {
+ for i := 0; i < n; i++ {
+ select {
+ case <-p.clients:
+ p.mu.Lock()
+ p.curSize--
+ p.mu.Unlock()
+ default:
+ return
+ }
+ }
+}
+
+// record timeout count
+func (p *TriClientPool) recordTimeout() {
+ p.mu.Lock()
+ p.getTimeouts++
+ p.mu.Unlock()
Review Comment:
done
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "Aetherance (via GitHub)" <gi...@apache.org>.
Aetherance commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3568898777
issue #3087
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "CAICAIIs (via GitHub)" <gi...@apache.org>.
CAICAIIs commented on code in PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#discussion_r2554667934
##########
protocol/triple/client.go:
##########
@@ -431,3 +426,121 @@ func (dt *dualTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return resp, nil
}
+
+var (
+ ErrPoolClosed = errors.New("tri client pool is closed")
+ ErrTimeout = errors.New("tri client pool get timeout")
+)
+
+type TriClientPool struct {
+ clients chan *tri.Client
+ factory func() *tri.Client
+ mu sync.Mutex
+ maxSize int
+ curSize int
+ closed bool
+}
+
+func NewTriClientPool(maxSize int, factory func() *tri.Client) *TriClientPool {
+ triClientPool := &TriClientPool{
+ clients: make(chan *tri.Client, maxSize),
+ maxSize: maxSize,
+ factory: factory,
+ }
+ return triClientPool
+}
+
+func (p *TriClientPool) Get(timeout time.Duration) (*tri.Client, error) {
+ // Try to get a client from the pool immediately
+ select {
+ case cli := <-p.clients:
+ return cli, nil
+ default:
+ // pool is empty continue to check if can create a new client
+ }
+
+ p.mu.Lock()
+ if p.closed {
+ p.mu.Unlock()
+ return nil, ErrPoolClosed // pool closed
+ }
+
+ if p.curSize < p.maxSize {
+ p.curSize++
+ p.mu.Unlock()
+
+ cli := p.factory()
+ return cli, nil
+ }
+ p.mu.Unlock()
+
+ select {
+ case cli, ok := <-p.clients:
+ if !ok {
+ return nil, ErrPoolClosed
+ }
+ return cli, nil
+ case <-time.After(timeout):
+ p.mu.Lock()
+ if p.closed {
+ p.mu.Unlock()
+ return nil, ErrPoolClosed
+ } else {
+ p.mu.Unlock()
+ return nil, ErrTimeout
+ }
+ }
+}
+
+func (p *TriClientPool) Put(c *tri.Client) {
Review Comment:
Please add comments to some important functions/methods.
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "marsevilspirit (via GitHub)" <gi...@apache.org>.
marsevilspirit commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3589530873
Write a performance test to demonstrate that your change can improve the throughput of triple.
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): Implement TriClientPool and Delay triple client Method Binding [dubbo-go]
Posted by "Aetherance (via GitHub)" <gi...@apache.org>.
Aetherance commented on code in PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#discussion_r2616841809
##########
protocol/triple/triple_protocol/client.go:
##########
@@ -117,8 +121,27 @@ func NewClient(httpClient HTTPClient, url string, options ...ClientOption) *Clie
return client
}
+func buildMethodLevelReqSpec(serviceLevelReqSpec *Spec, method string) (Spec, error) {
+ if serviceLevelReqSpec == nil {
+ return Spec{}, fmt.Errorf("serviceLevelReqSpec is nil")
+ }
+ methodLevelSpec := *serviceLevelReqSpec
+
+ methodLevelURL, err := joinPath(methodLevelSpec.Procedure, method)
+ if err != nil {
+ return Spec{}, fmt.Errorf("JoinPath failed for procedure %s, method %s", methodLevelSpec.Procedure, method)
+ }
+
+ methodLevelSpec.Procedure = methodLevelURL
+ return methodLevelSpec, nil
+}
+
+func joinPath(base string, elem ...string) (result string, err error) {
+ return url.JoinPath(base, elem...)
+}
Review Comment:
这个是从 protocol/triple/common.go 搬过来的
这里确实不应该这样 已修复
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): Implement TriClientPool and Delay triple client Method Binding [dubbo-go]
Posted by "Aetherance (via GitHub)" <gi...@apache.org>.
Aetherance commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3688637529
> Performance test reference: https://github.com/marsevilspirit/dubbo-go-benchmark
got it
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): Implement TriClientPool and Delay triple client Method Binding [dubbo-go]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3733435197
## [](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086) **Quality Gate passed**
Issues
 [0 New issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=ACCEPTED)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_coverage&view=list)
 [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_duplicated_lines_density&view=list)
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086)
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3567875844
## [](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086) **Quality Gate passed**
Issues
 [0 New issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=ACCEPTED)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_coverage&view=list)
 [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_duplicated_lines_density&view=list)
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086)
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3589255775
## [](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086) **Quality Gate passed**
Issues
 [1 New issue](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=ACCEPTED)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_coverage&view=list)
 [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_duplicated_lines_density&view=list)
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086)
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "Aetherance (via GitHub)" <gi...@apache.org>.
Aetherance commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3591817334
Thanks for reviewing. I will fix later
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "marsevilspirit (via GitHub)" <gi...@apache.org>.
marsevilspirit commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3644929846
Do you have test results? If so, please post them on this thread.
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3645042481
## [](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086) **Quality Gate passed**
Issues
 [0 New issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=ACCEPTED)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_coverage&view=list)
 [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_duplicated_lines_density&view=list)
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086)
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "Aetherance (via GitHub)" <gi...@apache.org>.
Aetherance commented on code in PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#discussion_r2594658813
##########
protocol/triple/client.go:
##########
@@ -51,6 +51,10 @@ import (
const (
httpPrefix string = "http://"
httpsPrefix string = "https://"
+ // triClientPoolMaxSize is the maximum number of tri.Client instances per method in the pool.
+ triClientPoolMaxSize = 32
+ // defaultClientGetTimeout is the timeout for getting a tri.Client from the TriClientPool.
+ defaultClientGetTimeout = 50 * time.Millisecond
Review Comment:
done
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): Implement TriClientPool and Delay triple client Method Binding [dubbo-go]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3650973807
## [](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086) **Quality Gate passed**
Issues
 [0 New issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=ACCEPTED)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_coverage&view=list)
 [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_duplicated_lines_density&view=list)
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086)
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] refactor(triple): upgrade triple client to service-level abstraction [dubbo-go]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3770650056
## [](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086) **Quality Gate passed**
Issues
 [0 New issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=ACCEPTED)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_coverage&view=list)
 [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_duplicated_lines_density&view=list)
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086)
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] refactor(triple): upgrade triple client to service-level abstraction [dubbo-go]
Posted by "Aetherance (via GitHub)" <gi...@apache.org>.
Aetherance commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3733548257
@marsevilspirit
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): Implement TriClientPool and Delay triple client Method Binding [dubbo-go]
Posted by "Aetherance (via GitHub)" <gi...@apache.org>.
Aetherance commented on code in PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#discussion_r2634693225
##########
protocol/triple/client_pool.go:
##########
@@ -0,0 +1,312 @@
+/*
+ * 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 triple
+
+import (
+ "errors"
+ "sync"
+ "time"
+)
+
+import (
+ "dubbo.apache.org/dubbo-go/v3/common/constant"
+ tri "dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol"
+)
+
+type ClientPool interface {
+ Get(timeout time.Duration) (*tri.Client, error)
+ Put(client *tri.Client)
+ Close()
+
+ MaxSize() int
+ Closed() bool
+}
+
+var (
+ ErrTriClientPoolClosed = errors.New("tri client pool is closed")
+ ErrTriClientPoolCloseWhenEmpty = errors.New("empty tri client pool close")
+)
+
+type TriClientPool struct {
+ clients chan *tri.Client
+ factory func() *tri.Client
+ mu sync.Mutex
+ maxSize int
+ curSize int
+ closed bool
+ getTimeouts int // recent timeout count, used to trigger expansion
+ lastScaleTime time.Time
+
+ fallback *tri.Client
+ consecutiveHighIdle int
+}
+
+// NewTriClientPool creates a new TriClientPool with optional warm-up.
+// warmUpSize specifies how many clients to pre-create and add to the pool.
+// If warmUpSize is 0, no warm-up is performed.
+// If warmUpSize > maxSize, it will be capped to maxSize.
+func NewTriClientPool(warmUpSize, maxSize int, factory func() *tri.Client) *TriClientPool {
+ if warmUpSize > maxSize {
+ warmUpSize = maxSize
+ }
+ if warmUpSize < 0 {
+ warmUpSize = 0
+ }
+
+ pool := &TriClientPool{
+ clients: make(chan *tri.Client, maxSize),
+ factory: factory,
+ maxSize: maxSize,
+ curSize: warmUpSize,
+ }
+
+ for i := 0; i < warmUpSize; i++ {
+ cli := factory()
+ select {
+ case pool.clients <- cli:
+ default:
+ pool.curSize--
+ }
+ }
+
+ return pool
+}
+
+// TriClient Get method
+// timeout means how long to wait for an available client.
+// TriClientPool keeps a fallback client pointer. If Get() times out,
+// and the pool cannot expand at that moment, the pool will return fallback.
+// This ensures there is at least one usable client.
+// Get tries a non-blocking receive first. If that fails, it tries to expand.
+// If expansion is not allowed, it waits for a client up to timeout.
+// After timeout, if still no client, Get() returns ErrTimeout with fallback.
+func (p *TriClientPool) Get(timeout time.Duration) (*tri.Client, error) {
+ now := time.Now()
+ if now.Sub(p.lastScaleTime) > constant.AutoScalerPeriod {
+ p.autoScaler()
+ }
+
+ select {
+ case cli := <-p.clients:
+ return cli, nil
+ default:
+ }
+
+ p.mu.Lock()
+ if p.closed {
+ p.mu.Unlock()
+ return nil, ErrTriClientPoolClosed
+ }
+ // try to expand
+ if p.curSize < p.maxSize {
+ p.curSize++
+ p.mu.Unlock()
+ cli := p.factory()
+ return cli, nil
+ }
+ p.mu.Unlock()
+
+ select {
+ case cli, ok := <-p.clients:
+ if !ok {
+ return nil, ErrTriClientPoolClosed
+ }
+ return cli, nil
+ case <-time.After(timeout):
+ p.recordTimeout()
+ p.mu.Lock()
+ if p.fallback == nil {
+ p.fallback = p.factory()
+ }
+ p.mu.Unlock()
+ return p.fallback, nil
+ }
+}
+
+// TriClient Put method
+// Put tries to put a tri.Client back into the pool.
+// If it fails, Put will drop the client and notify the pool.
+// Dropping a client is part of shrinking.
+func (p *TriClientPool) Put(c *tri.Client) {
+ now := time.Now()
+ if now.Sub(p.lastScaleTime) > constant.AutoScalerPeriod {
+ p.autoScaler()
+ }
+
+ if c == nil {
+ return
+ }
+
+ p.mu.Lock()
+ closed := p.closed
+ p.mu.Unlock()
+ if closed {
+ return
+ }
+
+ select {
+ case p.clients <- c:
+ default:
+ p.mu.Lock()
+ p.curSize--
+ p.mu.Unlock()
+ }
Review Comment:
第一次锁(155 行)在读取 p.closed 后立即释放(157 行 Unlock)。
第二次锁(165 行)只在 select default 分支执行,随后也立即释放。
应该不会尝试加两次锁
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): Implement TriClientPool and Delay triple client Method Binding [dubbo-go]
Posted by "Aetherance (via GitHub)" <gi...@apache.org>.
Aetherance commented on code in PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#discussion_r2634701777
##########
protocol/triple/client_pool.go:
##########
@@ -0,0 +1,312 @@
+/*
+ * 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 triple
+
+import (
+ "errors"
+ "sync"
+ "time"
+)
+
+import (
+ "dubbo.apache.org/dubbo-go/v3/common/constant"
+ tri "dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol"
+)
+
+type ClientPool interface {
+ Get(timeout time.Duration) (*tri.Client, error)
+ Put(client *tri.Client)
+ Close()
+
+ MaxSize() int
+ Closed() bool
+}
+
+var (
+ ErrTriClientPoolClosed = errors.New("tri client pool is closed")
+ ErrTriClientPoolCloseWhenEmpty = errors.New("empty tri client pool close")
+)
+
+type TriClientPool struct {
+ clients chan *tri.Client
+ factory func() *tri.Client
+ mu sync.Mutex
+ maxSize int
+ curSize int
+ closed bool
+ getTimeouts int // recent timeout count, used to trigger expansion
+ lastScaleTime time.Time
+
+ fallback *tri.Client
+ consecutiveHighIdle int
+}
+
+// NewTriClientPool creates a new TriClientPool with optional warm-up.
+// warmUpSize specifies how many clients to pre-create and add to the pool.
+// If warmUpSize is 0, no warm-up is performed.
+// If warmUpSize > maxSize, it will be capped to maxSize.
+func NewTriClientPool(warmUpSize, maxSize int, factory func() *tri.Client) *TriClientPool {
+ if warmUpSize > maxSize {
+ warmUpSize = maxSize
+ }
+ if warmUpSize < 0 {
+ warmUpSize = 0
+ }
+
+ pool := &TriClientPool{
+ clients: make(chan *tri.Client, maxSize),
+ factory: factory,
+ maxSize: maxSize,
+ curSize: warmUpSize,
+ }
+
+ for i := 0; i < warmUpSize; i++ {
+ cli := factory()
+ select {
+ case pool.clients <- cli:
+ default:
+ pool.curSize--
+ }
+ }
+
+ return pool
+}
+
+// TriClient Get method
+// timeout means how long to wait for an available client.
+// TriClientPool keeps a fallback client pointer. If Get() times out,
+// and the pool cannot expand at that moment, the pool will return fallback.
+// This ensures there is at least one usable client.
+// Get tries a non-blocking receive first. If that fails, it tries to expand.
+// If expansion is not allowed, it waits for a client up to timeout.
+// After timeout, if still no client, Get() returns ErrTimeout with fallback.
+func (p *TriClientPool) Get(timeout time.Duration) (*tri.Client, error) {
+ now := time.Now()
+ if now.Sub(p.lastScaleTime) > constant.AutoScalerPeriod {
+ p.autoScaler()
+ }
+
+ select {
+ case cli := <-p.clients:
+ return cli, nil
+ default:
+ }
+
+ p.mu.Lock()
+ if p.closed {
+ p.mu.Unlock()
+ return nil, ErrTriClientPoolClosed
+ }
+ // try to expand
+ if p.curSize < p.maxSize {
+ p.curSize++
+ p.mu.Unlock()
+ cli := p.factory()
+ return cli, nil
+ }
+ p.mu.Unlock()
+
+ select {
+ case cli, ok := <-p.clients:
+ if !ok {
+ return nil, ErrTriClientPoolClosed
+ }
+ return cli, nil
+ case <-time.After(timeout):
+ p.recordTimeout()
+ p.mu.Lock()
+ if p.fallback == nil {
+ p.fallback = p.factory()
+ }
+ p.mu.Unlock()
+ return p.fallback, nil
+ }
+}
+
+// TriClient Put method
+// Put tries to put a tri.Client back into the pool.
+// If it fails, Put will drop the client and notify the pool.
+// Dropping a client is part of shrinking.
+func (p *TriClientPool) Put(c *tri.Client) {
+ now := time.Now()
+ if now.Sub(p.lastScaleTime) > constant.AutoScalerPeriod {
+ p.autoScaler()
+ }
+
+ if c == nil {
+ return
+ }
+
+ p.mu.Lock()
+ closed := p.closed
+ p.mu.Unlock()
+ if closed {
+ return
+ }
+
+ select {
+ case p.clients <- c:
+ default:
+ p.mu.Lock()
+ p.curSize--
+ p.mu.Unlock()
+ }
Review Comment:
```go
p.mu.Lock()
defer p.mu.Unlock()
if p.closed {
return
}
select {
case p.clients <- c:
default:
p.curSize--
}
```
并且目前这里也已经优化成以上写法,不会尝试加两次锁
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3644959464
## [](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086) **Quality Gate passed**
Issues
 [0 New issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=ACCEPTED)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_coverage&view=list)
 [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_duplicated_lines_density&view=list)
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086)
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] refactor(triple): upgrade triple client to service-level abstraction [dubbo-go]
Posted by "AlexStocks (via GitHub)" <gi...@apache.org>.
AlexStocks merged PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "CAICAIIs (via GitHub)" <gi...@apache.org>.
CAICAIIs commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3568980664
Maybe it would be better to put TriClientPool in a separate new file instead of cramming it into the existing client.go?
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3619738919
## [](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086) **Quality Gate passed**
Issues
 [1 New issue](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=ACCEPTED)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_coverage&view=list)
 [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_duplicated_lines_density&view=list)
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086)
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): Implement TriClientPool and Delay triple client Method Binding [dubbo-go]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3733433301
## [](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086) **Quality Gate passed**
Issues
 [0 New issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=ACCEPTED)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_coverage&view=list)
 [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_duplicated_lines_density&view=list)
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086)
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): Implement TriClientPool and Delay triple client Method Binding [dubbo-go]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3733404585
## [](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086) **Quality Gate passed**
Issues
 [0 New issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=ACCEPTED)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_coverage&view=list)
 [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_duplicated_lines_density&view=list)
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086)
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): Implement TriClientPool and Delay triple client Method Binding [dubbo-go]
Posted by "marsevilspirit (via GitHub)" <gi...@apache.org>.
marsevilspirit commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3692498418
Is this PR process still ongoing?
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): Implement TriClientPool and Delay triple client Method Binding [dubbo-go]
Posted by "Aetherance (via GitHub)" <gi...@apache.org>.
Aetherance commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3688636530
> benchmark 能不能加 大流量 和 并发量高一些 的场景,看一下这两种场景中修改前后的benchmark结果
ok
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "CAICAIIs (via GitHub)" <gi...@apache.org>.
CAICAIIs commented on code in PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#discussion_r2554628935
##########
protocol/triple/client.go:
##########
@@ -26,25 +26,20 @@ import (
"net/http"
"reflect"
"strings"
+ "sync"
"time"
-)
-import (
Review Comment:
please divide into three blocks.
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3584209260
## [](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086) **Quality Gate passed**
Issues
 [1 New issue](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=ACCEPTED)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_coverage&view=list)
 [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_duplicated_lines_density&view=list)
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086)
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3584242653
## [](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086) **Quality Gate passed**
Issues
 [1 New issue](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=ACCEPTED)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_coverage&view=list)
 [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_duplicated_lines_density&view=list)
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086)
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "Aetherance (via GitHub)" <gi...@apache.org>.
Aetherance commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3587939567
@marsevilspirit review
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "github-advanced-security[bot] (via GitHub)" <gi...@apache.org>.
github-advanced-security[bot] commented on code in PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#discussion_r2593569879
##########
protocol/triple/client_pool_test.go:
##########
@@ -0,0 +1,282 @@
+/*
+ * 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 triple
+
+import (
+ "context"
+ "crypto/tls"
+ "fmt"
+ "io"
+ "log"
+ "net/http"
+ "net/http/httptest"
+ "sync/atomic"
+ "testing"
+ "time"
+)
+
+import (
+ tri "dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol"
+)
+
+// TriClientPool Unit Tests
+// Test if TriClientPool can warm up correctly
+func TestNewTriClientPoolWarmUp(t *testing.T) {
+ var created int32
+ factory := func() *tri.Client {
+ atomic.AddInt32(&created, 1)
+ return &tri.Client{}
+ }
+
+ const warmUpSize = 5
+ const maxSize = 10
+
+ pool := NewTriClientPool(warmUpSize, maxSize, factory)
+ defer pool.Close()
+
+ if got := pool.MaxSize(); got != maxSize {
+ t.Fatalf("MaxSize() = %d, want %d", got, maxSize)
+ }
+ if got := len(pool.clients); got != warmUpSize {
+ t.Fatalf("len(pool.clients) = %d, want %d", got, warmUpSize)
+ }
+ if got := atomic.LoadInt32(&created); got != warmUpSize {
+ t.Fatalf("created clients = %d, want %d", got, warmUpSize)
+ }
+}
+
+// Test if TriClientPool can get and put clients correctly
+func TestTriClientPoolGetAndPut_Basic(t *testing.T) {
+ var created int32
+ factory := func() *tri.Client {
+ atomic.AddInt32(&created, 1)
+ return &tri.Client{}
+ }
+
+ pool := NewTriClientPool(1, 1, factory)
+ defer pool.Close()
+
+ cli, err := pool.Get(10 * time.Millisecond)
+ if err != nil {
+ t.Fatalf("Get() error = %v, want nil", err)
+ }
+ if cli == nil {
+ t.Fatalf("Get() returned nil client")
+ }
+ if got := atomic.LoadInt32(&created); got != 1 {
+ t.Fatalf("created clients = %d, want 1", got)
+ }
+
+ pool.Put(cli)
+ if got := len(pool.clients); got != 1 {
+ t.Fatalf("len(pool.clients) after Put = %d, want 1", got)
+ }
+}
+
+// Test if TriClientPool can get timeout fallback correctly
+func TestTriClientPoolGetTimeoutFallback(t *testing.T) {
+ var created int32
+ factory := func() *tri.Client {
+ atomic.AddInt32(&created, 1)
+ return &tri.Client{}
+ }
+
+ pool := NewTriClientPool(0, 1, factory)
+ defer pool.Close()
+
+ cli, err := pool.Get(10 * time.Millisecond)
+ if err != nil {
+ t.Fatalf("first Get() error = %v, want nil", err)
+ }
+ if cli == nil {
+ t.Fatalf("first Get() returned nil client")
+ }
+
+ start := time.Now()
+ fallback, err := pool.Get(5 * time.Millisecond)
+ elapsed := time.Since(start)
+ if err != ErrTriClientPoolTimeout {
+ t.Fatalf("second Get() error = %v, want ErrTriClientPoolTimeout", err)
+ }
+ if fallback == nil {
+ t.Fatalf("second Get() returned nil fallback client")
+ }
+ if pool.fallback != fallback {
+ t.Fatalf("fallback client mismatch: Get() returned %p, pool.fallback = %p", fallback, pool.fallback)
+ }
+ if elapsed < 5*time.Millisecond {
+ t.Fatalf("Get() returned before timeout elapsed: got %v, want >= 5ms", elapsed)
+ }
+ if got := atomic.LoadInt32(&created); got != 2 {
+ t.Fatalf("created clients = %d, want 2 (one normal + one fallback)", got)
+ }
+}
+
+// Test if TriClientPool can close correctly
+func TestTriClientPoolClose(t *testing.T) {
+ pool := NewTriClientPool(1, 1, func() *tri.Client {
+ return &tri.Client{}
+ })
+
+ if pool.Closed() {
+ t.Fatalf("pool should not be closed immediately after creation")
+ }
+
+ pool.Close()
+
+ if !pool.Closed() {
+ t.Fatalf("pool should be closed after Close()")
+ }
+
+ cli, err := pool.Get(1 * time.Millisecond)
+ if cli != nil {
+ t.Fatalf("Get() after Close() should return nil client, got %v", cli)
+ }
+ if err != nil && err != ErrTriClientPoolClosed {
+ t.Fatalf("Get() after Close() error = %v, want nil or ErrTriClientPoolClosed", err)
+ }
+}
+
+// Test if TriClientPool can check expand and shrink correctly
+func TestCheckExpandAndShrink(t *testing.T) {
+ if got := checkExpand(10, 0, 1); got != 2 {
+ t.Fatalf("checkExpand(curSize=10,idle=0,timeouts=1) = %d, want 2", got)
+ }
+ if got := checkExpand(10, 0, 10); got != maxExpandPerCycle {
+ t.Fatalf("checkExpand(..., timeouts=10) = %d, want maxExpandPerCycle(%d)", got, maxExpandPerCycle)
+ }
+
+ if got := checkExpand(10, 1, 0); got != 1 {
+ t.Fatalf("checkExpand(curSize=10,idle=1,timeouts=0) = %d, want 1", got)
+ }
+
+ var streak int
+ for i := 0; i < highIdleStreakLimit-1; i++ {
+ if got := checkShrink(16, 16, &streak); got != 0 {
+ t.Fatalf("checkShrink iteration %d returned %d, want 0 before reaching limit", i, got)
+ }
+ }
+ if got := checkShrink(16, 16, &streak); got <= 0 {
+ t.Fatalf("checkShrink at highIdleStreakLimit returned %d, want > 0", got)
+ }
+}
+
+// TriClientPool Benchmark
+// Benchmark if TriClientPool with different delays
+
+var (
+ testDelays = []time.Duration{
+ 0,
+ 100 * time.Microsecond,
+ 1 * time.Millisecond,
+ 100 * time.Millisecond,
+ }
+)
+
+// setupTripleServer creates an HTTP/2 test server with configurable delay for benchmarks
+func setupTripleServer(delay time.Duration) (*httptest.Server, string) {
+ mux := http.NewServeMux()
+
+ mux.HandleFunc("/test/Echo", func(w http.ResponseWriter, r *http.Request) {
+ time.Sleep(delay)
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(http.StatusOK)
+ w.Write([]byte(`{"echo":"ok"}`))
+ })
+
+ server := httptest.NewUnstartedServer(mux)
+ server.EnableHTTP2 = true
+ server.StartTLS()
+ return server, server.URL + "/test/Echo"
+}
+
+func BenchmarkThroughputComparisonDelay(b *testing.B) {
+ originalLogOutput := log.Writer()
+ log.SetOutput(io.Discard)
+ defer log.SetOutput(originalLogOutput)
+
+ for _, delay := range testDelays {
+ b.Run(fmt.Sprintf("delay_%v", delay), func(b *testing.B) {
+ _, testURL := setupTripleServer(delay)
+ var singleTransport *http.Transport
+
+ b.Run("SingleClient", func(b *testing.B) {
+ b.SetParallelism(10)
+ singleTransport = &http.Transport{
+ TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
+ MaxIdleConns: 100,
+ MaxIdleConnsPerHost: 100,
+ MaxConnsPerHost: 100,
+ }
+
+ client := tri.NewClient(
+ &http.Client{
+ Transport: singleTransport,
+ Timeout: 10 * time.Second,
+ },
+ testURL,
+ )
+
+ b.ResetTimer()
+ b.RunParallel(func(pb *testing.PB) {
+ for pb.Next() {
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ req := tri.NewRequest(map[string]string{"test": "data"})
+ resp := tri.NewResponse(map[string]string{})
+ _ = client.CallUnary(ctx, req, resp)
+ cancel()
+ }
+ })
+ })
+
+ b.Run("ConnectionPool", func(b *testing.B) {
+ b.SetParallelism(10)
+ poolSize := 128
+ pool := NewTriClientPool(16, poolSize, func() *tri.Client {
+ transport := &http.Transport{
+ TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Review Comment:
## Server certificates should be verified during SSL/TLS connections
<!--SONAR_ISSUE_KEY:AZrumDowkncTmn-Jpuco-->Enable server certificate validation on this SSL/TLS connection. <p>See more on <a href="https://sonarcloud.io/project/issues?id=apache_dubbo-go&issues=AZrumDowkncTmn-Jpuco&open=AZrumDowkncTmn-Jpuco&pullRequest=3086">SonarQube Cloud</a></p>
[Show more details](https://github.com/apache/dubbo-go/security/code-scanning/63)
##########
protocol/triple/client_pool_test.go:
##########
@@ -0,0 +1,282 @@
+/*
+ * 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 triple
+
+import (
+ "context"
+ "crypto/tls"
+ "fmt"
+ "io"
+ "log"
+ "net/http"
+ "net/http/httptest"
+ "sync/atomic"
+ "testing"
+ "time"
+)
+
+import (
+ tri "dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol"
+)
+
+// TriClientPool Unit Tests
+// Test if TriClientPool can warm up correctly
+func TestNewTriClientPoolWarmUp(t *testing.T) {
+ var created int32
+ factory := func() *tri.Client {
+ atomic.AddInt32(&created, 1)
+ return &tri.Client{}
+ }
+
+ const warmUpSize = 5
+ const maxSize = 10
+
+ pool := NewTriClientPool(warmUpSize, maxSize, factory)
+ defer pool.Close()
+
+ if got := pool.MaxSize(); got != maxSize {
+ t.Fatalf("MaxSize() = %d, want %d", got, maxSize)
+ }
+ if got := len(pool.clients); got != warmUpSize {
+ t.Fatalf("len(pool.clients) = %d, want %d", got, warmUpSize)
+ }
+ if got := atomic.LoadInt32(&created); got != warmUpSize {
+ t.Fatalf("created clients = %d, want %d", got, warmUpSize)
+ }
+}
+
+// Test if TriClientPool can get and put clients correctly
+func TestTriClientPoolGetAndPut_Basic(t *testing.T) {
+ var created int32
+ factory := func() *tri.Client {
+ atomic.AddInt32(&created, 1)
+ return &tri.Client{}
+ }
+
+ pool := NewTriClientPool(1, 1, factory)
+ defer pool.Close()
+
+ cli, err := pool.Get(10 * time.Millisecond)
+ if err != nil {
+ t.Fatalf("Get() error = %v, want nil", err)
+ }
+ if cli == nil {
+ t.Fatalf("Get() returned nil client")
+ }
+ if got := atomic.LoadInt32(&created); got != 1 {
+ t.Fatalf("created clients = %d, want 1", got)
+ }
+
+ pool.Put(cli)
+ if got := len(pool.clients); got != 1 {
+ t.Fatalf("len(pool.clients) after Put = %d, want 1", got)
+ }
+}
+
+// Test if TriClientPool can get timeout fallback correctly
+func TestTriClientPoolGetTimeoutFallback(t *testing.T) {
+ var created int32
+ factory := func() *tri.Client {
+ atomic.AddInt32(&created, 1)
+ return &tri.Client{}
+ }
+
+ pool := NewTriClientPool(0, 1, factory)
+ defer pool.Close()
+
+ cli, err := pool.Get(10 * time.Millisecond)
+ if err != nil {
+ t.Fatalf("first Get() error = %v, want nil", err)
+ }
+ if cli == nil {
+ t.Fatalf("first Get() returned nil client")
+ }
+
+ start := time.Now()
+ fallback, err := pool.Get(5 * time.Millisecond)
+ elapsed := time.Since(start)
+ if err != ErrTriClientPoolTimeout {
+ t.Fatalf("second Get() error = %v, want ErrTriClientPoolTimeout", err)
+ }
+ if fallback == nil {
+ t.Fatalf("second Get() returned nil fallback client")
+ }
+ if pool.fallback != fallback {
+ t.Fatalf("fallback client mismatch: Get() returned %p, pool.fallback = %p", fallback, pool.fallback)
+ }
+ if elapsed < 5*time.Millisecond {
+ t.Fatalf("Get() returned before timeout elapsed: got %v, want >= 5ms", elapsed)
+ }
+ if got := atomic.LoadInt32(&created); got != 2 {
+ t.Fatalf("created clients = %d, want 2 (one normal + one fallback)", got)
+ }
+}
+
+// Test if TriClientPool can close correctly
+func TestTriClientPoolClose(t *testing.T) {
+ pool := NewTriClientPool(1, 1, func() *tri.Client {
+ return &tri.Client{}
+ })
+
+ if pool.Closed() {
+ t.Fatalf("pool should not be closed immediately after creation")
+ }
+
+ pool.Close()
+
+ if !pool.Closed() {
+ t.Fatalf("pool should be closed after Close()")
+ }
+
+ cli, err := pool.Get(1 * time.Millisecond)
+ if cli != nil {
+ t.Fatalf("Get() after Close() should return nil client, got %v", cli)
+ }
+ if err != nil && err != ErrTriClientPoolClosed {
+ t.Fatalf("Get() after Close() error = %v, want nil or ErrTriClientPoolClosed", err)
+ }
+}
+
+// Test if TriClientPool can check expand and shrink correctly
+func TestCheckExpandAndShrink(t *testing.T) {
+ if got := checkExpand(10, 0, 1); got != 2 {
+ t.Fatalf("checkExpand(curSize=10,idle=0,timeouts=1) = %d, want 2", got)
+ }
+ if got := checkExpand(10, 0, 10); got != maxExpandPerCycle {
+ t.Fatalf("checkExpand(..., timeouts=10) = %d, want maxExpandPerCycle(%d)", got, maxExpandPerCycle)
+ }
+
+ if got := checkExpand(10, 1, 0); got != 1 {
+ t.Fatalf("checkExpand(curSize=10,idle=1,timeouts=0) = %d, want 1", got)
+ }
+
+ var streak int
+ for i := 0; i < highIdleStreakLimit-1; i++ {
+ if got := checkShrink(16, 16, &streak); got != 0 {
+ t.Fatalf("checkShrink iteration %d returned %d, want 0 before reaching limit", i, got)
+ }
+ }
+ if got := checkShrink(16, 16, &streak); got <= 0 {
+ t.Fatalf("checkShrink at highIdleStreakLimit returned %d, want > 0", got)
+ }
+}
+
+// TriClientPool Benchmark
+// Benchmark if TriClientPool with different delays
+
+var (
+ testDelays = []time.Duration{
+ 0,
+ 100 * time.Microsecond,
+ 1 * time.Millisecond,
+ 100 * time.Millisecond,
+ }
+)
+
+// setupTripleServer creates an HTTP/2 test server with configurable delay for benchmarks
+func setupTripleServer(delay time.Duration) (*httptest.Server, string) {
+ mux := http.NewServeMux()
+
+ mux.HandleFunc("/test/Echo", func(w http.ResponseWriter, r *http.Request) {
+ time.Sleep(delay)
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(http.StatusOK)
+ w.Write([]byte(`{"echo":"ok"}`))
+ })
+
+ server := httptest.NewUnstartedServer(mux)
+ server.EnableHTTP2 = true
+ server.StartTLS()
+ return server, server.URL + "/test/Echo"
+}
+
+func BenchmarkThroughputComparisonDelay(b *testing.B) {
+ originalLogOutput := log.Writer()
+ log.SetOutput(io.Discard)
+ defer log.SetOutput(originalLogOutput)
+
+ for _, delay := range testDelays {
+ b.Run(fmt.Sprintf("delay_%v", delay), func(b *testing.B) {
+ _, testURL := setupTripleServer(delay)
+ var singleTransport *http.Transport
+
+ b.Run("SingleClient", func(b *testing.B) {
+ b.SetParallelism(10)
+ singleTransport = &http.Transport{
+ TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Review Comment:
## Server hostnames should be verified during SSL/TLS connections
<!--SONAR_ISSUE_KEY:AZrumDowkncTmn-Jpucn-->Enable server hostname verification on this SSL/TLS connection. <p>See more on <a href="https://sonarcloud.io/project/issues?id=apache_dubbo-go&issues=AZrumDowkncTmn-Jpucn&open=AZrumDowkncTmn-Jpucn&pullRequest=3086">SonarQube Cloud</a></p>
[Show more details](https://github.com/apache/dubbo-go/security/code-scanning/62)
##########
protocol/triple/client_pool_test.go:
##########
@@ -0,0 +1,282 @@
+/*
+ * 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 triple
+
+import (
+ "context"
+ "crypto/tls"
+ "fmt"
+ "io"
+ "log"
+ "net/http"
+ "net/http/httptest"
+ "sync/atomic"
+ "testing"
+ "time"
+)
+
+import (
+ tri "dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol"
+)
+
+// TriClientPool Unit Tests
+// Test if TriClientPool can warm up correctly
+func TestNewTriClientPoolWarmUp(t *testing.T) {
+ var created int32
+ factory := func() *tri.Client {
+ atomic.AddInt32(&created, 1)
+ return &tri.Client{}
+ }
+
+ const warmUpSize = 5
+ const maxSize = 10
+
+ pool := NewTriClientPool(warmUpSize, maxSize, factory)
+ defer pool.Close()
+
+ if got := pool.MaxSize(); got != maxSize {
+ t.Fatalf("MaxSize() = %d, want %d", got, maxSize)
+ }
+ if got := len(pool.clients); got != warmUpSize {
+ t.Fatalf("len(pool.clients) = %d, want %d", got, warmUpSize)
+ }
+ if got := atomic.LoadInt32(&created); got != warmUpSize {
+ t.Fatalf("created clients = %d, want %d", got, warmUpSize)
+ }
+}
+
+// Test if TriClientPool can get and put clients correctly
+func TestTriClientPoolGetAndPut_Basic(t *testing.T) {
+ var created int32
+ factory := func() *tri.Client {
+ atomic.AddInt32(&created, 1)
+ return &tri.Client{}
+ }
+
+ pool := NewTriClientPool(1, 1, factory)
+ defer pool.Close()
+
+ cli, err := pool.Get(10 * time.Millisecond)
+ if err != nil {
+ t.Fatalf("Get() error = %v, want nil", err)
+ }
+ if cli == nil {
+ t.Fatalf("Get() returned nil client")
+ }
+ if got := atomic.LoadInt32(&created); got != 1 {
+ t.Fatalf("created clients = %d, want 1", got)
+ }
+
+ pool.Put(cli)
+ if got := len(pool.clients); got != 1 {
+ t.Fatalf("len(pool.clients) after Put = %d, want 1", got)
+ }
+}
+
+// Test if TriClientPool can get timeout fallback correctly
+func TestTriClientPoolGetTimeoutFallback(t *testing.T) {
+ var created int32
+ factory := func() *tri.Client {
+ atomic.AddInt32(&created, 1)
+ return &tri.Client{}
+ }
+
+ pool := NewTriClientPool(0, 1, factory)
+ defer pool.Close()
+
+ cli, err := pool.Get(10 * time.Millisecond)
+ if err != nil {
+ t.Fatalf("first Get() error = %v, want nil", err)
+ }
+ if cli == nil {
+ t.Fatalf("first Get() returned nil client")
+ }
+
+ start := time.Now()
+ fallback, err := pool.Get(5 * time.Millisecond)
+ elapsed := time.Since(start)
+ if err != ErrTriClientPoolTimeout {
+ t.Fatalf("second Get() error = %v, want ErrTriClientPoolTimeout", err)
+ }
+ if fallback == nil {
+ t.Fatalf("second Get() returned nil fallback client")
+ }
+ if pool.fallback != fallback {
+ t.Fatalf("fallback client mismatch: Get() returned %p, pool.fallback = %p", fallback, pool.fallback)
+ }
+ if elapsed < 5*time.Millisecond {
+ t.Fatalf("Get() returned before timeout elapsed: got %v, want >= 5ms", elapsed)
+ }
+ if got := atomic.LoadInt32(&created); got != 2 {
+ t.Fatalf("created clients = %d, want 2 (one normal + one fallback)", got)
+ }
+}
+
+// Test if TriClientPool can close correctly
+func TestTriClientPoolClose(t *testing.T) {
+ pool := NewTriClientPool(1, 1, func() *tri.Client {
+ return &tri.Client{}
+ })
+
+ if pool.Closed() {
+ t.Fatalf("pool should not be closed immediately after creation")
+ }
+
+ pool.Close()
+
+ if !pool.Closed() {
+ t.Fatalf("pool should be closed after Close()")
+ }
+
+ cli, err := pool.Get(1 * time.Millisecond)
+ if cli != nil {
+ t.Fatalf("Get() after Close() should return nil client, got %v", cli)
+ }
+ if err != nil && err != ErrTriClientPoolClosed {
+ t.Fatalf("Get() after Close() error = %v, want nil or ErrTriClientPoolClosed", err)
+ }
+}
+
+// Test if TriClientPool can check expand and shrink correctly
+func TestCheckExpandAndShrink(t *testing.T) {
+ if got := checkExpand(10, 0, 1); got != 2 {
+ t.Fatalf("checkExpand(curSize=10,idle=0,timeouts=1) = %d, want 2", got)
+ }
+ if got := checkExpand(10, 0, 10); got != maxExpandPerCycle {
+ t.Fatalf("checkExpand(..., timeouts=10) = %d, want maxExpandPerCycle(%d)", got, maxExpandPerCycle)
+ }
+
+ if got := checkExpand(10, 1, 0); got != 1 {
+ t.Fatalf("checkExpand(curSize=10,idle=1,timeouts=0) = %d, want 1", got)
+ }
+
+ var streak int
+ for i := 0; i < highIdleStreakLimit-1; i++ {
+ if got := checkShrink(16, 16, &streak); got != 0 {
+ t.Fatalf("checkShrink iteration %d returned %d, want 0 before reaching limit", i, got)
+ }
+ }
+ if got := checkShrink(16, 16, &streak); got <= 0 {
+ t.Fatalf("checkShrink at highIdleStreakLimit returned %d, want > 0", got)
+ }
+}
+
+// TriClientPool Benchmark
+// Benchmark if TriClientPool with different delays
+
+var (
+ testDelays = []time.Duration{
+ 0,
+ 100 * time.Microsecond,
+ 1 * time.Millisecond,
+ 100 * time.Millisecond,
+ }
+)
+
+// setupTripleServer creates an HTTP/2 test server with configurable delay for benchmarks
+func setupTripleServer(delay time.Duration) (*httptest.Server, string) {
+ mux := http.NewServeMux()
+
+ mux.HandleFunc("/test/Echo", func(w http.ResponseWriter, r *http.Request) {
+ time.Sleep(delay)
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(http.StatusOK)
+ w.Write([]byte(`{"echo":"ok"}`))
+ })
+
+ server := httptest.NewUnstartedServer(mux)
+ server.EnableHTTP2 = true
+ server.StartTLS()
+ return server, server.URL + "/test/Echo"
+}
+
+func BenchmarkThroughputComparisonDelay(b *testing.B) {
+ originalLogOutput := log.Writer()
+ log.SetOutput(io.Discard)
+ defer log.SetOutput(originalLogOutput)
+
+ for _, delay := range testDelays {
+ b.Run(fmt.Sprintf("delay_%v", delay), func(b *testing.B) {
+ _, testURL := setupTripleServer(delay)
+ var singleTransport *http.Transport
+
+ b.Run("SingleClient", func(b *testing.B) {
+ b.SetParallelism(10)
+ singleTransport = &http.Transport{
+ TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
+ MaxIdleConns: 100,
+ MaxIdleConnsPerHost: 100,
+ MaxConnsPerHost: 100,
+ }
+
+ client := tri.NewClient(
+ &http.Client{
+ Transport: singleTransport,
+ Timeout: 10 * time.Second,
+ },
+ testURL,
+ )
+
+ b.ResetTimer()
+ b.RunParallel(func(pb *testing.PB) {
+ for pb.Next() {
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ req := tri.NewRequest(map[string]string{"test": "data"})
+ resp := tri.NewResponse(map[string]string{})
+ _ = client.CallUnary(ctx, req, resp)
+ cancel()
+ }
+ })
+ })
+
+ b.Run("ConnectionPool", func(b *testing.B) {
+ b.SetParallelism(10)
+ poolSize := 128
+ pool := NewTriClientPool(16, poolSize, func() *tri.Client {
+ transport := &http.Transport{
+ TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Review Comment:
## Server hostnames should be verified during SSL/TLS connections
<!--SONAR_ISSUE_KEY:AZrumDowkncTmn-Jpucp-->Enable server hostname verification on this SSL/TLS connection. <p>See more on <a href="https://sonarcloud.io/project/issues?id=apache_dubbo-go&issues=AZrumDowkncTmn-Jpucp&open=AZrumDowkncTmn-Jpucp&pullRequest=3086">SonarQube Cloud</a></p>
[Show more details](https://github.com/apache/dubbo-go/security/code-scanning/64)
##########
protocol/triple/client_pool_test.go:
##########
@@ -0,0 +1,282 @@
+/*
+ * 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 triple
+
+import (
+ "context"
+ "crypto/tls"
+ "fmt"
+ "io"
+ "log"
+ "net/http"
+ "net/http/httptest"
+ "sync/atomic"
+ "testing"
+ "time"
+)
+
+import (
+ tri "dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol"
+)
+
+// TriClientPool Unit Tests
+// Test if TriClientPool can warm up correctly
+func TestNewTriClientPoolWarmUp(t *testing.T) {
+ var created int32
+ factory := func() *tri.Client {
+ atomic.AddInt32(&created, 1)
+ return &tri.Client{}
+ }
+
+ const warmUpSize = 5
+ const maxSize = 10
+
+ pool := NewTriClientPool(warmUpSize, maxSize, factory)
+ defer pool.Close()
+
+ if got := pool.MaxSize(); got != maxSize {
+ t.Fatalf("MaxSize() = %d, want %d", got, maxSize)
+ }
+ if got := len(pool.clients); got != warmUpSize {
+ t.Fatalf("len(pool.clients) = %d, want %d", got, warmUpSize)
+ }
+ if got := atomic.LoadInt32(&created); got != warmUpSize {
+ t.Fatalf("created clients = %d, want %d", got, warmUpSize)
+ }
+}
+
+// Test if TriClientPool can get and put clients correctly
+func TestTriClientPoolGetAndPut_Basic(t *testing.T) {
+ var created int32
+ factory := func() *tri.Client {
+ atomic.AddInt32(&created, 1)
+ return &tri.Client{}
+ }
+
+ pool := NewTriClientPool(1, 1, factory)
+ defer pool.Close()
+
+ cli, err := pool.Get(10 * time.Millisecond)
+ if err != nil {
+ t.Fatalf("Get() error = %v, want nil", err)
+ }
+ if cli == nil {
+ t.Fatalf("Get() returned nil client")
+ }
+ if got := atomic.LoadInt32(&created); got != 1 {
+ t.Fatalf("created clients = %d, want 1", got)
+ }
+
+ pool.Put(cli)
+ if got := len(pool.clients); got != 1 {
+ t.Fatalf("len(pool.clients) after Put = %d, want 1", got)
+ }
+}
+
+// Test if TriClientPool can get timeout fallback correctly
+func TestTriClientPoolGetTimeoutFallback(t *testing.T) {
+ var created int32
+ factory := func() *tri.Client {
+ atomic.AddInt32(&created, 1)
+ return &tri.Client{}
+ }
+
+ pool := NewTriClientPool(0, 1, factory)
+ defer pool.Close()
+
+ cli, err := pool.Get(10 * time.Millisecond)
+ if err != nil {
+ t.Fatalf("first Get() error = %v, want nil", err)
+ }
+ if cli == nil {
+ t.Fatalf("first Get() returned nil client")
+ }
+
+ start := time.Now()
+ fallback, err := pool.Get(5 * time.Millisecond)
+ elapsed := time.Since(start)
+ if err != ErrTriClientPoolTimeout {
+ t.Fatalf("second Get() error = %v, want ErrTriClientPoolTimeout", err)
+ }
+ if fallback == nil {
+ t.Fatalf("second Get() returned nil fallback client")
+ }
+ if pool.fallback != fallback {
+ t.Fatalf("fallback client mismatch: Get() returned %p, pool.fallback = %p", fallback, pool.fallback)
+ }
+ if elapsed < 5*time.Millisecond {
+ t.Fatalf("Get() returned before timeout elapsed: got %v, want >= 5ms", elapsed)
+ }
+ if got := atomic.LoadInt32(&created); got != 2 {
+ t.Fatalf("created clients = %d, want 2 (one normal + one fallback)", got)
+ }
+}
+
+// Test if TriClientPool can close correctly
+func TestTriClientPoolClose(t *testing.T) {
+ pool := NewTriClientPool(1, 1, func() *tri.Client {
+ return &tri.Client{}
+ })
+
+ if pool.Closed() {
+ t.Fatalf("pool should not be closed immediately after creation")
+ }
+
+ pool.Close()
+
+ if !pool.Closed() {
+ t.Fatalf("pool should be closed after Close()")
+ }
+
+ cli, err := pool.Get(1 * time.Millisecond)
+ if cli != nil {
+ t.Fatalf("Get() after Close() should return nil client, got %v", cli)
+ }
+ if err != nil && err != ErrTriClientPoolClosed {
+ t.Fatalf("Get() after Close() error = %v, want nil or ErrTriClientPoolClosed", err)
+ }
+}
+
+// Test if TriClientPool can check expand and shrink correctly
+func TestCheckExpandAndShrink(t *testing.T) {
+ if got := checkExpand(10, 0, 1); got != 2 {
+ t.Fatalf("checkExpand(curSize=10,idle=0,timeouts=1) = %d, want 2", got)
+ }
+ if got := checkExpand(10, 0, 10); got != maxExpandPerCycle {
+ t.Fatalf("checkExpand(..., timeouts=10) = %d, want maxExpandPerCycle(%d)", got, maxExpandPerCycle)
+ }
+
+ if got := checkExpand(10, 1, 0); got != 1 {
+ t.Fatalf("checkExpand(curSize=10,idle=1,timeouts=0) = %d, want 1", got)
+ }
+
+ var streak int
+ for i := 0; i < highIdleStreakLimit-1; i++ {
+ if got := checkShrink(16, 16, &streak); got != 0 {
+ t.Fatalf("checkShrink iteration %d returned %d, want 0 before reaching limit", i, got)
+ }
+ }
+ if got := checkShrink(16, 16, &streak); got <= 0 {
+ t.Fatalf("checkShrink at highIdleStreakLimit returned %d, want > 0", got)
+ }
+}
+
+// TriClientPool Benchmark
+// Benchmark if TriClientPool with different delays
+
+var (
+ testDelays = []time.Duration{
+ 0,
+ 100 * time.Microsecond,
+ 1 * time.Millisecond,
+ 100 * time.Millisecond,
+ }
+)
+
+// setupTripleServer creates an HTTP/2 test server with configurable delay for benchmarks
+func setupTripleServer(delay time.Duration) (*httptest.Server, string) {
+ mux := http.NewServeMux()
+
+ mux.HandleFunc("/test/Echo", func(w http.ResponseWriter, r *http.Request) {
+ time.Sleep(delay)
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(http.StatusOK)
+ w.Write([]byte(`{"echo":"ok"}`))
+ })
+
+ server := httptest.NewUnstartedServer(mux)
+ server.EnableHTTP2 = true
+ server.StartTLS()
+ return server, server.URL + "/test/Echo"
+}
+
+func BenchmarkThroughputComparisonDelay(b *testing.B) {
+ originalLogOutput := log.Writer()
+ log.SetOutput(io.Discard)
+ defer log.SetOutput(originalLogOutput)
+
+ for _, delay := range testDelays {
+ b.Run(fmt.Sprintf("delay_%v", delay), func(b *testing.B) {
+ _, testURL := setupTripleServer(delay)
+ var singleTransport *http.Transport
+
+ b.Run("SingleClient", func(b *testing.B) {
+ b.SetParallelism(10)
+ singleTransport = &http.Transport{
+ TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Review Comment:
## Server certificates should be verified during SSL/TLS connections
<!--SONAR_ISSUE_KEY:AZrumDowkncTmn-Jpucm-->Enable server certificate validation on this SSL/TLS connection. <p>See more on <a href="https://sonarcloud.io/project/issues?id=apache_dubbo-go&issues=AZrumDowkncTmn-Jpucm&open=AZrumDowkncTmn-Jpucm&pullRequest=3086">SonarQube Cloud</a></p>
[Show more details](https://github.com/apache/dubbo-go/security/code-scanning/61)
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "Aetherance (via GitHub)" <gi...@apache.org>.
Aetherance commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3619762012
> `protocol/triple/client_pool.go` needs sound unit tests, please write a test file for it
done
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "Aetherance (via GitHub)" <gi...@apache.org>.
Aetherance commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3619762754
> `protocol/triple/client_pool.go` needs sound unit tests, please write a test file for it
done @Alanxtl
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] refactor(triple): upgrade triple client to service-level abstraction [dubbo-go]
Posted by "Aetherance (via GitHub)" <gi...@apache.org>.
Aetherance commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3767852962
@AlexStocks confliction fixed
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3592392013
## [](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086) **Quality Gate passed**
Issues
 [1 New issue](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=ACCEPTED)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_coverage&view=list)
 [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_duplicated_lines_density&view=list)
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086)
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] refactor(triple): upgrade triple client to service-level abstraction [dubbo-go]
Posted by "Aetherance (via GitHub)" <gi...@apache.org>.
Aetherance commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3733503802
This PR originally implemented a TriClientPool, but the design was refactored to a service-level client.
This simplifies management and reduces memory overhead.
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3616841450
## [](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086) **Quality Gate failed**
Failed conditions
 [D Security Rating on New Code](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086) (required ≥ A)
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086)
##
 Catch issues before they fail your Quality Gate with our IDE extension  [SonarQube for IDE](https://www.sonarsource.com/products/sonarlint/features/connected-mode/?referrer=pull-request)
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3636244743
## [](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086) **Quality Gate passed**
Issues
 [0 New issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=ACCEPTED)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_coverage&view=list)
 [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_duplicated_lines_density&view=list)
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086)
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3618242266
## [](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086) **Quality Gate failed**
Failed conditions
 [D Security Rating on New Code](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086) (required ≥ A)
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086)
##
 Catch issues before they fail your Quality Gate with our IDE extension  [SonarQube for IDE](https://www.sonarsource.com/products/sonarlint/features/connected-mode/?referrer=pull-request)
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "marsevilspirit (via GitHub)" <gi...@apache.org>.
marsevilspirit commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3644939319
Enhance the PR description by detailing the implementation approach, making it easier for future reference.
Refer to this link: https://github.com/apache/dubbo-go/pull/2972
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "Alanxtl (via GitHub)" <gi...@apache.org>.
Alanxtl commented on code in PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#discussion_r2613036888
##########
protocol/triple/triple_protocol/client.go:
##########
@@ -117,8 +121,27 @@ func NewClient(httpClient HTTPClient, url string, options ...ClientOption) *Clie
return client
}
+func buildMethodLevelReqSpec(serviceLevelReqSpec *Spec, method string) (Spec, error) {
+ if serviceLevelReqSpec == nil {
+ return Spec{}, fmt.Errorf("serviceLevelReqSpec is nil")
+ }
+ methodLevelSpec := *serviceLevelReqSpec
+
+ methodLevelURL, err := joinPath(methodLevelSpec.Procedure, method)
+ if err != nil {
+ return Spec{}, fmt.Errorf("JoinPath failed for procedure %s, method %s", methodLevelSpec.Procedure, method)
+ }
+
+ methodLevelSpec.Procedure = methodLevelURL
+ return methodLevelSpec, nil
+}
+
+func joinPath(base string, elem ...string) (result string, err error) {
+ return url.JoinPath(base, elem...)
+}
Review Comment:
把这个函数又包裹了一层的意义是什么
##########
protocol/triple/client_pool.go:
##########
@@ -0,0 +1,312 @@
+/*
+ * 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 triple
+
+import (
+ "errors"
+ "sync"
+ "time"
+)
+
+import (
+ "dubbo.apache.org/dubbo-go/v3/common/constant"
+ tri "dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol"
+)
+
+type ClientPool interface {
+ Get(timeout time.Duration) (*tri.Client, error)
+ Put(client *tri.Client)
+ Close()
+
+ MaxSize() int
+ Closed() bool
+}
+
+var (
+ ErrTriClientPoolClosed = errors.New("tri client pool is closed")
+ ErrTriClientPoolCloseWhenEmpty = errors.New("empty tri client pool close")
+)
+
+type TriClientPool struct {
+ clients chan *tri.Client
+ factory func() *tri.Client
+ mu sync.Mutex
+ maxSize int
+ curSize int
+ closed bool
+ getTimeouts int // recent timeout count, used to trigger expansion
+ lastScaleTime time.Time
+
+ fallback *tri.Client
+ consecutiveHighIdle int
+}
+
+// NewTriClientPool creates a new TriClientPool with optional warm-up.
+// warmUpSize specifies how many clients to pre-create and add to the pool.
+// If warmUpSize is 0, no warm-up is performed.
+// If warmUpSize > maxSize, it will be capped to maxSize.
+func NewTriClientPool(warmUpSize, maxSize int, factory func() *tri.Client) *TriClientPool {
+ if warmUpSize > maxSize {
+ warmUpSize = maxSize
+ }
+ if warmUpSize < 0 {
+ warmUpSize = 0
+ }
+
+ pool := &TriClientPool{
+ clients: make(chan *tri.Client, maxSize),
+ factory: factory,
+ maxSize: maxSize,
+ curSize: warmUpSize,
+ }
+
+ for i := 0; i < warmUpSize; i++ {
+ cli := factory()
+ select {
+ case pool.clients <- cli:
+ default:
+ pool.curSize--
+ }
+ }
+
+ return pool
+}
+
+// TriClient Get method
+// timeout means how long to wait for an available client.
+// TriClientPool keeps a fallback client pointer. If Get() times out,
+// and the pool cannot expand at that moment, the pool will return fallback.
+// This ensures there is at least one usable client.
+// Get tries a non-blocking receive first. If that fails, it tries to expand.
+// If expansion is not allowed, it waits for a client up to timeout.
+// After timeout, if still no client, Get() returns ErrTimeout with fallback.
+func (p *TriClientPool) Get(timeout time.Duration) (*tri.Client, error) {
+ now := time.Now()
+ if now.Sub(p.lastScaleTime) > constant.AutoScalerPeriod {
+ p.autoScaler()
+ }
+
+ select {
+ case cli := <-p.clients:
+ return cli, nil
+ default:
+ }
+
+ p.mu.Lock()
+ if p.closed {
+ p.mu.Unlock()
+ return nil, ErrTriClientPoolClosed
+ }
+ // try to expand
+ if p.curSize < p.maxSize {
+ p.curSize++
+ p.mu.Unlock()
+ cli := p.factory()
+ return cli, nil
+ }
+ p.mu.Unlock()
+
+ select {
+ case cli, ok := <-p.clients:
+ if !ok {
+ return nil, ErrTriClientPoolClosed
+ }
+ return cli, nil
+ case <-time.After(timeout):
+ p.recordTimeout()
+ p.mu.Lock()
+ if p.fallback == nil {
+ p.fallback = p.factory()
+ }
+ p.mu.Unlock()
+ return p.fallback, nil
+ }
+}
+
+// TriClient Put method
+// Put tries to put a tri.Client back into the pool.
+// If it fails, Put will drop the client and notify the pool.
+// Dropping a client is part of shrinking.
+func (p *TriClientPool) Put(c *tri.Client) {
+ now := time.Now()
+ if now.Sub(p.lastScaleTime) > constant.AutoScalerPeriod {
+ p.autoScaler()
+ }
+
+ if c == nil {
+ return
+ }
+
+ p.mu.Lock()
+ closed := p.closed
+ p.mu.Unlock()
+ if closed {
+ return
+ }
+
+ select {
+ case p.clients <- c:
+ default:
+ p.mu.Lock()
+ p.curSize--
+ p.mu.Unlock()
+ }
Review Comment:
这块逻辑对吗? 会不会发生尝试加两次锁的情况
另外,最后那块解锁使用defer
##########
protocol/triple/triple_protocol/compression_test.go:
##########
@@ -57,7 +57,7 @@ func TestAcceptEncodingOrdering(t *testing.T) {
withFakeBrotli,
withGzip(),
)
- _ = client.CallUnary(context.Background(), NewRequest(&emptypb.Empty{}), NewResponse(&emptypb.Empty{}))
+ _ = client.CallUnary(context.Background(), NewRequest(&emptypb.Empty{}), NewResponse(&emptypb.Empty{}), "AcceptEncodingOrdering")
Review Comment:
extract `AcceptEncodingOrdering` as a const
##########
protocol/triple/client_pool.go:
##########
@@ -0,0 +1,312 @@
+/*
+ * 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 triple
+
+import (
+ "errors"
+ "sync"
+ "time"
+)
+
+import (
+ "dubbo.apache.org/dubbo-go/v3/common/constant"
+ tri "dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol"
+)
+
+type ClientPool interface {
+ Get(timeout time.Duration) (*tri.Client, error)
+ Put(client *tri.Client)
+ Close()
+
+ MaxSize() int
+ Closed() bool
+}
+
+var (
+ ErrTriClientPoolClosed = errors.New("tri client pool is closed")
+ ErrTriClientPoolCloseWhenEmpty = errors.New("empty tri client pool close")
+)
+
+type TriClientPool struct {
+ clients chan *tri.Client
+ factory func() *tri.Client
+ mu sync.Mutex
+ maxSize int
+ curSize int
+ closed bool
+ getTimeouts int // recent timeout count, used to trigger expansion
+ lastScaleTime time.Time
+
+ fallback *tri.Client
+ consecutiveHighIdle int
+}
+
+// NewTriClientPool creates a new TriClientPool with optional warm-up.
+// warmUpSize specifies how many clients to pre-create and add to the pool.
+// If warmUpSize is 0, no warm-up is performed.
+// If warmUpSize > maxSize, it will be capped to maxSize.
+func NewTriClientPool(warmUpSize, maxSize int, factory func() *tri.Client) *TriClientPool {
+ if warmUpSize > maxSize {
+ warmUpSize = maxSize
+ }
+ if warmUpSize < 0 {
+ warmUpSize = 0
+ }
+
+ pool := &TriClientPool{
+ clients: make(chan *tri.Client, maxSize),
+ factory: factory,
+ maxSize: maxSize,
+ curSize: warmUpSize,
+ }
+
+ for i := 0; i < warmUpSize; i++ {
+ cli := factory()
+ select {
+ case pool.clients <- cli:
+ default:
+ pool.curSize--
+ }
+ }
+
+ return pool
+}
+
+// TriClient Get method
+// timeout means how long to wait for an available client.
+// TriClientPool keeps a fallback client pointer. If Get() times out,
+// and the pool cannot expand at that moment, the pool will return fallback.
+// This ensures there is at least one usable client.
+// Get tries a non-blocking receive first. If that fails, it tries to expand.
+// If expansion is not allowed, it waits for a client up to timeout.
+// After timeout, if still no client, Get() returns ErrTimeout with fallback.
+func (p *TriClientPool) Get(timeout time.Duration) (*tri.Client, error) {
+ now := time.Now()
+ if now.Sub(p.lastScaleTime) > constant.AutoScalerPeriod {
+ p.autoScaler()
+ }
+
+ select {
+ case cli := <-p.clients:
+ return cli, nil
+ default:
+ }
+
+ p.mu.Lock()
+ if p.closed {
+ p.mu.Unlock()
+ return nil, ErrTriClientPoolClosed
+ }
+ // try to expand
+ if p.curSize < p.maxSize {
+ p.curSize++
+ p.mu.Unlock()
+ cli := p.factory()
+ return cli, nil
+ }
+ p.mu.Unlock()
+
+ select {
+ case cli, ok := <-p.clients:
+ if !ok {
+ return nil, ErrTriClientPoolClosed
+ }
+ return cli, nil
+ case <-time.After(timeout):
+ p.recordTimeout()
+ p.mu.Lock()
+ if p.fallback == nil {
+ p.fallback = p.factory()
+ }
+ p.mu.Unlock()
+ return p.fallback, nil
+ }
+}
+
+// TriClient Put method
+// Put tries to put a tri.Client back into the pool.
+// If it fails, Put will drop the client and notify the pool.
+// Dropping a client is part of shrinking.
+func (p *TriClientPool) Put(c *tri.Client) {
+ now := time.Now()
+ if now.Sub(p.lastScaleTime) > constant.AutoScalerPeriod {
+ p.autoScaler()
+ }
+
+ if c == nil {
+ return
+ }
+
+ p.mu.Lock()
+ closed := p.closed
+ p.mu.Unlock()
+ if closed {
+ return
+ }
+
+ select {
+ case p.clients <- c:
+ default:
+ p.mu.Lock()
+ p.curSize--
+ p.mu.Unlock()
+ }
+}
+
+// close removes all clients from the channel and then closes it.
+func (p *TriClientPool) Close() {
+ p.mu.Lock()
+ if p.closed {
+ p.mu.Unlock()
+ return
+ }
+ p.closed = true
+
+ for len(p.clients) > 0 {
+ <-p.clients
+ p.curSize--
+ }
+
+ close(p.clients)
+ p.mu.Unlock()
+}
+
+func (p *TriClientPool) MaxSize() int {
+ return p.maxSize
+}
+
+func (p *TriClientPool) Closed() bool {
+ return p.closed
+}
+
+// autoScaler performs scaling check.
+// It is triggered by Get() or Put()
+// If the timeout count is high, autoScaler tends to expand.
+// If the idle client count is often high, autoScaler tends to shrink.
+func (p *TriClientPool) autoScaler() {
+ p.mu.Lock()
+
+ now := time.Now()
+ if now.Sub(p.lastScaleTime) < constant.AutoScalerPeriod {
+ p.mu.Unlock()
+ return
+ }
+
+ if p.closed {
+ p.mu.Unlock()
+ return
+ }
+
+ p.lastScaleTime = now
+
+ curSize := p.curSize
+ idle := len(p.clients)
+ timeouts := p.getTimeouts
+ p.getTimeouts = 0
+
+ needExpand := checkExpand(curSize, idle, timeouts)
+ if needExpand != 0 {
+ p.consecutiveHighIdle = 0
+ p.mu.Unlock()
+ p.expand(needExpand)
+ return
+ }
+
+ needShrink := checkShrink(curSize, idle, &p.consecutiveHighIdle)
+ p.mu.Unlock()
+ if needShrink != 0 {
+ p.shrink(needShrink)
+ }
+}
+
+// expand creates n more clients
+func (p *TriClientPool) expand(n int) {
+ for i := 0; i < n; i++ {
+ p.mu.Lock()
+ if p.curSize >= p.maxSize {
+ p.mu.Unlock()
+ return
+ }
+ p.curSize++
+ p.mu.Unlock()
+
+ cli := p.factory()
+ p.Put(cli)
+ }
+}
+
+// shrink removes n clients
+func (p *TriClientPool) shrink(n int) {
+ for i := 0; i < n; i++ {
+ select {
+ case <-p.clients:
+ p.mu.Lock()
+ p.curSize--
+ p.mu.Unlock()
+ default:
+ return
+ }
+ }
+}
+
+// record timeout count
+func (p *TriClientPool) recordTimeout() {
+ p.mu.Lock()
+ p.getTimeouts++
+ p.mu.Unlock()
Review Comment:
```suggestion
p.mu.Lock()
defer p.mu.Unlock()
p.getTimeouts++
```
##########
protocol/triple/client.go:
##########
@@ -213,111 +246,88 @@ func newClientManager(url *common.URL) (*clientManager, error) {
callProtocol = constant.CallHTTP2
}
+ transport, err := buildTransport(callProtocol, cfg, keepAliveInterval, keepAliveTimeout, tlsFlag)
+ if err != nil {
+ return nil, err
+ }
+ httpClient := &http.Client{
+ Transport: transport,
+ }
+ perClientTransport := url.GetParamBool("tri.per_client_transport", false)
+
+ var baseTriURL string
+ baseTriURL = strings.TrimPrefix(url.Location, httpPrefix)
+ baseTriURL = strings.TrimPrefix(baseTriURL, httpsPrefix)
+ if tlsFlag {
+ baseTriURL = httpsPrefix + baseTriURL
+ } else {
+ baseTriURL = httpPrefix + baseTriURL
+ }
+
+ triURL, err := joinPath(baseTriURL, url.Interface())
+ if err != nil {
+ return nil, fmt.Errorf("JoinPath failed for base %s, interface %s", baseTriURL, url.Interface())
+ }
+
+ pool := NewTriClientPool(constant.DefaultClientWarmingUp, constant.TriClientPoolMaxSize, func() *tri.Client {
+ if perClientTransport {
+ dedicatedTransport, buildErr := buildTransport(callProtocol, cfg, keepAliveInterval, keepAliveTimeout, tlsFlag)
+ if buildErr != nil {
+ return tri.NewClient(httpClient, triURL, cliOpts...)
+ }
+ return tri.NewClient(&http.Client{Transport: dedicatedTransport}, triURL, cliOpts...)
+ }
+ return tri.NewClient(httpClient, triURL, cliOpts...)
+ })
+
+ return &clientManager{
+ isIDL: isIDL,
+ triGuard: &sync.RWMutex{},
+ triClients: pool,
+ }, nil
+}
+
+func buildTransport(callProtocol string, cfg *tls.Config, keepAliveInterval, keepAliveTimeout time.Duration, tlsFlag bool) (http.RoundTripper, error) {
switch callProtocol {
- // This case might be for backward compatibility,
- // it's not useful for the Triple protocol, HTTP/1 lacks trailer functionality.
- // Triple protocol only supports HTTP/2 and HTTP/3.
case constant.CallHTTP:
- transport = &http.Transport{
+ return &http.Transport{
TLSClientConfig: cfg,
- }
- cliOpts = append(cliOpts, tri.WithTriple())
+ }, nil
case constant.CallHTTP2:
- // TODO: Enrich the http2 transport config for triple protocol.
if tlsFlag {
- transport = &http2.Transport{
+ return &http2.Transport{
TLSClientConfig: cfg,
ReadIdleTimeout: keepAliveInterval,
PingTimeout: keepAliveTimeout,
- }
- } else {
- transport = &http2.Transport{
- DialTLSContext: func(_ context.Context, network, addr string, _ *tls.Config) (net.Conn, error) {
- return net.Dial(network, addr)
- },
- AllowHTTP: true,
- ReadIdleTimeout: keepAliveInterval,
- PingTimeout: keepAliveTimeout,
- }
+ }, nil
}
+ return &http2.Transport{
+ DialTLSContext: func(_ context.Context, network, addr string, _ *tls.Config) (net.Conn, error) {
+ return net.Dial(network, addr)
+ },
+ AllowHTTP: true,
+ ReadIdleTimeout: keepAliveInterval,
+ PingTimeout: keepAliveTimeout,
+ }, nil
case constant.CallHTTP3:
if !tlsFlag {
return nil, fmt.Errorf("TRIPLE http3 client must have TLS config, but TLS config is nil")
}
-
- // TODO: Enrich the http3 transport config for triple protocol.
- transport = &http3.Transport{
+ return &http3.Transport{
TLSClientConfig: cfg,
QUICConfig: &quic.Config{
- // ref: https://quic-go.net/docs/quic/connection/#keeping-a-connection-alive
KeepAlivePeriod: keepAliveInterval,
- // ref: https://quic-go.net/docs/quic/connection/#idle-timeout
Review Comment:
keep these two ref comment
##########
protocol/triple/client_pool.go:
##########
@@ -0,0 +1,312 @@
+/*
+ * 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 triple
+
+import (
+ "errors"
+ "sync"
+ "time"
+)
+
+import (
+ "dubbo.apache.org/dubbo-go/v3/common/constant"
+ tri "dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol"
+)
+
+type ClientPool interface {
+ Get(timeout time.Duration) (*tri.Client, error)
+ Put(client *tri.Client)
+ Close()
+
+ MaxSize() int
+ Closed() bool
+}
+
+var (
+ ErrTriClientPoolClosed = errors.New("tri client pool is closed")
+ ErrTriClientPoolCloseWhenEmpty = errors.New("empty tri client pool close")
+)
+
+type TriClientPool struct {
+ clients chan *tri.Client
+ factory func() *tri.Client
+ mu sync.Mutex
+ maxSize int
+ curSize int
+ closed bool
+ getTimeouts int // recent timeout count, used to trigger expansion
+ lastScaleTime time.Time
+
+ fallback *tri.Client
+ consecutiveHighIdle int
+}
+
+// NewTriClientPool creates a new TriClientPool with optional warm-up.
+// warmUpSize specifies how many clients to pre-create and add to the pool.
+// If warmUpSize is 0, no warm-up is performed.
+// If warmUpSize > maxSize, it will be capped to maxSize.
+func NewTriClientPool(warmUpSize, maxSize int, factory func() *tri.Client) *TriClientPool {
+ if warmUpSize > maxSize {
+ warmUpSize = maxSize
+ }
+ if warmUpSize < 0 {
+ warmUpSize = 0
+ }
+
+ pool := &TriClientPool{
+ clients: make(chan *tri.Client, maxSize),
+ factory: factory,
+ maxSize: maxSize,
+ curSize: warmUpSize,
+ }
+
+ for i := 0; i < warmUpSize; i++ {
+ cli := factory()
+ select {
+ case pool.clients <- cli:
+ default:
+ pool.curSize--
+ }
+ }
+
+ return pool
+}
+
+// TriClient Get method
+// timeout means how long to wait for an available client.
+// TriClientPool keeps a fallback client pointer. If Get() times out,
+// and the pool cannot expand at that moment, the pool will return fallback.
+// This ensures there is at least one usable client.
+// Get tries a non-blocking receive first. If that fails, it tries to expand.
+// If expansion is not allowed, it waits for a client up to timeout.
+// After timeout, if still no client, Get() returns ErrTimeout with fallback.
+func (p *TriClientPool) Get(timeout time.Duration) (*tri.Client, error) {
+ now := time.Now()
+ if now.Sub(p.lastScaleTime) > constant.AutoScalerPeriod {
+ p.autoScaler()
+ }
+
+ select {
+ case cli := <-p.clients:
+ return cli, nil
+ default:
+ }
+
+ p.mu.Lock()
+ if p.closed {
+ p.mu.Unlock()
+ return nil, ErrTriClientPoolClosed
+ }
+ // try to expand
+ if p.curSize < p.maxSize {
+ p.curSize++
+ p.mu.Unlock()
+ cli := p.factory()
+ return cli, nil
+ }
+ p.mu.Unlock()
+
+ select {
+ case cli, ok := <-p.clients:
+ if !ok {
+ return nil, ErrTriClientPoolClosed
+ }
+ return cli, nil
+ case <-time.After(timeout):
+ p.recordTimeout()
+ p.mu.Lock()
+ if p.fallback == nil {
+ p.fallback = p.factory()
+ }
+ p.mu.Unlock()
+ return p.fallback, nil
+ }
+}
+
+// TriClient Put method
+// Put tries to put a tri.Client back into the pool.
+// If it fails, Put will drop the client and notify the pool.
+// Dropping a client is part of shrinking.
+func (p *TriClientPool) Put(c *tri.Client) {
+ now := time.Now()
+ if now.Sub(p.lastScaleTime) > constant.AutoScalerPeriod {
+ p.autoScaler()
+ }
+
+ if c == nil {
+ return
+ }
+
+ p.mu.Lock()
+ closed := p.closed
+ p.mu.Unlock()
+ if closed {
+ return
+ }
+
+ select {
+ case p.clients <- c:
+ default:
+ p.mu.Lock()
+ p.curSize--
+ p.mu.Unlock()
+ }
+}
+
+// close removes all clients from the channel and then closes it.
+func (p *TriClientPool) Close() {
+ p.mu.Lock()
+ if p.closed {
+ p.mu.Unlock()
+ return
+ }
+ p.closed = true
+
+ for len(p.clients) > 0 {
+ <-p.clients
+ p.curSize--
+ }
+
+ close(p.clients)
+ p.mu.Unlock()
+}
Review Comment:
```suggestion
func (p *TriClientPool) Close() {
p.mu.Lock()
defer p.mu.Unlock()
if p.closed {
return
}
p.closed = true
for len(p.clients) > 0 {
<-p.clients
p.curSize--
}
close(p.clients)
}
```
##########
protocol/triple/client.go:
##########
@@ -213,111 +246,88 @@ func newClientManager(url *common.URL) (*clientManager, error) {
callProtocol = constant.CallHTTP2
}
+ transport, err := buildTransport(callProtocol, cfg, keepAliveInterval, keepAliveTimeout, tlsFlag)
+ if err != nil {
+ return nil, err
+ }
+ httpClient := &http.Client{
+ Transport: transport,
+ }
+ perClientTransport := url.GetParamBool("tri.per_client_transport", false)
+
+ var baseTriURL string
+ baseTriURL = strings.TrimPrefix(url.Location, httpPrefix)
+ baseTriURL = strings.TrimPrefix(baseTriURL, httpsPrefix)
+ if tlsFlag {
+ baseTriURL = httpsPrefix + baseTriURL
+ } else {
+ baseTriURL = httpPrefix + baseTriURL
+ }
+
+ triURL, err := joinPath(baseTriURL, url.Interface())
+ if err != nil {
+ return nil, fmt.Errorf("JoinPath failed for base %s, interface %s", baseTriURL, url.Interface())
+ }
+
+ pool := NewTriClientPool(constant.DefaultClientWarmingUp, constant.TriClientPoolMaxSize, func() *tri.Client {
+ if perClientTransport {
+ dedicatedTransport, buildErr := buildTransport(callProtocol, cfg, keepAliveInterval, keepAliveTimeout, tlsFlag)
+ if buildErr != nil {
+ return tri.NewClient(httpClient, triURL, cliOpts...)
+ }
+ return tri.NewClient(&http.Client{Transport: dedicatedTransport}, triURL, cliOpts...)
+ }
+ return tri.NewClient(httpClient, triURL, cliOpts...)
+ })
+
+ return &clientManager{
+ isIDL: isIDL,
+ triGuard: &sync.RWMutex{},
+ triClients: pool,
+ }, nil
+}
+
+func buildTransport(callProtocol string, cfg *tls.Config, keepAliveInterval, keepAliveTimeout time.Duration, tlsFlag bool) (http.RoundTripper, error) {
switch callProtocol {
- // This case might be for backward compatibility,
- // it's not useful for the Triple protocol, HTTP/1 lacks trailer functionality.
- // Triple protocol only supports HTTP/2 and HTTP/3.
case constant.CallHTTP:
- transport = &http.Transport{
+ return &http.Transport{
TLSClientConfig: cfg,
- }
- cliOpts = append(cliOpts, tri.WithTriple())
+ }, nil
case constant.CallHTTP2:
- // TODO: Enrich the http2 transport config for triple protocol.
if tlsFlag {
- transport = &http2.Transport{
+ return &http2.Transport{
TLSClientConfig: cfg,
ReadIdleTimeout: keepAliveInterval,
PingTimeout: keepAliveTimeout,
- }
- } else {
- transport = &http2.Transport{
- DialTLSContext: func(_ context.Context, network, addr string, _ *tls.Config) (net.Conn, error) {
- return net.Dial(network, addr)
- },
- AllowHTTP: true,
- ReadIdleTimeout: keepAliveInterval,
- PingTimeout: keepAliveTimeout,
- }
+ }, nil
}
+ return &http2.Transport{
+ DialTLSContext: func(_ context.Context, network, addr string, _ *tls.Config) (net.Conn, error) {
+ return net.Dial(network, addr)
+ },
+ AllowHTTP: true,
+ ReadIdleTimeout: keepAliveInterval,
+ PingTimeout: keepAliveTimeout,
+ }, nil
case constant.CallHTTP3:
if !tlsFlag {
return nil, fmt.Errorf("TRIPLE http3 client must have TLS config, but TLS config is nil")
}
-
- // TODO: Enrich the http3 transport config for triple protocol.
- transport = &http3.Transport{
+ return &http3.Transport{
TLSClientConfig: cfg,
QUICConfig: &quic.Config{
- // ref: https://quic-go.net/docs/quic/connection/#keeping-a-connection-alive
KeepAlivePeriod: keepAliveInterval,
- // ref: https://quic-go.net/docs/quic/connection/#idle-timeout
- MaxIdleTimeout: keepAliveTimeout,
+ MaxIdleTimeout: keepAliveTimeout,
},
- }
-
- logger.Infof("Triple http3 client transport init successfully")
+ }, nil
case constant.CallHTTP2AndHTTP3:
if !tlsFlag {
return nil, fmt.Errorf("TRIPLE HTTP/2 and HTTP/3 client must have TLS config, but TLS config is nil")
}
-
- // Create a dual transport that can handle both HTTP/2 and HTTP/3
- transport = newDualTransport(cfg, keepAliveInterval, keepAliveTimeout)
- logger.Infof("Triple HTTP/2 and HTTP/3 client transport init successfully")
Review Comment:
keep these two `logger.info`, and make the info message unify
##########
protocol/triple/client_pool.go:
##########
@@ -0,0 +1,312 @@
+/*
+ * 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 triple
+
+import (
+ "errors"
+ "sync"
+ "time"
+)
+
+import (
+ "dubbo.apache.org/dubbo-go/v3/common/constant"
+ tri "dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol"
+)
+
+type ClientPool interface {
+ Get(timeout time.Duration) (*tri.Client, error)
+ Put(client *tri.Client)
+ Close()
+
+ MaxSize() int
+ Closed() bool
+}
+
+var (
+ ErrTriClientPoolClosed = errors.New("tri client pool is closed")
+ ErrTriClientPoolCloseWhenEmpty = errors.New("empty tri client pool close")
+)
+
+type TriClientPool struct {
+ clients chan *tri.Client
+ factory func() *tri.Client
+ mu sync.Mutex
+ maxSize int
+ curSize int
+ closed bool
+ getTimeouts int // recent timeout count, used to trigger expansion
+ lastScaleTime time.Time
+
+ fallback *tri.Client
+ consecutiveHighIdle int
+}
+
+// NewTriClientPool creates a new TriClientPool with optional warm-up.
+// warmUpSize specifies how many clients to pre-create and add to the pool.
+// If warmUpSize is 0, no warm-up is performed.
+// If warmUpSize > maxSize, it will be capped to maxSize.
+func NewTriClientPool(warmUpSize, maxSize int, factory func() *tri.Client) *TriClientPool {
+ if warmUpSize > maxSize {
+ warmUpSize = maxSize
+ }
+ if warmUpSize < 0 {
+ warmUpSize = 0
+ }
+
+ pool := &TriClientPool{
+ clients: make(chan *tri.Client, maxSize),
+ factory: factory,
+ maxSize: maxSize,
+ curSize: warmUpSize,
+ }
+
+ for i := 0; i < warmUpSize; i++ {
+ cli := factory()
+ select {
+ case pool.clients <- cli:
+ default:
+ pool.curSize--
+ }
+ }
+
+ return pool
+}
+
+// TriClient Get method
+// timeout means how long to wait for an available client.
+// TriClientPool keeps a fallback client pointer. If Get() times out,
+// and the pool cannot expand at that moment, the pool will return fallback.
+// This ensures there is at least one usable client.
+// Get tries a non-blocking receive first. If that fails, it tries to expand.
+// If expansion is not allowed, it waits for a client up to timeout.
+// After timeout, if still no client, Get() returns ErrTimeout with fallback.
+func (p *TriClientPool) Get(timeout time.Duration) (*tri.Client, error) {
+ now := time.Now()
+ if now.Sub(p.lastScaleTime) > constant.AutoScalerPeriod {
+ p.autoScaler()
+ }
+
+ select {
+ case cli := <-p.clients:
+ return cli, nil
+ default:
+ }
+
+ p.mu.Lock()
+ if p.closed {
+ p.mu.Unlock()
+ return nil, ErrTriClientPoolClosed
+ }
+ // try to expand
+ if p.curSize < p.maxSize {
+ p.curSize++
+ p.mu.Unlock()
+ cli := p.factory()
+ return cli, nil
+ }
+ p.mu.Unlock()
+
+ select {
+ case cli, ok := <-p.clients:
+ if !ok {
+ return nil, ErrTriClientPoolClosed
+ }
+ return cli, nil
+ case <-time.After(timeout):
+ p.recordTimeout()
+ p.mu.Lock()
+ if p.fallback == nil {
+ p.fallback = p.factory()
+ }
+ p.mu.Unlock()
+ return p.fallback, nil
+ }
+}
+
+// TriClient Put method
+// Put tries to put a tri.Client back into the pool.
+// If it fails, Put will drop the client and notify the pool.
+// Dropping a client is part of shrinking.
+func (p *TriClientPool) Put(c *tri.Client) {
+ now := time.Now()
+ if now.Sub(p.lastScaleTime) > constant.AutoScalerPeriod {
+ p.autoScaler()
+ }
+
+ if c == nil {
+ return
+ }
+
+ p.mu.Lock()
+ closed := p.closed
+ p.mu.Unlock()
+ if closed {
+ return
+ }
+
+ select {
+ case p.clients <- c:
+ default:
+ p.mu.Lock()
+ p.curSize--
+ p.mu.Unlock()
+ }
+}
+
+// close removes all clients from the channel and then closes it.
+func (p *TriClientPool) Close() {
+ p.mu.Lock()
+ if p.closed {
+ p.mu.Unlock()
+ return
+ }
+ p.closed = true
+
+ for len(p.clients) > 0 {
+ <-p.clients
+ p.curSize--
+ }
+
+ close(p.clients)
+ p.mu.Unlock()
+}
+
+func (p *TriClientPool) MaxSize() int {
+ return p.maxSize
+}
+
+func (p *TriClientPool) Closed() bool {
+ return p.closed
+}
+
+// autoScaler performs scaling check.
+// It is triggered by Get() or Put()
+// If the timeout count is high, autoScaler tends to expand.
+// If the idle client count is often high, autoScaler tends to shrink.
+func (p *TriClientPool) autoScaler() {
+ p.mu.Lock()
+
+ now := time.Now()
+ if now.Sub(p.lastScaleTime) < constant.AutoScalerPeriod {
+ p.mu.Unlock()
+ return
+ }
+
+ if p.closed {
+ p.mu.Unlock()
+ return
+ }
+
+ p.lastScaleTime = now
+
+ curSize := p.curSize
+ idle := len(p.clients)
+ timeouts := p.getTimeouts
+ p.getTimeouts = 0
+
+ needExpand := checkExpand(curSize, idle, timeouts)
+ if needExpand != 0 {
+ p.consecutiveHighIdle = 0
+ p.mu.Unlock()
+ p.expand(needExpand)
+ return
+ }
+
+ needShrink := checkShrink(curSize, idle, &p.consecutiveHighIdle)
+ p.mu.Unlock()
+ if needShrink != 0 {
+ p.shrink(needShrink)
+ }
+}
Review Comment:
ditto
##########
protocol/triple/client.go:
##########
@@ -213,111 +246,88 @@ func newClientManager(url *common.URL) (*clientManager, error) {
callProtocol = constant.CallHTTP2
}
+ transport, err := buildTransport(callProtocol, cfg, keepAliveInterval, keepAliveTimeout, tlsFlag)
+ if err != nil {
+ return nil, err
+ }
+ httpClient := &http.Client{
+ Transport: transport,
+ }
+ perClientTransport := url.GetParamBool("tri.per_client_transport", false)
Review Comment:
extract `tri.per_client_transport` as a const
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "marsevilspirit (via GitHub)" <gi...@apache.org>.
marsevilspirit commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3580776769
Fix ci error.
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): Implement TriClientPool and Delay triple client Method Binding [dubbo-go]
Posted by "Aetherance (via GitHub)" <gi...@apache.org>.
Aetherance commented on code in PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#discussion_r2632061453
##########
protocol/triple/client.go:
##########
@@ -213,111 +246,88 @@ func newClientManager(url *common.URL) (*clientManager, error) {
callProtocol = constant.CallHTTP2
}
+ transport, err := buildTransport(callProtocol, cfg, keepAliveInterval, keepAliveTimeout, tlsFlag)
+ if err != nil {
+ return nil, err
+ }
+ httpClient := &http.Client{
+ Transport: transport,
+ }
+ perClientTransport := url.GetParamBool("tri.per_client_transport", false)
Review Comment:
done
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): Implement TriClientPool and Delay triple client Method Binding [dubbo-go]
Posted by "Aetherance (via GitHub)" <gi...@apache.org>.
Aetherance commented on code in PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#discussion_r2632169552
##########
protocol/triple/client_pool.go:
##########
@@ -0,0 +1,312 @@
+/*
+ * 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 triple
+
+import (
+ "errors"
+ "sync"
+ "time"
+)
+
+import (
+ "dubbo.apache.org/dubbo-go/v3/common/constant"
+ tri "dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol"
+)
+
+type ClientPool interface {
+ Get(timeout time.Duration) (*tri.Client, error)
+ Put(client *tri.Client)
+ Close()
+
+ MaxSize() int
+ Closed() bool
+}
+
+var (
+ ErrTriClientPoolClosed = errors.New("tri client pool is closed")
+ ErrTriClientPoolCloseWhenEmpty = errors.New("empty tri client pool close")
+)
+
+type TriClientPool struct {
+ clients chan *tri.Client
+ factory func() *tri.Client
+ mu sync.Mutex
+ maxSize int
+ curSize int
+ closed bool
+ getTimeouts int // recent timeout count, used to trigger expansion
+ lastScaleTime time.Time
+
+ fallback *tri.Client
+ consecutiveHighIdle int
+}
+
+// NewTriClientPool creates a new TriClientPool with optional warm-up.
+// warmUpSize specifies how many clients to pre-create and add to the pool.
+// If warmUpSize is 0, no warm-up is performed.
+// If warmUpSize > maxSize, it will be capped to maxSize.
+func NewTriClientPool(warmUpSize, maxSize int, factory func() *tri.Client) *TriClientPool {
+ if warmUpSize > maxSize {
+ warmUpSize = maxSize
+ }
+ if warmUpSize < 0 {
+ warmUpSize = 0
+ }
+
+ pool := &TriClientPool{
+ clients: make(chan *tri.Client, maxSize),
+ factory: factory,
+ maxSize: maxSize,
+ curSize: warmUpSize,
+ }
+
+ for i := 0; i < warmUpSize; i++ {
+ cli := factory()
+ select {
+ case pool.clients <- cli:
+ default:
+ pool.curSize--
+ }
+ }
+
+ return pool
+}
+
+// TriClient Get method
+// timeout means how long to wait for an available client.
+// TriClientPool keeps a fallback client pointer. If Get() times out,
+// and the pool cannot expand at that moment, the pool will return fallback.
+// This ensures there is at least one usable client.
+// Get tries a non-blocking receive first. If that fails, it tries to expand.
+// If expansion is not allowed, it waits for a client up to timeout.
+// After timeout, if still no client, Get() returns ErrTimeout with fallback.
+func (p *TriClientPool) Get(timeout time.Duration) (*tri.Client, error) {
+ now := time.Now()
+ if now.Sub(p.lastScaleTime) > constant.AutoScalerPeriod {
+ p.autoScaler()
+ }
+
+ select {
+ case cli := <-p.clients:
+ return cli, nil
+ default:
+ }
+
+ p.mu.Lock()
+ if p.closed {
+ p.mu.Unlock()
+ return nil, ErrTriClientPoolClosed
+ }
+ // try to expand
+ if p.curSize < p.maxSize {
+ p.curSize++
+ p.mu.Unlock()
+ cli := p.factory()
+ return cli, nil
+ }
+ p.mu.Unlock()
+
+ select {
+ case cli, ok := <-p.clients:
+ if !ok {
+ return nil, ErrTriClientPoolClosed
+ }
+ return cli, nil
+ case <-time.After(timeout):
+ p.recordTimeout()
+ p.mu.Lock()
+ if p.fallback == nil {
+ p.fallback = p.factory()
+ }
+ p.mu.Unlock()
+ return p.fallback, nil
+ }
+}
+
+// TriClient Put method
+// Put tries to put a tri.Client back into the pool.
+// If it fails, Put will drop the client and notify the pool.
+// Dropping a client is part of shrinking.
+func (p *TriClientPool) Put(c *tri.Client) {
+ now := time.Now()
+ if now.Sub(p.lastScaleTime) > constant.AutoScalerPeriod {
+ p.autoScaler()
+ }
+
+ if c == nil {
+ return
+ }
+
+ p.mu.Lock()
+ closed := p.closed
+ p.mu.Unlock()
+ if closed {
+ return
+ }
+
+ select {
+ case p.clients <- c:
+ default:
+ p.mu.Lock()
+ p.curSize--
+ p.mu.Unlock()
+ }
+}
+
+// close removes all clients from the channel and then closes it.
+func (p *TriClientPool) Close() {
+ p.mu.Lock()
+ if p.closed {
+ p.mu.Unlock()
+ return
+ }
+ p.closed = true
+
+ for len(p.clients) > 0 {
+ <-p.clients
+ p.curSize--
+ }
+
+ close(p.clients)
+ p.mu.Unlock()
+}
Review Comment:
done
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): Implement TriClientPool and Delay triple client Method Binding [dubbo-go]
Posted by "Aetherance (via GitHub)" <gi...@apache.org>.
Aetherance commented on code in PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#discussion_r2632184322
##########
protocol/triple/client.go:
##########
@@ -57,30 +57,57 @@ const (
// callUnary, callClientStream, callServerStream, callBidiStream.
// A Reference has a clientManager.
type clientManager struct {
- isIDL bool
- // triple_protocol clients, key is method name
- triClients map[string]*tri.Client
+ isIDL bool
+ triGuard *sync.RWMutex
+ // triple_protocol clients
+ triClients *TriClientPool
}
// TODO: code a triple client between clientManager and triple_protocol client
// TODO: write a NewClient for triple client
func (cm *clientManager) getClient(method string) (*tri.Client, error) {
- triClient, ok := cm.triClients[method]
- if !ok {
+ cm.triGuard.RLock()
+ if cm.triClients == nil {
+ cm.triGuard.RUnlock()
+ return nil, fmt.Errorf("no client pool found for method: %s", method)
+ }
+ triClient, err := cm.triClients.Get(constant.DefaultClientGetTimeout)
+ cm.triGuard.RUnlock()
+ if triClient == nil {
+ return nil, fmt.Errorf("missing triple client for method: %s", method)
+ }
+ // Wait up to defaultClientGetTimeout for a pooled client; if none becomes available we return the
+ // fallback client and record a timeout which is used to drive autoscaling.
+ if err != nil {
return nil, fmt.Errorf("missing triple client for method: %s", method)
}
return triClient, nil
}
+func (cm *clientManager) putClient(method string, c *tri.Client) {
+ if c == nil {
+ return
+ }
+
+ if cm.triClients == nil {
Review Comment:
确实是我的问题,已修复
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): Implement TriClientPool and Delay triple client Method Binding [dubbo-go]
Posted by "Alanxtl (via GitHub)" <gi...@apache.org>.
Alanxtl commented on code in PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#discussion_r2633289731
##########
protocol/triple/client_pool.go:
##########
@@ -0,0 +1,312 @@
+/*
+ * 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 triple
+
+import (
+ "errors"
+ "sync"
+ "time"
+)
+
+import (
+ "dubbo.apache.org/dubbo-go/v3/common/constant"
+ tri "dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol"
+)
+
+type ClientPool interface {
+ Get(timeout time.Duration) (*tri.Client, error)
+ Put(client *tri.Client)
+ Close()
+
+ MaxSize() int
+ Closed() bool
+}
+
+var (
+ ErrTriClientPoolClosed = errors.New("tri client pool is closed")
+ ErrTriClientPoolCloseWhenEmpty = errors.New("empty tri client pool close")
+)
+
+type TriClientPool struct {
+ clients chan *tri.Client
+ factory func() *tri.Client
+ mu sync.Mutex
+ maxSize int
+ curSize int
+ closed bool
+ getTimeouts int // recent timeout count, used to trigger expansion
+ lastScaleTime time.Time
+
+ fallback *tri.Client
+ consecutiveHighIdle int
+}
+
+// NewTriClientPool creates a new TriClientPool with optional warm-up.
+// warmUpSize specifies how many clients to pre-create and add to the pool.
+// If warmUpSize is 0, no warm-up is performed.
+// If warmUpSize > maxSize, it will be capped to maxSize.
+func NewTriClientPool(warmUpSize, maxSize int, factory func() *tri.Client) *TriClientPool {
+ if warmUpSize > maxSize {
+ warmUpSize = maxSize
+ }
+ if warmUpSize < 0 {
+ warmUpSize = 0
+ }
+
+ pool := &TriClientPool{
+ clients: make(chan *tri.Client, maxSize),
+ factory: factory,
+ maxSize: maxSize,
+ curSize: warmUpSize,
+ }
+
+ for i := 0; i < warmUpSize; i++ {
+ cli := factory()
+ select {
+ case pool.clients <- cli:
+ default:
+ pool.curSize--
+ }
+ }
+
+ return pool
+}
+
+// TriClient Get method
+// timeout means how long to wait for an available client.
+// TriClientPool keeps a fallback client pointer. If Get() times out,
+// and the pool cannot expand at that moment, the pool will return fallback.
+// This ensures there is at least one usable client.
+// Get tries a non-blocking receive first. If that fails, it tries to expand.
+// If expansion is not allowed, it waits for a client up to timeout.
+// After timeout, if still no client, Get() returns ErrTimeout with fallback.
+func (p *TriClientPool) Get(timeout time.Duration) (*tri.Client, error) {
+ now := time.Now()
+ if now.Sub(p.lastScaleTime) > constant.AutoScalerPeriod {
+ p.autoScaler()
+ }
+
+ select {
+ case cli := <-p.clients:
+ return cli, nil
+ default:
+ }
+
+ p.mu.Lock()
+ if p.closed {
+ p.mu.Unlock()
+ return nil, ErrTriClientPoolClosed
+ }
+ // try to expand
+ if p.curSize < p.maxSize {
+ p.curSize++
+ p.mu.Unlock()
+ cli := p.factory()
+ return cli, nil
+ }
+ p.mu.Unlock()
+
+ select {
+ case cli, ok := <-p.clients:
+ if !ok {
+ return nil, ErrTriClientPoolClosed
+ }
+ return cli, nil
+ case <-time.After(timeout):
+ p.recordTimeout()
+ p.mu.Lock()
+ if p.fallback == nil {
+ p.fallback = p.factory()
+ }
+ p.mu.Unlock()
+ return p.fallback, nil
+ }
+}
+
+// TriClient Put method
+// Put tries to put a tri.Client back into the pool.
+// If it fails, Put will drop the client and notify the pool.
+// Dropping a client is part of shrinking.
+func (p *TriClientPool) Put(c *tri.Client) {
+ now := time.Now()
+ if now.Sub(p.lastScaleTime) > constant.AutoScalerPeriod {
+ p.autoScaler()
+ }
+
+ if c == nil {
+ return
+ }
+
+ p.mu.Lock()
+ closed := p.closed
+ p.mu.Unlock()
+ if closed {
+ return
+ }
+
+ select {
+ case p.clients <- c:
+ default:
+ p.mu.Lock()
+ p.curSize--
+ p.mu.Unlock()
+ }
Review Comment:
如果没158行没close掉,在165行不会尝试加两次锁吗
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): Implement TriClientPool and Delay triple client Method Binding [dubbo-go]
Posted by "Aetherance (via GitHub)" <gi...@apache.org>.
Aetherance commented on code in PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#discussion_r2616841970
##########
protocol/triple/client.go:
##########
@@ -213,111 +246,88 @@ func newClientManager(url *common.URL) (*clientManager, error) {
callProtocol = constant.CallHTTP2
}
+ transport, err := buildTransport(callProtocol, cfg, keepAliveInterval, keepAliveTimeout, tlsFlag)
+ if err != nil {
+ return nil, err
+ }
+ httpClient := &http.Client{
+ Transport: transport,
+ }
+ perClientTransport := url.GetParamBool("tri.per_client_transport", false)
+
+ var baseTriURL string
+ baseTriURL = strings.TrimPrefix(url.Location, httpPrefix)
+ baseTriURL = strings.TrimPrefix(baseTriURL, httpsPrefix)
+ if tlsFlag {
+ baseTriURL = httpsPrefix + baseTriURL
+ } else {
+ baseTriURL = httpPrefix + baseTriURL
+ }
+
+ triURL, err := joinPath(baseTriURL, url.Interface())
+ if err != nil {
+ return nil, fmt.Errorf("JoinPath failed for base %s, interface %s", baseTriURL, url.Interface())
+ }
+
+ pool := NewTriClientPool(constant.DefaultClientWarmingUp, constant.TriClientPoolMaxSize, func() *tri.Client {
+ if perClientTransport {
+ dedicatedTransport, buildErr := buildTransport(callProtocol, cfg, keepAliveInterval, keepAliveTimeout, tlsFlag)
+ if buildErr != nil {
+ return tri.NewClient(httpClient, triURL, cliOpts...)
+ }
+ return tri.NewClient(&http.Client{Transport: dedicatedTransport}, triURL, cliOpts...)
+ }
+ return tri.NewClient(httpClient, triURL, cliOpts...)
+ })
+
+ return &clientManager{
+ isIDL: isIDL,
+ triGuard: &sync.RWMutex{},
+ triClients: pool,
+ }, nil
+}
+
+func buildTransport(callProtocol string, cfg *tls.Config, keepAliveInterval, keepAliveTimeout time.Duration, tlsFlag bool) (http.RoundTripper, error) {
switch callProtocol {
- // This case might be for backward compatibility,
- // it's not useful for the Triple protocol, HTTP/1 lacks trailer functionality.
- // Triple protocol only supports HTTP/2 and HTTP/3.
case constant.CallHTTP:
- transport = &http.Transport{
+ return &http.Transport{
TLSClientConfig: cfg,
- }
- cliOpts = append(cliOpts, tri.WithTriple())
+ }, nil
case constant.CallHTTP2:
- // TODO: Enrich the http2 transport config for triple protocol.
if tlsFlag {
- transport = &http2.Transport{
+ return &http2.Transport{
TLSClientConfig: cfg,
ReadIdleTimeout: keepAliveInterval,
PingTimeout: keepAliveTimeout,
- }
- } else {
- transport = &http2.Transport{
- DialTLSContext: func(_ context.Context, network, addr string, _ *tls.Config) (net.Conn, error) {
- return net.Dial(network, addr)
- },
- AllowHTTP: true,
- ReadIdleTimeout: keepAliveInterval,
- PingTimeout: keepAliveTimeout,
- }
+ }, nil
}
+ return &http2.Transport{
+ DialTLSContext: func(_ context.Context, network, addr string, _ *tls.Config) (net.Conn, error) {
+ return net.Dial(network, addr)
+ },
+ AllowHTTP: true,
+ ReadIdleTimeout: keepAliveInterval,
+ PingTimeout: keepAliveTimeout,
+ }, nil
case constant.CallHTTP3:
if !tlsFlag {
return nil, fmt.Errorf("TRIPLE http3 client must have TLS config, but TLS config is nil")
}
-
- // TODO: Enrich the http3 transport config for triple protocol.
- transport = &http3.Transport{
+ return &http3.Transport{
TLSClientConfig: cfg,
QUICConfig: &quic.Config{
- // ref: https://quic-go.net/docs/quic/connection/#keeping-a-connection-alive
KeepAlivePeriod: keepAliveInterval,
- // ref: https://quic-go.net/docs/quic/connection/#idle-timeout
Review Comment:
fixed
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org
Re: [PR] feat(triple): implement TriClientPool [dubbo-go]
Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #3086:
URL: https://github.com/apache/dubbo-go/pull/3086#issuecomment-3589256240
## [](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086) **Quality Gate passed**
Issues
 [1 New issue](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_dubbo-go&pullRequest=3086&issueStatuses=ACCEPTED)
Measures
 [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_dubbo-go&pullRequest=3086&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)
 [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_coverage&view=list)
 [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_dubbo-go&pullRequest=3086&metric=new_duplicated_lines_density&view=list)
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_dubbo-go&pullRequest=3086)
--
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: notifications-unsubscribe@dubbo.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org