-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathgracehttp_main.go
More file actions
48 lines (42 loc) · 921 Bytes
/
gracehttp_main.go
File metadata and controls
48 lines (42 loc) · 921 Bytes
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
40
41
42
43
44
45
46
47
48
package main
import (
"flag"
"fmt"
"github.com/fevin/gracehttp"
"net/http"
"syscall"
"time"
)
var (
httpPort string
)
func init() {
flag.StringVar(&httpPort, "http_port", "9090", "the port of http server")
}
func main() {
hd := &Controller{}
grace := gracehttp.NewGraceHTTP()
srv := &http.Server{
Addr: ":" + httpPort,
Handler: hd,
ReadTimeout: time.Duration(time.Second),
WriteTimeout: time.Duration(time.Second),
}
option := &gracehttp.ServerOption{
HTTPServer: srv,
MaxListenConnection: 1000, // 限制 server 连接数
}
grace.AddServer(option)
if err := grace.Run(); err != nil {
fmt.Println(err)
}
}
type Controller struct {
}
func (this *Controller) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/ping" {
resp.Write([]byte(fmt.Sprintf("pong by pid:%d", syscall.Getpid())))
} else {
resp.Write([]byte("unknown"))
}
}