forked from topfreegames/pitaya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc_rpc_client.go
372 lines (329 loc) · 10.3 KB
/
grpc_rpc_client.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Copyright (c) TFG Co. All Rights Reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package cluster
import (
"context"
"fmt"
"sync"
"time"
opentracing "github.com/opentracing/opentracing-go"
"github.com/topfreegames/pitaya/config"
"github.com/topfreegames/pitaya/conn/message"
"github.com/topfreegames/pitaya/constants"
pcontext "github.com/topfreegames/pitaya/context"
pitErrors "github.com/topfreegames/pitaya/errors"
"github.com/topfreegames/pitaya/interfaces"
"github.com/topfreegames/pitaya/logger"
"github.com/topfreegames/pitaya/metrics"
"github.com/topfreegames/pitaya/protos"
"github.com/topfreegames/pitaya/route"
"github.com/topfreegames/pitaya/session"
"github.com/topfreegames/pitaya/tracing"
"google.golang.org/grpc"
)
// GRPCClient rpc server struct
type GRPCClient struct {
bindingStorage interfaces.BindingStorage
clientMap sync.Map
dialTimeout time.Duration
infoRetriever InfoRetriever
lazy bool
metricsReporters []metrics.Reporter
reqTimeout time.Duration
server *Server
}
// NewGRPCClient returns a new instance of GRPCClient
func NewGRPCClient(
config *config.Config,
server *Server,
metricsReporters []metrics.Reporter,
bindingStorage interfaces.BindingStorage,
infoRetriever InfoRetriever,
) (*GRPCClient, error) {
gs := &GRPCClient{
bindingStorage: bindingStorage,
infoRetriever: infoRetriever,
metricsReporters: metricsReporters,
server: server,
}
gs.configure(config)
return gs, nil
}
type grpcClient struct {
address string
cli protos.PitayaClient
conn *grpc.ClientConn
connected bool
lock sync.Mutex
}
// Init inits grpc rpc client
func (gs *GRPCClient) Init() error {
return nil
}
func (gs *GRPCClient) configure(cfg *config.Config) {
gs.dialTimeout = cfg.GetDuration("pitaya.cluster.rpc.client.grpc.dialtimeout")
gs.lazy = cfg.GetBool("pitaya.cluster.rpc.client.grpc.lazyconnection")
gs.reqTimeout = cfg.GetDuration("pitaya.cluster.rpc.client.grpc.requesttimeout")
}
// Call makes a RPC Call
func (gs *GRPCClient) Call(
ctx context.Context,
rpcType protos.RPCType,
route *route.Route,
session *session.Session,
msg *message.Message,
server *Server,
) (*protos.Response, error) {
c, ok := gs.clientMap.Load(server.ID)
if !ok {
return nil, constants.ErrNoConnectionToServer
}
parent, err := tracing.ExtractSpan(ctx)
if err != nil {
logger.Log.Warnf("[grpc client] failed to retrieve parent span: %s", err.Error())
}
tags := opentracing.Tags{
"span.kind": "client",
"local.id": gs.server.ID,
"peer.serverType": server.Type,
"peer.id": server.ID,
}
ctx = tracing.StartSpan(ctx, "RPC Call", tags, parent)
defer tracing.FinishSpan(ctx, err)
req, err := buildRequest(ctx, rpcType, route, session, msg, gs.server)
if err != nil {
return nil, err
}
ctxT, done := context.WithTimeout(ctx, gs.reqTimeout)
defer done()
if gs.metricsReporters != nil {
startTime := time.Now()
ctxT = pcontext.AddToPropagateCtx(ctxT, constants.StartTimeKey, startTime.UnixNano())
ctxT = pcontext.AddToPropagateCtx(ctxT, constants.RouteKey, route.String())
defer metrics.ReportTimingFromCtx(ctxT, gs.metricsReporters, "rpc", err)
}
res, err := c.(*grpcClient).call(ctxT, &req)
if err != nil {
return nil, err
}
if res.Error != nil {
if res.Error.Code == "" {
res.Error.Code = pitErrors.ErrUnknownCode
}
err = &pitErrors.Error{
Code: res.Error.Code,
Message: res.Error.Msg,
Metadata: res.Error.Metadata,
}
return nil, err
}
return res, nil
}
// Send not implemented in grpc client
func (gs *GRPCClient) Send(uid string, d []byte) error {
return constants.ErrNotImplemented
}
// BroadcastSessionBind sends the binding information to other servers that may be interested in this info
func (gs *GRPCClient) BroadcastSessionBind(uid string) error {
if gs.bindingStorage == nil {
return constants.ErrNoBindingStorageModule
}
fid, _ := gs.bindingStorage.GetUserFrontendID(uid, gs.server.Type)
if fid != "" {
if c, ok := gs.clientMap.Load(fid); ok {
msg := &protos.BindMsg{
Uid: uid,
Fid: gs.server.ID,
}
ctxT, done := context.WithTimeout(context.Background(), gs.reqTimeout)
defer done()
err := c.(*grpcClient).sessionBindRemote(ctxT, msg)
return err
}
}
return nil
}
// SendKick sends a kick to an user
func (gs *GRPCClient) SendKick(userID string, serverType string, kick *protos.KickMsg) error {
var svID string
var err error
if gs.bindingStorage == nil {
return constants.ErrNoBindingStorageModule
}
svID, err = gs.bindingStorage.GetUserFrontendID(userID, serverType)
if err != nil {
return err
}
if c, ok := gs.clientMap.Load(svID); ok {
ctxT, done := context.WithTimeout(context.Background(), gs.reqTimeout)
defer done()
err := c.(*grpcClient).sendKick(ctxT, kick)
return err
}
return constants.ErrNoConnectionToServer
}
// SendPush sends a message to an user, if you dont know the serverID that the user is connected to, you need to set a BindingStorage when creating the client
// TODO: Jaeger?
func (gs *GRPCClient) SendPush(userID string, frontendSv *Server, push *protos.Push) error {
var svID string
var err error
if frontendSv.ID != "" {
svID = frontendSv.ID
} else {
if gs.bindingStorage == nil {
return constants.ErrNoBindingStorageModule
}
svID, err = gs.bindingStorage.GetUserFrontendID(userID, frontendSv.Type)
if err != nil {
return err
}
}
if c, ok := gs.clientMap.Load(svID); ok {
ctxT, done := context.WithTimeout(context.Background(), gs.reqTimeout)
defer done()
err := c.(*grpcClient).pushToUser(ctxT, push)
return err
}
return constants.ErrNoConnectionToServer
}
// AddServer is called when a new server is discovered
func (gs *GRPCClient) AddServer(sv *Server) {
var host, port, portKey string
var ok bool
host, portKey = gs.getServerHost(sv)
if host == "" {
logger.Log.Errorf("[grpc client] server %s has no grpcHost specified in metadata", sv.ID)
return
}
if port, ok = sv.Metadata[portKey]; !ok {
logger.Log.Errorf("[grpc client] server %s has no %s specified in metadata", sv.ID, portKey)
return
}
address := fmt.Sprintf("%s:%s", host, port)
client := &grpcClient{address: address}
if !gs.lazy {
if err := client.connect(); err != nil {
logger.Log.Errorf("[grpc client] unable to connect to server %s at %s: %v", sv.ID, address, err)
}
}
gs.clientMap.Store(sv.ID, client)
logger.Log.Debugf("[grpc client] added server %s at %s", sv.ID, address)
}
// RemoveServer is called when a server is removed
func (gs *GRPCClient) RemoveServer(sv *Server) {
if c, ok := gs.clientMap.Load(sv.ID); ok {
c.(*grpcClient).disconnect()
gs.clientMap.Delete(sv.ID)
logger.Log.Debugf("[grpc client] removed server %s", sv.ID)
}
}
// AfterInit runs after initialization
func (gs *GRPCClient) AfterInit() {}
// BeforeShutdown runs before shutdown
func (gs *GRPCClient) BeforeShutdown() {}
// Shutdown stops grpc rpc server
func (gs *GRPCClient) Shutdown() error {
return nil
}
func (gs *GRPCClient) getServerHost(sv *Server) (host, portKey string) {
var (
serverRegion, hasRegion = sv.Metadata[constants.RegionKey]
externalHost, hasExternal = sv.Metadata[constants.GRPCExternalHostKey]
internalHost, _ = sv.Metadata[constants.GRPCHostKey]
)
hasRegion = hasRegion && serverRegion != ""
hasExternal = hasExternal && externalHost != ""
if !hasRegion {
if hasExternal {
logger.Log.Warnf("[grpc client] server %s has no region specified in metadata, using external host", sv.ID)
return externalHost, constants.GRPCExternalPortKey
}
logger.Log.Warnf("[grpc client] server %s has no region nor external host specified in metadata, using internal host", sv.ID)
return internalHost, constants.GRPCPortKey
}
if gs.infoRetriever.Region() == serverRegion || !hasExternal {
logger.Log.Infof("[grpc client] server %s is in same region or external host not provided, using internal host", sv.ID)
return internalHost, constants.GRPCPortKey
}
logger.Log.Infof("[grpc client] server %s is in other region, using external host", sv.ID)
return externalHost, constants.GRPCExternalPortKey
}
func (gc *grpcClient) connect() error {
gc.lock.Lock()
defer gc.lock.Unlock()
if gc.connected {
return nil
}
conn, err := grpc.Dial(
gc.address,
grpc.WithInsecure(),
)
if err != nil {
return err
}
c := protos.NewPitayaClient(conn)
gc.cli = c
gc.conn = conn
gc.connected = true
return nil
}
func (gc *grpcClient) disconnect() {
gc.lock.Lock()
if gc.connected {
gc.conn.Close()
gc.connected = false
}
gc.lock.Unlock()
}
func (gc *grpcClient) pushToUser(ctx context.Context, push *protos.Push) error {
if !gc.connected {
if err := gc.connect(); err != nil {
return err
}
}
_, err := gc.cli.PushToUser(ctx, push)
return err
}
func (gc *grpcClient) call(ctx context.Context, req *protos.Request) (*protos.Response, error) {
if !gc.connected {
if err := gc.connect(); err != nil {
return nil, err
}
}
return gc.cli.Call(ctx, req)
}
func (gc *grpcClient) sessionBindRemote(ctx context.Context, req *protos.BindMsg) error {
if !gc.connected {
if err := gc.connect(); err != nil {
return err
}
}
_, err := gc.cli.SessionBindRemote(ctx, req)
return err
}
func (gc *grpcClient) sendKick(ctx context.Context, req *protos.KickMsg) error {
if !gc.connected {
if err := gc.connect(); err != nil {
return err
}
}
_, err := gc.cli.KickUser(ctx, req)
return err
}