404-server: graceful shutdown
This commit is contained in:
parent
73f5b9ba2f
commit
2f8e81e383
1 changed files with 24 additions and 4 deletions
|
@ -18,11 +18,14 @@ limitations under the License.
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
@ -57,10 +60,27 @@ func main() {
|
||||||
fmt.Fprint(w, "ok")
|
fmt.Fprint(w, "ok")
|
||||||
})
|
})
|
||||||
http.Handle("/metrics", promhttp.Handler())
|
http.Handle("/metrics", promhttp.Handler())
|
||||||
// TODO: Use .Shutdown from Go 1.8
|
|
||||||
err := http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)
|
srv := &http.Server{
|
||||||
if err != nil {
|
Addr: fmt.Sprintf(":%d", *port),
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
err := srv.ListenAndServe()
|
||||||
|
if err != http.ErrServerClosed {
|
||||||
fmt.Fprintf(os.Stderr, "could not start http server: %s\n", err)
|
fmt.Fprintf(os.Stderr, "could not start http server: %s\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
stop := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(stop, syscall.SIGTERM)
|
||||||
|
<-stop
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
||||||
|
defer cancel()
|
||||||
|
err := srv.Shutdown(ctx)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "could not graceful shutdown http server: %s\n", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue