-
Notifications
You must be signed in to change notification settings - Fork 3
/
funcs.go
125 lines (115 loc) · 3.72 KB
/
funcs.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
package callback
import (
"context"
pb "github.com/meitu/bifrost/grpc/callback"
)
// OnConnect call backend with username password clientID cleansessiOn and address.
// return connection_code cookie statlabel and error
func (c *callback) OnConnect(ctx context.Context, service string, req *pb.OnConnectRequest) (*pb.OnConnectReply, error) {
if s, ok := c.record.OnConnect[service]; ok {
reply, err := s.OnConnect(ctx, req)
if err != nil {
return nil, err
}
return reply, nil
}
return &pb.OnConnectReply{}, nil
}
// OnSubscribe called On subscribtion return success and Cookie in return
func (c *callback) OnSubscribe(ctx context.Context, service string, req *pb.OnSubscribeRequest) (*pb.OnSubscribeReply, error) {
if s, ok := c.record.OnSubscribe[service]; ok {
reply, err := s.OnSubscribe(ctx, req)
if err != nil {
return nil, err
}
if len(reply.Cookie) == 0 {
reply.Cookie = req.Cookie
}
return reply, nil
}
return &pb.OnSubscribeReply{Cookie: req.Cookie}, nil
}
// PostSubscribe called after subscribe succeed, return cookie and error
func (c *callback) PostSubscribe(ctx context.Context, service string, req *pb.PostSubscribeRequest) (*pb.PostSubscribeReply, error) {
if s, ok := c.record.PostSubscribe[service]; ok {
reply, err := s.PostSubscribe(ctx, req)
if err != nil {
return nil, err
}
if len(reply.Cookie) == 0 {
reply.Cookie = req.Cookie
}
return reply, nil
}
return &pb.PostSubscribeReply{Cookie: req.Cookie}, nil
}
// OnPublish called On client call pbPublish, callback may return errSkip to break publish flow
// return cookid and error
func (c *callback) OnPublish(ctx context.Context, service string, req *pb.OnPublishRequest) (*pb.OnPublishReply, error) {
if s, ok := c.record.OnPublish[service]; ok {
reply, err := s.OnPublish(ctx, req)
if err != nil {
return nil, err
}
if len(reply.Cookie) == 0 {
reply.Cookie = req.Cookie
}
if len(reply.Message) == 0 {
reply.Message = req.Message
}
return reply, nil
}
return &pb.OnPublishReply{Skip: false, Cookie: req.Cookie, Message: req.Message}, nil
}
// OnUnsubscribe called On Unsubscribtion, return cookie and error
func (c *callback) OnUnsubscribe(ctx context.Context, service string, req *pb.OnUnsubscribeRequest) (*pb.OnUnsubscribeReply, error) {
if s, ok := c.record.OnUnsubscribe[service]; ok {
reply, err := s.OnUnsubscribe(ctx, req)
if err != nil {
return nil, err
}
if len(reply.Cookie) == 0 {
reply.Cookie = req.Cookie
}
return reply, nil
}
return &pb.OnUnsubscribeReply{Cookie: req.Cookie}, nil
}
// OnDisconnect called On Disconnect. Only error return
func (c *callback) OnDisconnect(ctx context.Context, service string, req *pb.OnDisconnectRequest) (*pb.OnDisconnectReply, error) {
if s, ok := c.record.OnDisconnect[service]; ok {
_, err := s.OnDisconnect(ctx, req)
if err != nil {
return nil, err
}
}
return &pb.OnDisconnectReply{}, nil
}
// OnACK called OnACK . return OnACKReply and error
func (c *callback) OnACK(ctx context.Context, service string, req *pb.OnACKRequest) (*pb.OnACKReply, error) {
if s, ok := c.record.OnACK[service]; ok {
reply, err := s.OnACK(ctx, req)
if err != nil {
return nil, err
}
if len(reply.Cookie) == 0 {
reply.Cookie = req.Cookie
}
return reply, nil
}
return &pb.OnACKReply{Cookie: req.Cookie}, nil
}
// OnOffline called OnOffline. return OnOfflineReply and error
func (c *callback) OnOffline(ctx context.Context, service string, req *pb.OnOfflineRequest) (*pb.OnOfflineReply, error) {
if s, ok := c.record.OnOffline[service]; ok {
reply, err := s.OnOffline(ctx, req)
if err != nil {
return nil, err
}
if len(reply.Cookie) == 0 {
reply.Cookie = req.Cookie
}
return reply, nil
}
return &pb.OnOfflineReply{Cookie: req.Cookie}, nil
}