blob: eb87b39e986cb0a5c8bb4e4ed1de1d09c2fa61b1 (
plain)
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
|
package memory
import (
"sync"
"github.com/spiral/roadrunner/v2/pkg/pubsub/message"
"github.com/spiral/roadrunner/v2/plugins/logger"
"github.com/spiral/roadrunner/v2/utils"
)
const (
PluginName string = "memory"
)
type Plugin struct {
log logger.Logger
// channel with the messages from the RPC
pushCh chan []byte
// user-subscribed topics
topics sync.Map
}
func (p *Plugin) Init(log logger.Logger) error {
p.log = log
p.pushCh = make(chan []byte, 100)
return nil
}
// Available interface implementation for the plugin
func (p *Plugin) Available() {}
// Name is endure.Named interface implementation
func (p *Plugin) Name() string {
return PluginName
}
func (p *Plugin) Publish(messages []byte) error {
p.pushCh <- messages
return nil
}
func (p *Plugin) PublishAsync(messages []byte) {
go func() {
p.pushCh <- messages
}()
}
func (p *Plugin) Subscribe(topics ...string) error {
for i := 0; i < len(topics); i++ {
p.topics.Store(topics[i], struct{}{})
}
return nil
}
func (p *Plugin) Unsubscribe(topics ...string) error {
for i := 0; i < len(topics); i++ {
p.topics.Delete(topics[i])
}
return nil
}
func (p *Plugin) Next() (*message.Message, error) {
msg := <-p.pushCh
if msg == nil {
return nil, nil
}
fbsMsg := message.GetRootAsMessage(msg, 0)
// push only messages, which are subscribed
// TODO better???
for i := 0; i < fbsMsg.TopicsLength(); i++ {
if _, ok := p.topics.Load(utils.AsString(fbsMsg.Topics(i))); ok {
return fbsMsg, nil
}
}
return nil, nil
}
|