File tree Expand file tree Collapse file tree 2 files changed +50
-0
lines changed
Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change 1+ version : ' 3'
2+
3+ tasks :
4+ run :
5+ cmds :
6+ - go run -race main.go
Original file line number Diff line number Diff line change 1+ package main
2+
3+ import (
4+ "context"
5+ "log"
6+ "net/http"
7+ "time"
8+ )
9+
10+ func main () {
11+ var (
12+ mux = http .NewServeMux ()
13+ srv = & http.Server {Addr : ":8888" , Handler : mux }
14+ mainCtx = context .Background ()
15+ procCtx , procCxl = context .WithTimeout (mainCtx , 3 * time .Second )
16+ )
17+ defer procCxl ()
18+
19+ // ----------- Start ----------- //
20+
21+ go func () {
22+ log .Println ("Server is up." )
23+ srv .ListenAndServe ()
24+ }()
25+
26+ <- procCtx .Done ()
27+
28+ // ----------- Shutdown ----------- //
29+
30+ var (
31+ shutdownCtx , shutdownCxl = context .WithTimeout (mainCtx , 1 * time .Second )
32+ )
33+ defer shutdownCxl ()
34+
35+ if err := srv .Shutdown (shutdownCtx ); err != nil {
36+ switch err {
37+ case context .DeadlineExceeded :
38+ log .Println ("Server shutdown process timed out." )
39+ default :
40+ log .Fatal (err )
41+ }
42+ }
43+ log .Println ("Server has been shutdown." )
44+ }
You can’t perform that action at this time.
0 commit comments