-
Notifications
You must be signed in to change notification settings - Fork 1k
fix: optimization of Serve in server/server.go #3099
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+123
−73
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d382f3b
fix: optimization of Serve in server/server.go (#3042)
6745f41
fix: all shutdown-related logs have been modified to use a consistent…
0325d18
fix: conflict with graceful shutdown package (#3042)
e5b18db
refactor: Refactored the poorly written code. (#3042)
32af2ab
Merge branch 'develop' into fix/server
y138g 321c9be
style: Format code. (#3042)
28be5f5
fix(server): replace select{} with signal-based graceful shutdown in …
618d831
fix: adopt upstream changes to resolve develop branch conflicts.(#3042)
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,10 +20,12 @@ package server | |
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "os/signal" | ||
| "reflect" | ||
| "runtime/debug" | ||
| "sort" | ||
| "strconv" | ||
| "strings" | ||
| "sync" | ||
| ) | ||
|
|
||
|
|
@@ -37,6 +39,7 @@ import ( | |
| "dubbo.apache.org/dubbo-go/v3/common" | ||
| "dubbo.apache.org/dubbo-go/v3/common/constant" | ||
| "dubbo.apache.org/dubbo-go/v3/common/dubboutil" | ||
| "dubbo.apache.org/dubbo-go/v3/graceful_shutdown" | ||
| "dubbo.apache.org/dubbo-go/v3/metadata" | ||
| "dubbo.apache.org/dubbo-go/v3/registry/exposed_tmp" | ||
| ) | ||
|
|
@@ -104,10 +107,12 @@ func (s *Server) genSvcOpts(handler any, info *common.ServiceInfo, opts ...Servi | |
| return nil, errors.New("Server has not been initialized, please use NewServer() to create Server") | ||
| } | ||
| var svcOpts []ServiceOption | ||
|
|
||
| appCfg := s.cfg.Application | ||
| proCfg := s.cfg.Provider | ||
| prosCfg := s.cfg.Protocols | ||
| regsCfg := s.cfg.Registries | ||
|
|
||
| // todo(DMwangnima): record the registered service | ||
| // Record the registered service for debugging and monitoring | ||
| interfaceName := common.GetReference(handler) | ||
|
|
@@ -116,7 +121,7 @@ func (s *Server) genSvcOpts(handler any, info *common.ServiceInfo, opts ...Servi | |
| newSvcOpts := defaultServiceOptions() | ||
| if appCfg != nil { | ||
| svcOpts = append(svcOpts, | ||
| SetApplication(s.cfg.Application), | ||
| SetApplication(appCfg), | ||
| ) | ||
| } | ||
| if proCfg != nil { | ||
|
|
@@ -234,32 +239,33 @@ func createReflectionMethodFunc(method reflect.Method) func(ctx context.Context, | |
| } | ||
| } | ||
|
|
||
| // Add a method with a name of a different first-letter case | ||
| // to achieve interoperability with java | ||
| // TODO: The method name case sensitivity in Dubbo-java should be addressed. | ||
| // We ought to make changes to handle this issue. | ||
| // enhanceServiceInfo fills in missing MethodFunc entries via reflection. | ||
| // Case-insensitive Triple routing is handled in the transport-layer route mux, | ||
| // but lowercase-first ServiceInfo method names still need MethodFunc backfill so | ||
| // reflection-based invocation can reach the exported Go method. | ||
| func enhanceServiceInfo(info *common.ServiceInfo) *common.ServiceInfo { | ||
| if info == nil { | ||
| return info | ||
| } | ||
|
|
||
| // Get service type for reflection-based method calls | ||
| var svcType reflect.Type | ||
| if info.ServiceType != nil { | ||
| svcType = reflect.TypeOf(info.ServiceType) | ||
| } | ||
|
|
||
| // Build method map for reflection lookup | ||
| // Build method map for reflection lookup. | ||
| // Keep the first-rune-swapped alias for lowercase-first ServiceInfo names | ||
| // (for example "sayHello" -> "SayHello") without duplicating metadata. | ||
| methodMap := make(map[string]reflect.Method) | ||
| if svcType != nil { | ||
| for i := 0; i < svcType.NumMethod(); i++ { | ||
| m := svcType.Method(i) | ||
| methodMap[m.Name] = m | ||
| methodMap[strings.ToLower(m.Name)] = m | ||
| methodMap[dubboutil.SwapCaseFirstRune(m.Name)] = m | ||
| } | ||
| } | ||
|
|
||
| // Add MethodFunc to methods that don't have it | ||
| // Fill in MethodFunc for methods that don't already have one. | ||
| for i := range info.Methods { | ||
| if info.Methods[i].MethodFunc == nil && svcType != nil { | ||
| if reflectMethod, ok := methodMap[info.Methods[i].Name]; ok { | ||
|
|
@@ -268,26 +274,13 @@ func enhanceServiceInfo(info *common.ServiceInfo) *common.ServiceInfo { | |
| } | ||
| } | ||
|
|
||
| // Create additional methods with swapped-case names for Java interoperability | ||
| var additionalMethods []common.MethodInfo | ||
| for _, method := range info.Methods { | ||
| newMethod := method | ||
| newMethod.Name = dubboutil.SwapCaseFirstRune(method.Name) | ||
| if method.MethodFunc != nil { | ||
| newMethod.MethodFunc = method.MethodFunc | ||
| } else if svcType != nil { | ||
| if reflectMethod, ok := methodMap[dubboutil.SwapCaseFirstRune(method.Name)]; ok { | ||
| newMethod.MethodFunc = createReflectionMethodFunc(reflectMethod) | ||
| } | ||
| } | ||
| additionalMethods = append(additionalMethods, newMethod) | ||
| } | ||
| info.Methods = append(info.Methods, additionalMethods...) | ||
|
|
||
| return info | ||
| } | ||
|
|
||
| func (s *Server) exportServices() error { | ||
| // add read lock to protect svcOptsMap data | ||
| s.mu.RLock() | ||
| defer s.mu.RUnlock() | ||
| for _, svcOpts := range s.svcOptsMap { | ||
| if err := svcOpts.Export(); err != nil { | ||
| logger.Errorf("export %s service failed, err: %s", svcOpts.Service.Interface, err) | ||
|
|
@@ -299,12 +292,17 @@ func (s *Server) exportServices() error { | |
|
|
||
| func (s *Server) Serve() error { | ||
| s.mu.Lock() | ||
| defer s.mu.Unlock() | ||
| if s.serve { | ||
| // release lock in case causing deadlock | ||
| s.mu.Unlock() | ||
| return errors.New("server has already been started") | ||
| } | ||
| // prevent multiple calls to Serve | ||
| s.serve = true | ||
|
|
||
| // release lock in case causing deadlock | ||
| s.mu.Unlock() | ||
|
|
||
| // the registryConfig in ServiceOptions and ServerOptions all need to init a metadataReporter, | ||
| // when ServiceOptions.init() is called we don't know if a new registry config is set in the future use serviceOption | ||
| if err := metadata.InitRegistryMetadataReport(s.cfg.Registries); err != nil { | ||
|
|
@@ -329,12 +327,40 @@ func (s *Server) Serve() error { | |
| if err := exposed_tmp.RegisterServiceInstance(); err != nil { | ||
| return err | ||
| } | ||
| select {} | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
建议: |
||
| // Listen for shutdown signals to enable graceful shutdown. | ||
| // Use the same signal set as the graceful_shutdown package for consistency. | ||
| shutdown := s.cfg.Shutdown | ||
| sigChan := make(chan os.Signal, 1) | ||
| signal.Notify(sigChan, graceful_shutdown.ShutdownSignals...) | ||
| defer signal.Stop(sigChan) | ||
|
|
||
| // Block until a shutdown signal is received. | ||
| sig := <-sigChan | ||
| logger.Infof("Received signal: %v, application is shutting down gracefully", sig) | ||
|
|
||
| // Perform graceful shutdown cleanup. | ||
| // BeforeShutdown is protected by sync.Once, so even if graceful_shutdown.Init() | ||
| // (InternalSignal=true) also calls it concurrently, only one execution will run. | ||
| if shutdown != nil { | ||
| graceful_shutdown.BeforeShutdown(shutdown) | ||
| } | ||
|
|
||
| // Handle signals that require heap dump (e.g., SIGQUIT, SIGILL, SIGTRAP, SIGABRT, SIGSYS) | ||
| for _, dumpSignal := range graceful_shutdown.DumpHeapShutdownSignals { | ||
| if sig == dumpSignal { | ||
| debug.WriteHeapDump(os.Stdout.Fd()) | ||
| } | ||
| } | ||
|
|
||
| os.Exit(0) | ||
| return nil // unreachable, but satisfies the compiler | ||
| } | ||
|
|
||
| // In order to expose internal services | ||
| func (s *Server) exportInternalServices() error { | ||
| cfg := &ServiceOptions{} | ||
|
|
||
| cfg.Application = s.cfg.Application | ||
| cfg.Provider = s.cfg.Provider | ||
| cfg.Protocols = s.cfg.Protocols | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
原来
beforeShutdown是私有函数,只在Init的 goroutine 里调用一次。导出为BeforeShutdown后,server.go可以直接调用,而Init内部的 goroutine(InternalSignal=true时)也会调用。两次destroyRegistries()+destroyProtocols()会对已销毁的资源重复操作(double free 语义),行为未定义。虽然server.go靠InternalSignal判断来规避,但这依赖调用者自律。建议:加
sync.Once保证无论被谁调用多少次,只执行一次。