AlexStocks commented on code in PR #3281:
URL: https://github.com/apache/dubbo-go/pull/3281#discussion_r3006219806


##########
protocol/triple/dual_transport.go:
##########
@@ -0,0 +1,353 @@
+/*
+ * 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"
+       "errors"
+       "io"
+       "net/http"
+       "net/url"
+       "sync"
+       "time"
+)
+
+import (
+       "github.com/dubbogo/gost/log/logger"
+
+       "github.com/quic-go/quic-go"
+       "github.com/quic-go/quic-go/http3"
+
+       "golang.org/x/net/http2"
+)
+
+import (
+       "dubbo.apache.org/dubbo-go/v3/common/constant"
+       tri "dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol"
+)
+
+type originMode int
+
+const (
+       // originUnknown means the origin has not advertised HTTP/3 yet.
+       originUnknown originMode = iota
+       // originCandidate means the origin advertised HTTP/3 and is waiting 
for validation.
+       originCandidate
+       // originProbing means an out-of-band HTTP/3 probe is in flight.
+       originProbing
+       // originH3Healthy means later requests may be sent over HTTP/3.
+       originH3Healthy
+       // originCooldown means HTTP/3 recently failed and should be avoided 
for a while.
+       originCooldown
+)
+
+const (
+       defaultH3ProbeTimeout = 3 * time.Second
+       defaultH3BaseCooldown = 4 * time.Second
+       defaultH3MaxCooldown  = 1 * time.Minute
+)
+
+// originState tracks whether the current upstream origin is ready for HTTP/3.
+type originState struct {
+       mode          originMode
+       failures      int
+       cooldownUntil time.Time
+}
+
+// dualTransport keeps HTTP/2 as the stable path and only uses HTTP/3 after 
discovery and probe.
+type dualTransport struct {
+       http2Transport http.RoundTripper
+       http3Transport http.RoundTripper
+       // Cache for alternative services to avoid repeated lookups
+       altSvcCache *tri.AltSvcCache
+
+       // state tracks HTTP/3 readiness for the bound upstream origin.
+       state originState
+
+       // mu protects state and serializes transitions between 
unknown/probing/healthy/cooldown.
+       mu sync.Mutex
+
+       probeTimeout time.Duration
+       baseCooldown time.Duration
+       maxCooldown  time.Duration
+}
+
+// newDualTransport creates a new dual transport that supports both HTTP/2 and 
HTTP/3
+func newDualTransport(tlsConfig *tls.Config, keepAliveInterval, 
keepAliveTimeout time.Duration) http.RoundTripper {
+       http2Transport := &http2.Transport{
+               TLSClientConfig: tlsConfig,
+               ReadIdleTimeout: keepAliveInterval,
+               PingTimeout:     keepAliveTimeout,
+       }
+
+       http3Transport := &http3.Transport{
+               TLSClientConfig: tlsConfig,
+               QUICConfig: &quic.Config{
+                       KeepAlivePeriod: keepAliveInterval,
+                       MaxIdleTimeout:  keepAliveTimeout,
+               },
+       }
+
+       return &dualTransport{
+               http2Transport: http2Transport,
+               http3Transport: http3Transport,
+               altSvcCache:    tri.NewAltSvcCache(),
+               probeTimeout:   defaultH3ProbeTimeout,
+               baseCooldown:   defaultH3BaseCooldown,
+               maxCooldown:    defaultH3MaxCooldown,
+       }
+}
+
+// RoundTrip implements http.RoundTripper interface with HTTP Alternative 
Services support
+func (dt *dualTransport) RoundTrip(req *http.Request) (*http.Response, error) {
+       host := req.URL.Host
+
+       if dt.shouldUseH3(host) {
+               // Only use HTTP/3 after a separate probe marks the origin 
healthy.
+               // If the HTTP/3 request fails, return the error directly 
instead of
+               // replaying the same request over HTTP/2 with a partially 
consumed body.
+               resp, err := dt.http3Transport.RoundTrip(req)
+               if err == nil {
+                       dt.markH3Success(host)
+                       return resp, nil
+               }
+               if dt.shouldMarkH3Failure(req, err) {
+                       logger.Warnf("HTTP/3 request failed for %s: %v", 
req.URL.String(), err)
+                       dt.markH3Failure(host)
+               }
+               return nil, err
+       }
+
+       resp, err := dt.http2Transport.RoundTrip(req)
+       if err != nil {
+               return nil, err
+       }
+
+       // Learn HTTP/3 availability from HTTP/2 responses and validate it with
+       // an independent probe before routing later requests over HTTP/3.
+       dt.observeH2Response(req.URL, resp.Header)
+       return resp, nil
+}
+
+// shouldUseH3 only decides whether the current request may use HTTP/3.
+func (dt *dualTransport) shouldUseH3(host string) bool {
+       if host == "" {
+               return false
+       }
+
+       altSvc := dt.altSvcCache.Get(host)
+       if altSvc == nil || altSvc.Protocol != constant.AltSvcProtocolH3 {
+               return false
+       }
+
+       now := time.Now()
+
+       dt.mu.Lock()
+       defer dt.mu.Unlock()
+
+       switch dt.state.mode {
+       case originH3Healthy:
+               return true
+
+       case originCooldown:
+               // Wait for the cooldown window to expire before probing HTTP/3 
again.
+               if now.Before(dt.state.cooldownUntil) {
+                       return false
+               }
+
+               logger.Debugf("HTTP/3 cooldown expired for %s", host)
+               dt.state.mode = originCandidate
+               dt.state.cooldownUntil = time.Time{}
+               return false

Review Comment:
   [P0] dualTransport 结构体缺少对多 origin 场景的支持。
   
   当前 state 字段是单个 originState,这意味着整个 dualTransport 只能跟踪一个 origin 的状态。但实际使用中,一个 
client 可能会请求多个不同的 host。
   
   **问题:**
   - 如果请求 hostA 和 hostB,它们会共享同一个 state
   - hostA 进入 cooldown 会影响 hostB 的 HTTP/3 使用
   
   **改进建议:**
   使用 map[string]*originState 为每个 origin 维护独立状态。



##########
protocol/triple/dual_transport.go:
##########
@@ -0,0 +1,353 @@
+/*
+ * 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"
+       "errors"
+       "io"
+       "net/http"
+       "net/url"
+       "sync"
+       "time"
+)
+
+import (
+       "github.com/dubbogo/gost/log/logger"
+
+       "github.com/quic-go/quic-go"
+       "github.com/quic-go/quic-go/http3"
+
+       "golang.org/x/net/http2"
+)
+
+import (
+       "dubbo.apache.org/dubbo-go/v3/common/constant"
+       tri "dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol"
+)
+
+type originMode int
+
+const (
+       // originUnknown means the origin has not advertised HTTP/3 yet.
+       originUnknown originMode = iota
+       // originCandidate means the origin advertised HTTP/3 and is waiting 
for validation.
+       originCandidate
+       // originProbing means an out-of-band HTTP/3 probe is in flight.
+       originProbing
+       // originH3Healthy means later requests may be sent over HTTP/3.
+       originH3Healthy
+       // originCooldown means HTTP/3 recently failed and should be avoided 
for a while.
+       originCooldown
+)
+
+const (
+       defaultH3ProbeTimeout = 3 * time.Second
+       defaultH3BaseCooldown = 4 * time.Second
+       defaultH3MaxCooldown  = 1 * time.Minute
+)
+
+// originState tracks whether the current upstream origin is ready for HTTP/3.
+type originState struct {
+       mode          originMode
+       failures      int
+       cooldownUntil time.Time
+}
+
+// dualTransport keeps HTTP/2 as the stable path and only uses HTTP/3 after 
discovery and probe.
+type dualTransport struct {
+       http2Transport http.RoundTripper
+       http3Transport http.RoundTripper
+       // Cache for alternative services to avoid repeated lookups
+       altSvcCache *tri.AltSvcCache
+
+       // state tracks HTTP/3 readiness for the bound upstream origin.
+       state originState
+
+       // mu protects state and serializes transitions between 
unknown/probing/healthy/cooldown.
+       mu sync.Mutex
+
+       probeTimeout time.Duration
+       baseCooldown time.Duration
+       maxCooldown  time.Duration
+}
+
+// newDualTransport creates a new dual transport that supports both HTTP/2 and 
HTTP/3
+func newDualTransport(tlsConfig *tls.Config, keepAliveInterval, 
keepAliveTimeout time.Duration) http.RoundTripper {
+       http2Transport := &http2.Transport{
+               TLSClientConfig: tlsConfig,
+               ReadIdleTimeout: keepAliveInterval,
+               PingTimeout:     keepAliveTimeout,
+       }
+
+       http3Transport := &http3.Transport{
+               TLSClientConfig: tlsConfig,
+               QUICConfig: &quic.Config{
+                       KeepAlivePeriod: keepAliveInterval,
+                       MaxIdleTimeout:  keepAliveTimeout,
+               },
+       }
+
+       return &dualTransport{
+               http2Transport: http2Transport,
+               http3Transport: http3Transport,
+               altSvcCache:    tri.NewAltSvcCache(),
+               probeTimeout:   defaultH3ProbeTimeout,
+               baseCooldown:   defaultH3BaseCooldown,
+               maxCooldown:    defaultH3MaxCooldown,
+       }
+}
+
+// RoundTrip implements http.RoundTripper interface with HTTP Alternative 
Services support
+func (dt *dualTransport) RoundTrip(req *http.Request) (*http.Response, error) {
+       host := req.URL.Host
+
+       if dt.shouldUseH3(host) {
+               // Only use HTTP/3 after a separate probe marks the origin 
healthy.
+               // If the HTTP/3 request fails, return the error directly 
instead of
+               // replaying the same request over HTTP/2 with a partially 
consumed body.
+               resp, err := dt.http3Transport.RoundTrip(req)
+               if err == nil {
+                       dt.markH3Success(host)

Review Comment:
   [P1] Import 顺序违反 Go 代码规范。
   
   根据 Google Go Style Guide,import 应该按以下顺序分组:
   1. 标准库
   2. 第三方库  
   3. 项目内部库
   
   当前 line 127-134 的第三方库分成了多个 import 块,应该合并为一个。
   
   **改进建议:**
   将所有第三方库合并到一个 import 块中。



-- 
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