Skip to content

Commit

Permalink
Merge pull request #1598 from acoshift/404-server-shutdown
Browse files Browse the repository at this point in the history
404-server: graceful shutdown
  • Loading branch information
aledbf authored Oct 27, 2017
2 parents 3751159 + 2f8e81e commit e4e1e68
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions images/404-server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ limitations under the License.
package main

import (
"context"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"strconv"
"syscall"
"time"

"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -57,10 +60,27 @@ func main() {
fmt.Fprint(w, "ok")
})
http.Handle("/metrics", promhttp.Handler())
// TODO: Use .Shutdown from Go 1.8
err := http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)

srv := &http.Server{
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)
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 start http server: %s\n", err)
os.Exit(1)
fmt.Fprintf(os.Stderr, "could not graceful shutdown http server: %s\n", err)
}
}

0 comments on commit e4e1e68

Please sign in to comment.