zbchi commented on code in PR #3301:
URL: https://github.com/apache/dubbo-go/pull/3301#discussion_r3124122204
##########
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一直是tri.server的mux,用户传入的http handler只是被 attach到server上,然后在
transport层通过fallback接进这个mux
--
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]