-
Notifications
You must be signed in to change notification settings - Fork 6
/
server_grpc.go
193 lines (163 loc) · 4.66 KB
/
server_grpc.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package hfw
import (
"context"
"errors"
"net"
"sync/atomic"
"time"
logger "github.com/hsyan2008/go-logger"
"github.com/hsyan2008/hfw/common"
"github.com/hsyan2008/hfw/configs"
"github.com/hsyan2008/hfw/grpc/discovery"
"github.com/hsyan2008/hfw/grpc/server"
"github.com/hsyan2008/hfw/prometheus"
"github.com/hsyan2008/hfw/signal"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/reflection"
"google.golang.org/grpc/status"
)
//如果是https+证书grpc,请配置好Server并使用NewGrpcServer+hfw.Run
//如果是grpc,请配置好Server和GrpcServer并使用NewGrpcServer+hfw.RunGrpc
//如果是grpc+http,请配置好Server和GrpcServer并使用NewGrpcServer+hfw.RunGrpc+hfw.Run
func NewGrpcServer(config configs.AllConfig) (s *grpc.Server, err error) {
return server.NewServer(config.Server.ServerConfig, grpc.UnaryInterceptor(UnaryServerInterceptor),
grpc.StreamInterceptor(StreamServerInterceptor))
}
var grpcListener net.Listener
func GetGrpcServerListener() net.Listener {
return grpcListener
}
func RunGrpc(s *grpc.Server, config configs.GrpcServerConfig) (err error) {
//监听信号
signalContext := signal.GetSignalContext()
go signalContext.Listen()
signalContext.Mix("grpc server Starting ...")
defer signalContext.Mix("grpc server Shutdowned!")
//等待工作完成
defer signalContext.Shutdowned()
address, err := common.GetAddrForListen(config.Address)
if err != nil {
logger.Fatal("grpc StartServer:", err)
return err
}
grpcListener, err = net.Listen("tcp", address)
if err != nil {
logger.Fatal("grpc StartServer:", err)
return err
}
//注册服务
r, err := discovery.RegisterServer(config.ServerConfig, common.GetServerAddr(grpcListener.Addr().String(), config.Address))
if err != nil {
return err
}
if r != nil {
defer r.UnRegister()
}
go func() {
signalContext.WgAdd()
defer signalContext.WgDone()
select {
case <-signalContext.Ctx.Done():
signalContext.Info("grpc server stoping...")
defer signalContext.Info("grpc server stoped")
s.GracefulStop()
}
}()
logger.Mix("Listen on grpc:", grpcListener.Addr().String())
// Register reflection service on gRPC server.
reflection.Register(s)
return s.Serve(grpcListener)
}
func UnaryServerInterceptor(
ctx context.Context, req interface{},
info *grpc.UnaryServerInfo, handler grpc.UnaryHandler,
) (resp interface{}, err error) {
httpCtx := NewHTTPContextWithGrpcIncomingCtx(ctx)
defer httpCtx.Cancel()
httpCtx.AppendPrefix("Path:" + info.FullMethod)
httpCtx.Debug("Req:", req)
defer func() {
if err == nil {
httpCtx.Debug("Res:", resp)
} else if status.Code(err) == codes.Canceled {
httpCtx.Warn("Err:", err)
} else {
httpCtx.Warn("Req:", req, "Err:", err)
}
}()
startTime := time.Now()
prometheus.RequestsTotal(info.FullMethod, "GRPC")
defer func() {
costTime := time.Since(startTime)
httpCtx.Mixf("Method:%s CostTime:%s", "GRPC", costTime)
prometheus.RequestsCosttime(info.FullMethod, "GRPC", costTime)
}()
onlineNum := atomic.AddUint32(&online, 1)
httpCtx.Mixf("Online:%d", onlineNum)
defer func() {
atomic.AddUint32(&online, ^uint32(0))
if e := recover(); e != nil {
if e == ErrStopRun {
return
}
err = errors.New("panic")
httpCtx.Fatal(err, string(common.GetStack()))
}
}()
err = checkConcurrence(onlineNum)
if err != nil {
return
}
return handler(httpCtx, req)
}
func StreamServerInterceptor(
srv interface{}, ss grpc.ServerStream,
info *grpc.StreamServerInfo, handler grpc.StreamHandler,
) (err error) {
httpCtx := NewHTTPContextWithGrpcIncomingCtx(ss.Context())
defer httpCtx.Cancel()
httpCtx.AppendPrefix("Path:" + info.FullMethod)
defer func() {
if err != nil {
httpCtx.Warn("Err:", err)
}
}()
startTime := time.Now()
prometheus.RequestsTotal(info.FullMethod, "Stream")
defer func() {
costTime := time.Since(startTime)
httpCtx.Mixf("Method:%s CostTime:%s", "Stream", costTime)
prometheus.RequestsCosttime(info.FullMethod, "Stream", costTime)
}()
onlineNum := atomic.AddUint32(&online, 1)
httpCtx.Mixf("Online:%d", onlineNum)
defer func() {
atomic.AddUint32(&online, ^uint32(0))
if e := recover(); e != nil {
if e == ErrStopRun {
return
}
err = errors.New("panic")
httpCtx.Fatal(err, string(common.GetStack()))
}
}()
err = checkConcurrence(onlineNum)
if err != nil {
return
}
return handler(srv, WarpServerStream(ss, httpCtx))
}
func WarpServerStream(ss grpc.ServerStream, httpCtx *HTTPContext) *GrpcServerStream {
return &GrpcServerStream{
ServerStream: ss,
ctx: httpCtx,
}
}
type GrpcServerStream struct {
grpc.ServerStream
ctx context.Context
}
func (w *GrpcServerStream) Context() context.Context {
return w.ctx
}