-
Notifications
You must be signed in to change notification settings - Fork 72
/
provider.go
77 lines (63 loc) · 1.65 KB
/
provider.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
package node
import (
"context"
"github.com/dobyte/due/v2/cluster"
"github.com/dobyte/due/v2/errors"
"github.com/dobyte/due/v2/transport"
)
type provider struct {
node *Node
}
// Trigger 触发事件
func (p *provider) Trigger(ctx context.Context, args *transport.TriggerArgs) (bool, error) {
switch args.Event {
case cluster.Connect:
// ignore
case cluster.Reconnect:
if args.UID <= 0 {
return false, errors.ErrInvalidArgument
}
_, ok, err := p.node.proxy.AskNode(ctx, args.UID, p.node.opts.name, p.node.opts.id)
if err != nil {
return false, err
}
if !ok {
return true, errors.ErrNotFoundUserLocation
}
case cluster.Disconnect:
if args.UID > 0 {
_, ok, err := p.node.proxy.AskNode(ctx, args.UID, p.node.opts.name, p.node.opts.id)
if err != nil {
return false, err
}
if !ok {
return true, errors.ErrNotFoundUserLocation
}
}
}
p.node.events.trigger(args.Event, args.GID, args.CID, args.UID)
return false, nil
}
// Deliver 投递消息
func (p *provider) Deliver(ctx context.Context, args *transport.DeliverArgs) (bool, error) {
stateful, ok := p.node.router.CheckRouteStateful(args.Message.Route)
if !ok {
if ok = p.node.router.HasDefaultRouteHandler(); !ok {
return false, nil
}
}
if stateful {
if args.UID <= 0 {
return false, errors.ErrInvalidArgument
}
_, ok, err := p.node.proxy.AskNode(ctx, args.UID, p.node.opts.name, p.node.opts.id)
if err != nil {
return false, err
}
if !ok {
return true, errors.ErrNotFoundUserLocation
}
}
p.node.router.deliver(args.GID, args.NID, args.CID, args.UID, args.Message.Seq, args.Message.Route, args.Message.Buffer)
return false, nil
}