-
Notifications
You must be signed in to change notification settings - Fork 5.6k
支持New优雅关闭,避免重启时流式回复被打断 #4258
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
base: main
Are you sure you want to change the base?
支持New优雅关闭,避免重启时流式回复被打断 #4258
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,13 +2,17 @@ package main | |
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "embed" | ||
| "errors" | ||
| "fmt" | ||
| "log" | ||
| "net/http" | ||
| "os" | ||
| "os/signal" | ||
| "strconv" | ||
| "strings" | ||
| "syscall" | ||
| "time" | ||
|
|
||
| "github.com/QuantumNous/new-api/common" | ||
|
|
@@ -192,10 +196,30 @@ func main() { | |
| // Log startup success message | ||
| common.LogStartupSuccess(startTime, port) | ||
|
|
||
| err = server.Run(":" + port) | ||
| if err != nil { | ||
| common.FatalLog("failed to start HTTP server: " + err.Error()) | ||
| srv := &http.Server{ | ||
| Addr: ":" + port, | ||
| Handler: server, | ||
| } | ||
|
|
||
| go func() { | ||
| if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { | ||
| common.FatalLog("failed to start HTTP server: " + err.Error()) | ||
| } | ||
| }() | ||
|
|
||
| quit := make(chan os.Signal, 1) | ||
| signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) | ||
| sig := <-quit | ||
| common.SysLog(fmt.Sprintf("received signal: %v, shutting down...", sig)) | ||
|
|
||
| // SSE streams may run for minutes; give them time to finish before forced exit | ||
| shutdownTimeout := time.Duration(common.GetEnvOrDefault("SHUTDOWN_TIMEOUT_SECONDS", 120)) * time.Second | ||
| ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout) | ||
| defer cancel() | ||
| if err := srv.Shutdown(ctx); err != nil { | ||
| common.SysError(fmt.Sprintf("server forced to shutdown: %v", err)) | ||
| } | ||
| common.SysLog("server exited") | ||
|
Comment on lines
+215
to
+222
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. Wait for relay/background workers before returning from
🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| func InjectUmamiAnalytics() { | ||
|
|
||
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.
Bind the listener before logging startup success.
Line 197 logs a successful startup before Line 205 has actually bound the port. If the bind fails, operators still get a false-positive “started” log and the real failure only appears from a background goroutine.
Suggested shape
Also add the
netimport.Verify each finding against the current code and only fix it if needed.
In
@main.goaround lines 196 - 207, Move the LogStartupSuccess call to after yousuccessfully bind the socket: perform a net.Listen("tcp", ":"+port) first (add
the net import), if listen returns without error then call
common.LogStartupSuccess(startTime, port), then start the server with
srv.Serve(listener) in the goroutine (or wrap listener in the existing srv).
Update the goroutine to use srv.Serve(listener) and still treat
http.ErrServerClosed as non-fatal; ensure the listener is closed on shutdown so
resources are cleaned up.