Skip to content

Commit 7716c72

Browse files
committed
chore: add signal handler to remove temp file
1 parent 9d8f573 commit 7716c72

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

spannerlib/grpc-server/server.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"log"
66
"net"
77
"os"
8+
"os/signal"
9+
"syscall"
810

911
"cloud.google.com/go/spanner/apiv1/spannerpb"
1012
"google.golang.org/grpc"
@@ -23,9 +25,22 @@ func main() {
2325
if len(os.Args) > 2 {
2426
tp = os.Args[2]
2527
}
28+
//
2629
if tp == "unix" {
2730
defer func() { _ = os.Remove(name) }()
31+
// Set up a channel to listen for OS signals that terminate the process,
32+
// so we can clean up the temp file in those cases as well.
33+
sigs := make(chan os.Signal, 1)
34+
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
35+
go func() {
36+
// Wait for a signal.
37+
<-sigs
38+
// Delete the temp file.
39+
_ = os.Remove(name)
40+
os.Exit(0)
41+
}()
2842
}
43+
2944
lis, err := net.Listen(tp, name)
3045
if err != nil {
3146
log.Fatalf("failed to listen: %v\n", err)

0 commit comments

Comments
 (0)