forked from snapp-incubator/thanos-federate-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
39 lines (31 loc) · 756 Bytes
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main
import (
"context"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"k8s.io/klog/v2"
)
func startServer(listen string, mux *http.ServeMux, cancel context.CancelFunc) {
srv := &http.Server{
Addr: listen,
Handler: mux,
}
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
go func() {
klog.Infof("Listening on port %s ...\n", listen)
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
klog.Fatalf("listen:%+s\n", err)
}
}()
<-sigCh
cancel()
timeoutCtx, cancelTimeout := context.WithTimeout(context.Background(), time.Second*5)
defer cancelTimeout()
if err := srv.Shutdown(timeoutCtx); err != nil {
klog.Infof("HTTP server Shutdown: %v", err)
}
}