This repository has been archived by the owner on Dec 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 752
Perform a graceful shutdown for the function proxy on SIGINT & SIGTERM. #1108
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
18415a5
Perform a graceful shutdown for the function proxy on SIGINT & SIGTERM.
413403e
Add comment to exported function NewServer.
d0x2f 8246415
gofmt
d0x2f 5d9f243
Read SHUTDOWN_TIMEOUT env var for the shutdown timeout value.
59da64f
Add comment to exported function.
7cea83a
replace accidently removed function call.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,9 @@ import ( | |
"net/http" | ||
"os" | ||
"os/exec" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/kubeless/kubeless/pkg/function-proxy/utils" | ||
|
||
|
@@ -80,10 +83,35 @@ func startNativeDaemon() { | |
} | ||
} | ||
|
||
func gracefulShutdown(server *http.Server) { | ||
stop := make(chan os.Signal, 1) | ||
signal.Notify(stop, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) | ||
<-stop | ||
timeout := 10 * time.Second | ||
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. it may be useful to configure this timeout, maybe we should add a env var to be able to change the default value? 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. I've added an env var SHUTDOWN_TIMEOUT to control this. |
||
ctx, cancel := context.WithTimeout(context.Background(), timeout) | ||
defer cancel() | ||
|
||
log.Printf("Shuting down with timeout: %s\n", timeout) | ||
if err := server.Shutdown(ctx); err != nil { | ||
log.Printf("Error: %v\n", err) | ||
} else { | ||
log.Println("Server stopped") | ||
} | ||
} | ||
|
||
func main() { | ||
go startNativeDaemon() | ||
http.HandleFunc("/", handler) | ||
http.HandleFunc("/healthz", health) | ||
http.Handle("/metrics", promhttp.Handler()) | ||
utils.ListenAndServe() | ||
mux := http.NewServeMux() | ||
mux.HandleFunc("/", handler) | ||
mux.HandleFunc("/healthz", health) | ||
mux.Handle("/metrics", promhttp.Handler()) | ||
|
||
server := utils.NewServer(mux) | ||
|
||
go func() { | ||
if err := server.ListenAndServe(); err != http.ErrServerClosed { | ||
panic(err) | ||
} | ||
}() | ||
|
||
gracefulShutdown(server) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
as far as I know, you cannot capture a
SIGTERM
signalThere 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.
Actually you can capture SIGTERM, and it's what kubernetes sends to a pod when it requests a graceful shutdown. 30 seconds later it'll send SIGKILL which can't be captured, maybe that's what you're thinking of?
It can be tested by running the proxy locally and running
kill -15 <pid>
.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.
See https://cloud.google.com/blog/products/gcp/kubernetes-best-practices-terminating-with-grace
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.
I see, indeed they suggest so. I tried it with a simple example and it didn't seem to work:
As you can see, in the second execution I am sending
kill -15 <pid>
and it's not catching the signal (but I may be missing something),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.
Curiously I tried your example.
When running with
go run foo.go
it seems to make two processes, when I sent sigterm to the binary in /tmp it worked as expected.i.e. the second entry in this
ps
outputCompiling with
go build foo.go
and doing the same with the produced binary should work as well.Anyway thanks for being so responsive!
I hope to get the node runtime doing the same soon.
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.
Oh, I see, good to know, thanks for following up!