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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to