-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.go
158 lines (137 loc) · 3.92 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package server
import (
gocontext "context"
"fmt"
"github.com/orda-io/orda/client/pkg/constants"
"github.com/orda-io/orda/client/pkg/context"
"github.com/orda-io/orda/client/pkg/errors"
"github.com/orda-io/orda/client/pkg/iface"
"github.com/orda-io/orda/client/pkg/model"
svrConstant "github.com/orda-io/orda/server/constants"
"github.com/orda-io/orda/server/managers"
"google.golang.org/grpc/reflection"
"net"
"os"
"os/signal"
"sync"
"syscall"
"time"
"google.golang.org/grpc"
"github.com/orda-io/orda/server/service"
)
const banner = `
.:::: .::
.:: .:: .::
.:: .::.: .::: .:: .::
.:: .:: .:: .:: .:: .:: .::
.:: .:: .:: .: .::.:: .::
.:: .:: .:: .: .::.:: .::
.:::: .::: .:: .:: .:: .:::
`
const defaultGracefulTimeout = 10 * time.Second
// OrdaServer is an Orda server
type OrdaServer struct {
closed bool
mutex sync.Mutex
closedCh chan struct{}
rpcServer *grpc.Server
restServer *RestServer
ctx iface.OrdaContext
conf *managers.OrdaServerConfig
service *service.OrdaService
managers *managers.Managers
}
// NewOrdaServer creates a new Orda server
func NewOrdaServer(goCtx gocontext.Context, conf *managers.OrdaServerConfig) (*OrdaServer, errors.OrdaError) {
ctx := context.NewOrdaContext(goCtx, svrConstant.TagServer)
ctx.L().Infof("Config: %v", conf)
return &OrdaServer{
ctx: ctx,
conf: conf,
closed: false,
closedCh: make(chan struct{}),
}, nil
}
// Start starts the Orda Server
func (its *OrdaServer) Start() errors.OrdaError {
its.mutex.Lock()
defer its.mutex.Unlock()
var oErr errors.OrdaError
if its.managers, oErr = managers.New(its.ctx, its.conf); oErr != nil {
return oErr
}
lis, err := net.Listen("tcp", its.conf.GetRPCServerAddr())
if err != nil {
return errors.ServerInit.New(its.ctx.L(), "fail to listen RPC:"+err.Error())
}
its.rpcServer = grpc.NewServer()
reflection.Register(its.rpcServer)
its.service = service.NewOrdaService(its.managers)
model.RegisterOrdaServiceServer(its.rpcServer, its.service)
go func() {
its.ctx.L().Infof("open port: tcp://0.0.0.0%s", its.conf.GetRPCServerAddr())
if err := its.rpcServer.Serve(lis); err != nil {
_ = errors.ServerInit.New(its.ctx.L(), err.Error())
panic("fail to serve RPC Server")
}
}()
its.restServer = NewRestServer(its.ctx, its.conf, its.managers)
go func() {
if err := its.restServer.Start(); err != nil {
_ = errors.ServerInit.New(its.ctx.L(), err.Error())
panic("fail to serve control server")
}
}()
server := fmt.Sprintf("Orda-Server-%s (%s)", constants.Version, constants.BuildInfo)
its.ctx.L().Infof("%s Started at %s %s", server, time.Now().String(), banner)
its.ctx.L().Info("start Orda server successfully")
return nil
}
// Close closes all the server threads.
func (its *OrdaServer) Close(graceful bool) {
its.mutex.Lock()
defer func() {
its.managers.Close(its.ctx)
its.mutex.Unlock()
its.closed = true
}()
if graceful {
its.ctx.L().Infof("gracefully shutdown server")
its.rpcServer.GracefulStop()
} else {
its.ctx.L().Infof("Stop server")
its.rpcServer.Stop()
}
}
// HandleSignals can handle signals to the server.
func (its *OrdaServer) HandleSignals() int {
its.ctx.L().Infof("ready to handle signals")
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, syscall.SIGTERM, syscall.SIGINT, syscall.SIGHUP)
var sig os.Signal
select {
case s := <-signalCh:
sig = s
case <-its.closedCh:
return 0
}
its.ctx.L().Infof("caught signal: %s", sig.String())
graceful := false
if sig == syscall.SIGINT || sig == syscall.SIGTERM {
graceful = true
}
gracefulCh := make(chan struct{})
go func() {
its.Close(graceful)
close(gracefulCh)
}()
select {
case <-signalCh:
return 1
case <-time.After(defaultGracefulTimeout):
return 1
case <-gracefulCh:
its.ctx.L().Infof("closed successfully")
return 0
}
}