-
Notifications
You must be signed in to change notification settings - Fork 57
How to exit middleware gracefully? #1265
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
Comments
/probe can you confirm? |
Yes, I can confirm the issue with middleware zombie processes in GoReplay. After analyzing the code, I've identified the root cause and can provide a solution. The IssueWhen GoReplay runs with a middleware (
However, this doesn't always properly terminate the middleware process, resulting in a zombie process. Root CauseThe current implementation in
SolutionTo fix this issue, middleware implementations in Go should:
Here's an example of a properly implemented middleware in Go: package main
import (
"bufio"
"encoding/hex"
"fmt"
"os"
"os/signal"
"syscall"
)
func main() {
// Set up signal handling
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
// Create a done channel to coordinate shutdown
done := make(chan struct{})
// Start processing in a goroutine
go func() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
// Process input and write output
line := scanner.Bytes()
// Your middleware logic here
fmt.Fprintf(os.Stdout, "%s\n", line)
}
close(done)
}()
// Wait for either a signal or processing to complete
select {
case <-sigChan:
fmt.Fprintln(os.Stderr, "Signal received, shutting down...")
case <-done:
fmt.Fprintln(os.Stderr, "Processing complete")
}
// Perform any cleanup needed
// No need to explicitly exit - the program will exit naturally
} This implementation ensures that:
With this approach, the middleware process will terminate gracefully when GoReplay exits, preventing zombie processes. Tip: Start comment with |
Start goreplay and middleware with
goreplay --middleware /app/middleware
, which handles signals like syscall.SIGINT, syscall.SIGTERM, etc. After the goreplay process is terminated, middleware also exits, but a middleware zombie process is formed. How to implement middleware correctly with golang?The text was updated successfully, but these errors were encountered: