Skip to content

SeaweedFS is a distributed storage system for object storage (S3), file systems, Iceberg table buckets, and Lance vector buckets, designed to handle billions of files with O(1) disk access and effortless horizontal scaling.

better handling of os signals

Chris Ludac95464b86b
diff --git a/go/weed/mount_std.go b/go/weed/mount_std.go
index 35ca4011d4..ab3467d26d 100644
--- a/go/weed/mount_std.go
+++ b/go/weed/mount_std.go
@@ -11,7 +11,6 @@ import (
"code.google.com/p/weed-fs/go/util"
"fmt"
"os"
- "os/signal"
"runtime"
)
@@ -28,16 +27,10 @@ func runMount(cmd *Command, args []string) bool {
return false
}
- signalChan := make(chan os.Signal, 1)
- signal.Notify(signalChan, os.Interrupt, os.Kill)
- go func() {
- for _ = range signalChan {
- // sig is a ^C, handle it
- fuse.Unmount(*mountOptions.dir)
- c.Close()
- os.Exit(0)
- }
- }()
+ OnInterrupt(func() {
+ fuse.Unmount(*mountOptions.dir)
+ c.Close()
+ })
err = fs.Serve(c, WFS{})
if err != nil {
diff --git a/go/weed/server.go b/go/weed/server.go
index 7684dcbde9..8d85042854 100644
--- a/go/weed/server.go
+++ b/go/weed/server.go
@@ -7,7 +7,6 @@ import (
"github.com/gorilla/mux"
"net/http"
"os"
- "os/signal"
"runtime"
"runtime/pprof"
"strconv"
@@ -218,16 +217,10 @@ func runServer(cmd *Command, args []string) bool {
glog.Fatalf(e.Error())
}
- // deal with control+c
- signalChan := make(chan os.Signal, 1)
- signal.Notify(signalChan, os.Interrupt, os.Kill)
- go func() {
- for _ = range signalChan {
- volumeServer.Shutdown()
- pprof.StopCPUProfile()
- os.Exit(0)
- }
- }()
+ OnInterrupt(func() {
+ volumeServer.Shutdown()
+ pprof.StopCPUProfile()
+ })
if e := http.Serve(volumeListener, r); e != nil {
glog.Fatalf("Fail to serve:%s", e.Error())
diff --git a/go/weed/signal_handling.go b/go/weed/signal_handling.go
new file mode 100644
index 0000000000..5d57c8fdbb
--- /dev/null
+++ b/go/weed/signal_handling.go
@@ -0,0 +1,27 @@
+// +build !plan9
+
+package main
+
+import (
+ "os"
+ "os/signal"
+ "syscall"
+)
+
+func OnInterrupt(fn func()) {
+ // deal with control+c,etc
+ signalChan := make(chan os.Signal, 1)
+ signal.Notify(signalChan,
+ os.Interrupt,
+ os.Kill,
+ syscall.SIGHUP,
+ syscall.SIGINT,
+ syscall.SIGTERM,
+ syscall.SIGQUIT)
+ go func() {
+ for _ = range signalChan {
+ fn()
+ os.Exit(0)
+ }
+ }()
+}
diff --git a/go/weed/signal_handling_notsupported.go b/go/weed/signal_handling_notsupported.go
new file mode 100644
index 0000000000..ad4f37b0c9
--- /dev/null
+++ b/go/weed/signal_handling_notsupported.go
@@ -0,0 +1,8 @@
+// +build plan9
+
+package main
+
+import ()
+
+func OnInterrupt(fn func()) {
+}
diff --git a/go/weed/volume.go b/go/weed/volume.go
index 5afa8eea5d..c4177a98a4 100644
--- a/go/weed/volume.go
+++ b/go/weed/volume.go
@@ -6,7 +6,6 @@ import (
"code.google.com/p/weed-fs/go/weed/weed_server"
"net/http"
"os"
- "os/signal"
"runtime"
"strconv"
"strings"
@@ -92,15 +91,9 @@ func runVolume(cmd *Command, args []string) bool {
glog.Fatalf(e.Error())
}
- // deal with control+c
- signalChan := make(chan os.Signal, 1)
- signal.Notify(signalChan, os.Interrupt, os.Kill)
- go func() {
- for _ = range signalChan {
- volumeServer.Shutdown()
- os.Exit(0)
- }
- }()
+ OnInterrupt(func() {
+ volumeServer.Shutdown()
+ })
if e := http.Serve(listener, r); e != nil {
glog.Fatalf("Fail to serve:%s", e.Error())