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


##########
protocol/triple/dual_transport.go:
##########
@@ -0,0 +1,350 @@
+/*
+ * 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 (
+       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 {

Review Comment:
   `altSvcCache` is tracked per host, but `dualTransport` keeps only a single 
state originState for all requests. If this `RoundTripper` is ever reused 
across multiple origins, one host’s H3 success/cooldown can affect another host 
incorrectly. It seems the negotiation state should be keyed by host as well, or 
we should explicitly guarantee that one `dualTransport` instance is bound to 
exactly one origin.



##########
protocol/triple/dual_transport.go:
##########
@@ -0,0 +1,350 @@
+/*
+ * 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 (
+       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 originState
+
+       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 != "h3" {

Review Comment:
   make "h3" into a const in common/consts



##########
protocol/triple/dual_transport.go:
##########
@@ -0,0 +1,350 @@
+/*
+ * 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 (
+       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 originState
+
+       mu sync.Mutex

Review Comment:
   it would also be good to add a test covering two different hosts sharing the 
same transport, to prove the negotiation/cooldown state is isolated correctly.



##########
protocol/triple/dual_transport.go:
##########
@@ -0,0 +1,350 @@
+/*
+ * 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 (
+       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 originState
+
+       mu sync.Mutex

Review Comment:
   pls write a comment to clearify what does this mu protect



##########
protocol/triple/dual_transport.go:
##########
@@ -0,0 +1,350 @@
+/*
+ * 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 (
+       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 originState
+
+       mu sync.Mutex

Review Comment:
   it would also be good to add a test covering two different hosts sharing the 
same transport, to prove the negotiation/cooldown state is isolated correctly.



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