-
Notifications
You must be signed in to change notification settings - Fork 701
/
server.go
76 lines (64 loc) · 1.36 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package governor
import (
"context"
"net"
"net/http"
"github.com/douyu/jupiter/pkg/core/constant"
"github.com/douyu/jupiter/pkg/server"
"github.com/douyu/jupiter/pkg/xlog"
)
// Server ...
type Server struct {
*http.Server
listener net.Listener
*Config
}
func newServer(config *Config) *Server {
var listener, err = net.Listen("tcp4", config.Address())
if err != nil {
xlog.Jupiter().Panic("governor start error", xlog.FieldErr(err))
}
return &Server{
Server: &http.Server{
Addr: config.Address(),
Handler: DefaultServeMux,
},
listener: listener,
Config: config,
}
}
//Serve ..
func (s *Server) Serve() error {
err := s.Server.Serve(s.listener)
if err == http.ErrServerClosed {
return nil
}
return err
}
//Stop ..
func (s *Server) Stop() error {
return s.Server.Close()
}
//GracefulStop ..
func (s *Server) GracefulStop(ctx context.Context) error {
return s.Server.Shutdown(ctx)
}
// Healthz
// TODO(roamerlv):
func (s *Server) Healthz() bool {
return true
}
//Info ..
func (s *Server) Info() *server.ServiceInfo {
serviceAddr := s.listener.Addr().String()
if s.Config.ServiceAddress != "" {
serviceAddr = s.Config.ServiceAddress
}
info := server.ApplyOptions(
server.WithScheme("govern"),
server.WithAddress(serviceAddr),
server.WithKind(constant.ServiceGovernor),
)
// info.Name = info.Name + "." + ModName
return &info
}