-
Notifications
You must be signed in to change notification settings - Fork 62
feat(gateway): add graceful shutdown with signal handling #41
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?
Changes from 1 commit
b4d0043
d94d91f
4b63721
1be2d92
a432b1b
c910d79
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -17,6 +17,8 @@ import ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "strconv" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "strings" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "time" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "os/signal" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "syscall" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+23
to
+24
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. Import formatting is incorrect - these imports have leading spaces and should align with the other imports above.
Suggested change
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! Prompt To Fix With AIThis is a comment left during a code review.
Path: gateway/main.go
Line: 23:24
Comment:
Import formatting is incorrect - these imports have leading spaces and should align with the other imports above.
```suggestion
"os/signal"
"syscall"
```
<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>
How can I resolve this? If you propose a fix, please make it concise. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/gin-contrib/cors" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/gin-gonic/gin" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -167,8 +169,36 @@ func main() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| port = "3000" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| log.Printf("Go Gateway running on port %s", port) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| r.Run(":" + port) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| addr := ":" + port | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| srv := &http.Server{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Addr: addr, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Handler: r, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| go func() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| log.Printf("[INFO] Gateway listening on %s", addr) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| log.Fatalf("[FATAL] listen error: %v", err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+205
to
+210
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. Critical: Using 🔎 Proposed fix using error channel+ errChan := make(chan error, 1)
+
go func() {
log.Printf("[INFO] Gateway listening on %s", addr)
- if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
- log.Fatalf("[FATAL] listen error: %v", err)
- }
+ errChan <- srv.ListenAndServe()
}()
// ---- Graceful shutdown ----
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
- <-quit
- log.Println("[INFO] Shutdown signal received, draining connections...")
+ select {
+ case err := <-errChan:
+ if err != nil && err != http.ErrServerClosed {
+ log.Fatalf("[FATAL] Server failed to start: %v", err)
+ }
+ return
+ case <-quit:
+ log.Println("[INFO] Shutdown signal received, draining connections...")
+ }
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Printf("[ERROR] Server forced to shutdown: %v", err)
} else {
log.Println("[OK] Server shutdown completed")
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // ---- Graceful shutdown ---- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| quit := make(chan os.Signal, 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <-quit | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| log.Println("[INFO] Shutdown signal received, draining connections...") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| defer cancel() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err := srv.Shutdown(ctx); err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| log.Printf("[ERROR] Server forced to shutdown: %v", err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| log.Println("[OK] Server shutdown completed") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+212
to
+234
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. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Check timeout configurations in gateway directory
rg -n "time\.(Second|Minute|Duration)" gateway/ -A 2 -B 2Repository: AnkanMisra/MicroAI-Paygate Length of output: 11091 🏁 Script executed: #!/bin/bash
# Also check around line 164 specifically for AI timeout
sed -n '150,180p' gateway/main.goRepository: AnkanMisra/MicroAI-Paygate Length of output: 958 🏁 Script executed: #!/bin/bash
# Look for all timeout-related constants or configurations
rg -n "(timeout|TIMEOUT|Timeout)" gateway/ -iRepository: AnkanMisra/MicroAI-Paygate Length of output: 7862 Consider extending shutdown timeout to avoid race with AI request deadlines. The graceful shutdown correctly uses The timeouts are configurable ( 🤖 Prompt for AI Agents
coderabbitai[bot] marked this conversation as resolved.
Outdated
Comment on lines
+213
to
+234
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. Race condition: wait for in-flight requests after stopping new connections. The current sequence waits for in-flight requests (lines 219-223) before calling
The standard graceful shutdown pattern is to call 🔧 Recommended fixEither remove the manual wait and rely on <-quit
log.Println("[INFO] Shutdown signal received, draining connections...")
- active := GetActiveRequestCount()
- if active > 0 {
- log.Printf("[INFO] Waiting for %d in-flight request(s)...", active)
- WaitForInFlightRequests()
- log.Println("[INFO] All in-flight requests completed")
- }
-
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Printf("[ERROR] Server forced to shutdown: %v", err)
} else {
log.Println("[OK] Server shutdown completed")
}Or, if logging the active count is important, just log it without the manual wait: <-quit
log.Println("[INFO] Shutdown signal received, draining connections...")
active := GetActiveRequestCount()
if active > 0 {
log.Printf("[INFO] %d in-flight request(s) detected, waiting for completion...", active)
- WaitForInFlightRequests()
- log.Println("[INFO] All in-flight requests completed")
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Printf("[ERROR] Server forced to shutdown: %v", err)
} else {
log.Println("[OK] Server shutdown completed")
}
📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // handleSummarize handles POST /api/ai/summarize requests. It validates | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
Fix indentation to use tabs instead of spaces.
The import statements appear to use spaces for indentation instead of tabs. Run
gofmtorgo fmtto ensure consistent formatting with Go standards.🔎 Fix formatting
📝 Committable suggestion
🤖 Prompt for AI Agents