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
|
package pool
import (
"bytes"
"sync"
"github.com/fasthttp/websocket"
"github.com/spiral/roadrunner/v2/pkg/pubsub"
"github.com/spiral/roadrunner/v2/pkg/pubsub/message"
"github.com/spiral/roadrunner/v2/plugins/logger"
"github.com/spiral/roadrunner/v2/plugins/websockets/connection"
"github.com/spiral/roadrunner/v2/utils"
)
type WorkersPool struct {
storage map[string]pubsub.PubSub
connections *sync.Map
resPool sync.Pool
bPool sync.Pool
log logger.Logger
queue chan *message.Message
exit chan struct{}
}
// NewWorkersPool constructs worker pool for the websocket connections
func NewWorkersPool(pubsubs map[string]pubsub.PubSub, connections *sync.Map, log logger.Logger) *WorkersPool {
wp := &WorkersPool{
connections: connections,
queue: make(chan *message.Message, 100),
storage: pubsubs,
log: log,
exit: make(chan struct{}),
}
wp.resPool.New = func() interface{} {
return make(map[string]struct{}, 10)
}
wp.bPool.New = func() interface{} {
return new(bytes.Buffer)
}
// start 10 workers
for i := 0; i < 50; i++ {
wp.do()
}
return wp
}
func (wp *WorkersPool) Queue(msg *message.Message) {
wp.queue <- msg
}
func (wp *WorkersPool) Stop() {
for i := 0; i < 50; i++ {
wp.exit <- struct{}{}
}
close(wp.exit)
}
func (wp *WorkersPool) put(res map[string]struct{}) {
// optimized
// https://go-review.googlesource.com/c/go/+/110055/
// not O(n), but O(1)
for k := range res {
delete(res, k)
}
}
func (wp *WorkersPool) get() map[string]struct{} {
return wp.resPool.Get().(map[string]struct{})
}
func (wp *WorkersPool) putBytes(b *bytes.Buffer) {
b.Reset()
wp.bPool.Put(b)
}
func (wp *WorkersPool) getBytes() *bytes.Buffer {
return wp.bPool.Get().(*bytes.Buffer)
}
func (wp *WorkersPool) do() { //nolint:gocognit
go func() {
for {
select {
case msg, ok := <-wp.queue:
if !ok {
return
}
_ = msg
if msg == nil {
continue
}
if msg.TopicsLength() == 0 {
continue
}
br, ok := wp.storage[utils.AsString(msg.Broker())]
if !ok {
wp.log.Warn("no such broker", "requested", utils.AsString(msg.Broker()), "available", wp.storage)
continue
}
res := wp.get()
bb := wp.getBytes()
for i := 0; i < msg.TopicsLength(); i++ {
// get connections for the particular topic
br.Connections(utils.AsString(msg.Topics(i)), res)
}
if len(res) == 0 {
for i := 0; i < msg.TopicsLength(); i++ {
wp.log.Info("no such topic", "topic", utils.AsString(msg.Topics(i)))
}
wp.putBytes(bb)
wp.put(res)
continue
}
for i := range res {
c, ok := wp.connections.Load(i)
if !ok {
for i := 0; i < msg.TopicsLength(); i++ {
wp.log.Warn("the user disconnected connection before the message being written to it", "broker", utils.AsString(msg.Broker()), "topics", utils.AsString(msg.Topics(i)))
}
continue
}
conn := c.(*connection.Connection)
// put data into the bytes buffer
for i := 0; i < msg.PayloadLength(); i++ {
bb.WriteByte(byte(msg.Payload(i)))
}
err := conn.Write(websocket.BinaryMessage, bb.Bytes())
if err != nil {
for i := 0; i < msg.TopicsLength(); i++ {
wp.log.Error("error sending payload over the connection", "error", err, "broker", utils.AsString(msg.Broker()), "topics", utils.AsString(msg.Topics(i)))
}
continue
}
}
// put bytes buffer back
wp.putBytes(bb)
// put map with results back
wp.put(res)
case <-wp.exit:
wp.log.Info("get exit signal, exiting from the workers pool")
return
}
}
}()
}
|