-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
63 lines (53 loc) · 1.28 KB
/
http.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package mago
import (
"context"
"fmt"
"time"
"github.com/maliboot/mago/config"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/common/hlog"
)
type Http struct {
*server.Hertz
c *config.Conf
}
func NewDefaultHttp(c *config.Conf) *Http {
return &Http{
server.Default(server.WithHostPorts(fmt.Sprintf("%s:%d", c.Server.Http.IP, c.Server.Http.Port))),
c,
}
}
func NewHttp(c *config.Conf, hz *server.Hertz) *Http {
return &Http{
hz,
c,
}
}
func (h *Http) Start(_ context.Context) error {
err := h.Run()
if err != nil {
return err
}
return nil
}
func (h *Http) Stop(ctx context.Context) error {
if forceErr, ok := ctx.Value(StopError{}).(error); ok && forceErr != nil {
// 强制关闭
hlog.Errorf("Hertz: Receive close signal: error=%v", forceErr)
if err := h.Close(); err != nil {
hlog.Errorf("Hertz: Close error=%v", err)
}
}
hlog.SystemLogger().Infof(
"Server[%s:%d]Begin graceful shutdown, wait at most num=%d seconds...",
h.c.Server.Http.IP,
h.c.Server.Http.Port,
h.GetOptions().ExitWaitTimeout/time.Second,
)
ctx, cancel := context.WithTimeout(ctx, h.GetOptions().ExitWaitTimeout/time.Second)
defer cancel()
if err := h.Shutdown(ctx); err != nil {
hlog.SystemLogger().Errorf("Shutdown error=%v", err)
}
return nil
}