fix: cleanupCtx 在启动时创建,可能在 shutdown 前过期
This commit is contained in:
@@ -17,13 +17,13 @@ tmp_dir = "tmp"
|
||||
include_dir = []
|
||||
include_ext = ["go", "tpl", "tmpl", "html", "yaml"]
|
||||
include_file = []
|
||||
kill_delay = "0s"
|
||||
kill_delay = "30s"
|
||||
log = "build-errors.log"
|
||||
poll = false
|
||||
poll_interval = 0
|
||||
rerun = false
|
||||
rerun_delay = 500
|
||||
send_interrupt = false
|
||||
send_interrupt = true
|
||||
stop_on_error = false
|
||||
|
||||
[color]
|
||||
|
||||
+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()
|
||||
}()
|
||||
|
||||
@@ -88,6 +88,9 @@ func BuildContainer(container *dig.Container) *dig.Container {
|
||||
must(container.Provide(initAntsPool))
|
||||
must(container.Provide(initContextStorage))
|
||||
|
||||
// Register tracer cleanup handler (tracer needs to be available for cleanup registration)
|
||||
must(container.Invoke(registerTracerCleanup))
|
||||
|
||||
// Register goroutine pool cleanup handler
|
||||
must(container.Invoke(registerPoolCleanup))
|
||||
|
||||
@@ -740,6 +743,19 @@ func registerPoolCleanup(pool *ants.Pool, cleaner interfaces.ResourceCleaner) {
|
||||
})
|
||||
}
|
||||
|
||||
// registerTracerCleanup registers the tracer for cleanup
|
||||
// Ensures proper cleanup of the tracer when application shuts down
|
||||
// Parameters:
|
||||
// - tracer: Tracer instance
|
||||
// - cleaner: Resource cleaner
|
||||
func registerTracerCleanup(tracer *tracing.Tracer, cleaner interfaces.ResourceCleaner) {
|
||||
// Register the cleanup function - actual context will be provided during cleanup
|
||||
cleaner.RegisterWithName("Tracer", func() error {
|
||||
// Create context for cleanup with longer timeout for tracer shutdown
|
||||
return tracer.Cleanup(context.Background())
|
||||
})
|
||||
}
|
||||
|
||||
// initDocReaderClient initializes the DocumentReader client (lightweight API).
|
||||
func initDocReaderClient(cfg *config.Config) (interfaces.DocumentReader, error) {
|
||||
addr := strings.TrimSpace(os.Getenv("DOCREADER_ADDR"))
|
||||
|
||||
Reference in New Issue
Block a user