Alanxtl commented on code in PR #3301:
URL: https://github.com/apache/dubbo-go/pull/3301#discussion_r3114870321
##########
protocol/triple/triple.go:
##########
@@ -95,40 +96,42 @@ func (tp *TripleProtocol) Export(invoker base.Invoker)
base.Exporter {
exporter := NewTripleExporter(serviceKey, invoker, tp.ExporterMap())
tp.SetExporterMap(serviceKey, exporter)
logger.Infof("[TRIPLE Protocol] Export service: %s", url.String())
- tp.openServer(invoker, info)
- internal.HealthSetServingStatusServing(serviceKey)
+ if err := tp.openServer(invoker, info); err != nil {
+ exporter.UnExport()
+ panic(err)
Review Comment:
panic之前log一下
##########
server/http_mount.go:
##########
@@ -0,0 +1,114 @@
+/*
+ * 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 server
+
+import (
+ "fmt"
+ "net/http"
+)
+
+import (
+ "dubbo.apache.org/dubbo-go/v3/common"
+ "dubbo.apache.org/dubbo-go/v3/common/constant"
+ "dubbo.apache.org/dubbo-go/v3/common/extension"
+ "dubbo.apache.org/dubbo-go/v3/global"
+ "dubbo.apache.org/dubbo-go/v3/protocol/base"
+)
+
+// MountHTTPHandler attaches an existing HTTP root handler to the server before
+// Serve starts. The mounted handler acts as the transport-level fallback after
+// Triple route lookup, so callers should aggregate any HTTP sub-services
behind
+// their own mux/router before mounting.
+func (s *Server) MountHTTPHandler(handler http.Handler) error {
+ if handler == nil {
+ return fmt.Errorf("mounted HTTP handler must not be nil")
+ }
+
+ s.mu.Lock()
+ defer s.mu.Unlock()
+
+ if s.serve {
+ return fmt.Errorf("mounted HTTP handler must be configured
before Serve")
+ }
+ // Server-level HTTP integration is a single root handler. Callers that
need
+ // multiple HTTP services should compose them behind their own
mux/router.
+ if s.mountedHTTPHandler != nil {
+ return fmt.Errorf("an HTTP handler has already been mounted")
+ }
+
+ s.mountedHTTPHandler = handler
+ return nil
+}
+
+// mountHTTPHandlers boots HTTP-capable protocols that support hosting an
+// existing root handler on the same listener as framework-managed routes.
+// Today that means Triple running on an explicit, user-selected port.
+func (s *Server) mountHTTPHandlers() error {
Review Comment:
这两个函数的名字也太confusing了
##########
protocol/triple/server.go:
##########
@@ -68,64 +79,168 @@ func NewServer(cfg *global.TripleConfig) *Server {
}
}
-// Start TRIPLE server
-func (s *Server) Start(invoker base.Invoker, info *common.ServiceInfo) {
+// Start prepares the Triple transport for one exported service. It resolves
the
+// listener configuration, ensures the underlying tri.Server exists, registers
+// the service handlers, and finally starts the shared transport.
+func (s *Server) Start(invoker base.Invoker, info *common.ServiceInfo) error {
url := invoker.GetURL()
- addr := url.Location
- var tripleConf *global.TripleConfig
+ settings, err := resolveServerTransport(url)
+ if err != nil {
+ return fmt.Errorf("TRIPLE server init failed: %w", err)
+ }
- tripleConfRaw, ok := url.GetAttribute(constant.TripleConfigKey)
- if ok {
- tripleConf = tripleConfRaw.(*global.TripleConfig)
+ if err := s.ensureTriServer(settings); err != nil {
+ return err
+ }
+ if err := s.refreshService(invoker, info); err != nil {
+ return err
}
+ s.startTransport(settings.callProtocol, settings.tlsConfig)
+ return nil
+}
- var callProtocol string
- if tripleConf != nil && tripleConf.Http3 != nil &&
tripleConf.Http3.Enable {
- callProtocol = constant.CallHTTP2AndHTTP3
- } else {
- // HTTP default type is HTTP/2.
- callProtocol = constant.CallHTTP2
+// resolveServerTransport extracts transport-level Triple configuration from
the
+// export URL and converts it into the concrete listener settings used by the
+// adaptation layer.
+func resolveServerTransport(url *common.URL) (*transportSettings, error) {
+ if url == nil {
+ return nil, fmt.Errorf("triple server url must not be nil")
}
- // initialize tri.Server
- s.triServer = tri.NewServer(addr, tripleConf)
+ tripleConf, err := resolveTripleConfig(url)
+ if err != nil {
+ return nil, err
+ }
- serialization := url.GetParam(constant.SerializationKey,
constant.ProtobufSerialization)
- switch serialization {
- case constant.ProtobufSerialization:
- case constant.JSONSerialization:
- case constant.Hessian2Serialization:
- case constant.MsgpackSerialization:
- default:
- panic(fmt.Sprintf("Unsupported serialization: %s",
serialization))
+ callProtocol := constant.CallHTTP2
+ if tripleConf != nil && tripleConf.Http3 != nil &&
tripleConf.Http3.Enable {
+ callProtocol = constant.CallHTTP2AndHTTP3
}
+
// todo: support opentracing interceptor
// TODO: move tls config to handleService
- var globalTlsConf *global.TLSConfig
- var tlsConf *tls.Config
- var err error
-
- // handle tls
- tlsConfRaw, ok := url.GetAttribute(constant.TLSConfigKey)
- if ok {
- globalTlsConf, ok = tlsConfRaw.(*global.TLSConfig)
- if !ok {
- logger.Errorf("TRIPLE Server initialized the TLSConfig
configuration failed")
- return
- }
+ rawTLSConfig, err := resolveRawTLSConfig(url)
+ if err != nil {
+ return nil, err
}
- if dubbotls.IsServerTLSValid(globalTlsConf) {
- tlsConf, err = dubbotls.GetServerTlSConfig(globalTlsConf)
+
+ var tlsConf *tls.Config
+ if dubbotls.IsServerTLSValid(rawTLSConfig) {
+ tlsConf, err = dubbotls.GetServerTlSConfig(rawTLSConfig)
if err != nil {
- logger.Errorf("TRIPLE Server initialized the TLSConfig
configuration failed. err: %v", err)
- return
+ return nil, fmt.Errorf("TRIPLE server initialized the
TLSConfig configuration failed: %w", err)
}
logger.Infof("TRIPLE Server initialized the TLSConfig
configuration")
}
+ return &transportSettings{
+ location: url.Location,
+ callProtocol: callProtocol,
+ tripleConfig: tripleConf,
+ tlsConfig: tlsConf,
+ rawTLSConfig: rawTLSConfig,
+ }, nil
+}
+
+// ensureTriServer lazily creates the shared tri.Server for one listener
+// address. Service export and mount-first startup both converge here so they
+// share the same transport lifecycle and reflection registration.
+func (s *Server) ensureTriServer(settings *transportSettings) error {
+ if err := s.validateTransportSettings(settings); err != nil {
+ return err
+ }
+
+ if s.triServer != nil {
+ return nil
+ }
+
+ s.mu.Lock()
+ s.transportSettings = settings
+ s.cfg = settings.tripleConfig
+ s.mu.Unlock()
+
+ s.triServer = tri.NewServer(settings.location, settings.tripleConfig)
+ // MountHTTPHandler may initialize transport before any Triple service
is
+ // exported, so the first tri.Server instance must inherit the root
handler.
+ s.mu.RLock()
+ handler := s.mountedHTTPHandler
+ s.mu.RUnlock()
+ if handler != nil {
+ s.triServer.SetFallbackHTTPHandler(handler)
+ }
+ internal.ReflectionRegister(s)
+ return nil
Review Comment:
这里是不是主handler是s.mountedHTTPHandler
然后fallback handler也是s.mountedHTTPHandler呀
--
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]