@cryptotaxi247 / kubo / commits / 8cf765af3

daemon: listen for term signals

Juan Batiz-Benet committed Nov 15, 2014 at 22:11 UTC 8cf765af334386220e53fded13bfbc15cfc8c470
3 files changed +41 -8
cmd/ipfs2/daemon.go
+39 -6
@@ -3,10 +3,14 @@ package main
3 import (
4 "fmt"
5 "net/http"
6 + "os"
7 + "os/signal"
8 + "syscall"
9
10 ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
11 manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net"
12
13 + manners "github.com/braintree/manners"
14 cmds "github.com/jbenet/go-ipfs/commands"
15 cmdsHttp "github.com/jbenet/go-ipfs/commands/http"
16 core "github.com/jbenet/go-ipfs/core"
@@ -63,18 +67,47 @@ func daemonFunc(req cmds.Request) (interface{}, error) {
67 return nil, err
68 }
69
70 + mux := http.NewServeMux()
71 cmdHandler := cmdsHttp.NewHandler(*req.Context(), commands.Root)
67 - http.Handle(cmdsHttp.ApiPath+"/", cmdHandler)
72 + mux.Handle(cmdsHttp.ApiPath+"/", cmdHandler)
73
74 ifpsHandler := &ipfsHandler{node}
70 - http.Handle("/ipfs/", ifpsHandler)
75 + mux.Handle("/ipfs/", ifpsHandler)
76 +
77 + err = listenAndServe(mux, host)
78 + return nil, err
79 +}
80 +
81 +func listenAndServe(mux *http.ServeMux, host string) error {
82
83 fmt.Printf("API server listening on '%s'\n", host)
84 + s := manners.NewServer()
85 + done := make(chan struct{}, 1)
86 + defer func() {
87 + done <- struct{}{}
88 + }()
89
74 - err = http.ListenAndServe(host, nil)
75 - if err != nil {
76 - return nil, err
90 + // go wait until we kill it.
91 + go func() {
92 + sig := sigTerm()
93 + select {
94 + case <-done:
95 + log.Info("daemon terminated at %s.", host)
96 + case <-sig:
97 + s.Shutdown <- true
98 + log.Info("terminating daemon at %s...", host)
99 + }
100 + }()
101 +
102 + if err := s.ListenAndServe(host, mux); err != nil {
103 + return err
104 }
105 + return nil
106 +}
107
79 - return nil, nil
108 +func sigTerm() chan os.Signal {
109 + sigc := make(chan os.Signal, 1)
110 + signal.Notify(sigc, syscall.SIGHUP, syscall.SIGINT,
111 + syscall.SIGTERM, syscall.SIGQUIT)
112 + return sigc
113 }
server/http/http.go
+1 -1
@@ -6,7 +6,7 @@ import (
6 "io"
7 "net/http"
8
9 - "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gorilla/mux"
9 + mux "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gorilla/mux"
10 ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
11 manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net"
12 mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
tour/chapter.go
+1 -1
@@ -2,7 +2,7 @@ package tour
2
3 import "fmt"
4
5 -// returns a partially applied function.
5 +// returns a partially applied function.
6 //
7 // It's designed to make it easy to re-order chapters with minimal fuss.
8 //