forked from topfreegames/pitaya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_pool.go
126 lines (107 loc) · 3.4 KB
/
handler_pool.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
package service
import (
"context"
"fmt"
"reflect"
"github.com/topfreegames/pitaya/v2/component"
"github.com/topfreegames/pitaya/v2/conn/message"
"github.com/topfreegames/pitaya/v2/constants"
e "github.com/topfreegames/pitaya/v2/errors"
"github.com/topfreegames/pitaya/v2/logger/interfaces"
"github.com/topfreegames/pitaya/v2/pipeline"
"github.com/topfreegames/pitaya/v2/route"
"github.com/topfreegames/pitaya/v2/serialize"
"github.com/topfreegames/pitaya/v2/session"
"github.com/topfreegames/pitaya/v2/util"
)
// HandlerPool ...
type HandlerPool struct {
handlers map[string]*component.Handler // all handler method
}
// NewHandlerPool ...
func NewHandlerPool() *HandlerPool {
return &HandlerPool{
handlers: make(map[string]*component.Handler),
}
}
// Register ...
func (h *HandlerPool) Register(serviceName string, name string, handler *component.Handler) {
h.handlers[fmt.Sprintf("%s.%s", serviceName, name)] = handler
}
// GetHandlers ...
func (h *HandlerPool) GetHandlers() map[string]*component.Handler {
return h.handlers
}
// ProcessHandlerMessage ...
func (h *HandlerPool) ProcessHandlerMessage(
ctx context.Context,
rt *route.Route,
serializer serialize.Serializer,
handlerHooks *pipeline.HandlerHooks,
session session.Session,
data []byte,
msgTypeIface interface{},
remote bool,
) ([]byte, error) {
if ctx == nil {
ctx = context.Background()
}
ctx = context.WithValue(ctx, constants.SessionCtxKey, session)
ctx = util.CtxWithDefaultLogger(ctx, rt.String(), session.UID())
handler, err := h.getHandler(rt)
if err != nil {
return nil, e.NewError(err, e.ErrNotFoundCode)
}
msgType, err := getMsgType(msgTypeIface)
if err != nil {
return nil, e.NewError(err, e.ErrInternalCode)
}
logger := ctx.Value(constants.LoggerCtxKey).(interfaces.Logger)
exit, err := handler.ValidateMessageType(msgType)
if err != nil && exit {
return nil, e.NewError(err, e.ErrBadRequestCode)
} else if err != nil {
logger.Warnf("invalid message type, error: %s", err.Error())
}
// First unmarshal the handler arg that will be passed to
// both handler and pipeline functions
arg, err := unmarshalHandlerArg(handler, serializer, data)
if err != nil {
return nil, e.NewError(err, e.ErrBadRequestCode)
}
ctx, arg, err = handlerHooks.BeforeHandler.ExecuteBeforePipeline(ctx, arg)
if err != nil {
return nil, err
}
logger.Debugf("SID=%d, Data=%s", session.ID(), data)
args := []reflect.Value{handler.Receiver, reflect.ValueOf(ctx)}
if arg != nil {
args = append(args, reflect.ValueOf(arg))
}
resp, err := util.Pcall(handler.Method, args)
if remote && msgType == message.Notify {
// This is a special case and should only happen with nats rpc client
// because we used nats request we have to answer to it or else a timeout
// will happen in the caller server and will be returned to the client
// the reason why we don't just Publish is to keep track of failed rpc requests
// with timeouts, maybe we can improve this flow
resp = []byte("ack")
}
resp, err = handlerHooks.AfterHandler.ExecuteAfterPipeline(ctx, resp, err)
if err != nil {
return nil, err
}
ret, err := serializeReturn(serializer, resp)
if err != nil {
return nil, err
}
return ret, nil
}
func (h *HandlerPool) getHandler(rt *route.Route) (*component.Handler, error) {
handler, ok := h.handlers[rt.Short()]
if !ok {
e := fmt.Errorf("pitaya/handler: %s not found", rt.String())
return nil, e
}
return handler, nil
}