fix: cleanupCtx 在启动时创建,可能在 shutdown 前过期
This commit is contained in:
+6
-16
@@ -59,19 +59,6 @@ func main() {
|
||||
tracer *tracing.Tracer,
|
||||
resourceCleaner interfaces.ResourceCleaner,
|
||||
) error {
|
||||
// Create context for resource cleanup
|
||||
shutdownTimeout := cfg.Server.ShutdownTimeout
|
||||
if shutdownTimeout == 0 {
|
||||
shutdownTimeout = 30 * time.Second
|
||||
}
|
||||
cleanupCtx, cleanupCancel := context.WithTimeout(context.Background(), shutdownTimeout)
|
||||
defer cleanupCancel()
|
||||
|
||||
// Register tracer cleanup function to resource cleaner
|
||||
resourceCleaner.RegisterWithName("Tracer", func() error {
|
||||
return tracer.Cleanup(cleanupCtx)
|
||||
})
|
||||
|
||||
// Create HTTP server
|
||||
server := &http.Server{
|
||||
Addr: fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port),
|
||||
@@ -86,7 +73,11 @@ func main() {
|
||||
logger.Infof(context.Background(), "Received signal: %v, starting server shutdown...", sig)
|
||||
|
||||
// Create a context with timeout for server shutdown
|
||||
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
shutdownTimeout := cfg.Server.ShutdownTimeout
|
||||
if shutdownTimeout == 0 {
|
||||
shutdownTimeout = 30 * time.Second
|
||||
}
|
||||
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), shutdownTimeout)
|
||||
defer shutdownCancel()
|
||||
|
||||
if err := server.Shutdown(shutdownCtx); err != nil {
|
||||
@@ -95,11 +86,10 @@ func main() {
|
||||
|
||||
// Clean up all registered resources
|
||||
logger.Info(context.Background(), "Cleaning up resources...")
|
||||
errs := resourceCleaner.Cleanup(cleanupCtx)
|
||||
errs := resourceCleaner.Cleanup(shutdownCtx)
|
||||
if len(errs) > 0 {
|
||||
logger.Errorf(context.Background(), "Errors occurred during resource cleanup: %v", errs)
|
||||
}
|
||||
|
||||
logger.Info(context.Background(), "Server has exited")
|
||||
done()
|
||||
}()
|
||||
|
||||
Reference in New Issue
Block a user