-
-
Notifications
You must be signed in to change notification settings - Fork 93
/
writer.go
161 lines (142 loc) · 3.48 KB
/
writer.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
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
159
160
161
package centrifuge
import (
"sync"
"time"
"github.com/centrifugal/centrifuge/internal/queue"
"github.com/centrifugal/centrifuge/internal/timers"
)
type writerConfig struct {
WriteManyFn func(...queue.Item) error
WriteFn func(item queue.Item) error
MaxQueueSize int
}
// writer helps to manage per-connection message byte queue.
type writer struct {
mu sync.Mutex
config writerConfig
messages *queue.Queue
closed bool
closeCh chan struct{}
}
func newWriter(config writerConfig, queueInitialCap int) *writer {
if queueInitialCap == 0 {
queueInitialCap = 2
}
w := &writer{
config: config,
messages: queue.New(queueInitialCap),
closeCh: make(chan struct{}),
}
return w
}
const (
defaultMaxMessagesInFrame = 16
)
func (w *writer) waitSendMessage(maxMessagesInFrame int, writeDelay time.Duration) bool {
// Wait for message from the queue.
ok := w.messages.Wait()
if !ok {
return false
}
if writeDelay > 0 {
tm := timers.AcquireTimer(writeDelay)
if writeDelay > 0 {
select {
case <-tm.C:
case <-w.closeCh:
timers.ReleaseTimer(tm)
return false
}
}
timers.ReleaseTimer(tm)
}
w.mu.Lock()
defer w.mu.Unlock()
msg, ok := w.messages.Remove()
if !ok {
return !w.messages.Closed()
}
var writeErr error
messageCount := w.messages.Len()
if (maxMessagesInFrame == -1 || maxMessagesInFrame > 1) && messageCount > 0 {
// There are several more messages left in queue, try to send them in single frame,
// but no more than maxMessagesInFrame.
// Limit message count to get from queue with (maxMessagesInFrame - 1)
// (as we already have one message received from queue above).
messagesCap := messageCount + 1
if messagesCap > maxMessagesInFrame && maxMessagesInFrame > -1 {
messagesCap = maxMessagesInFrame
}
messages := make([]queue.Item, 0, messagesCap)
messages = append(messages, msg)
for messageCount > 0 {
messageCount--
if maxMessagesInFrame > -1 && len(messages) >= maxMessagesInFrame {
break
}
m, ok := w.messages.Remove()
if ok {
messages = append(messages, m)
} else {
if w.messages.Closed() {
return false
}
break
}
}
if len(messages) == 1 {
writeErr = w.config.WriteFn(messages[0])
} else {
writeErr = w.config.WriteManyFn(messages...)
}
} else {
// WriteMany single message without allocating new slice.
writeErr = w.config.WriteFn(msg)
}
if writeErr != nil {
// WriteMany failed, transport must close itself, here we just return from routine.
return false
}
return true
}
// run supposed to be run in goroutine, this goroutine will be closed as
// soon as queue is closed.
func (w *writer) run(writeDelay time.Duration, maxMessagesInFrame int) {
if maxMessagesInFrame == 0 {
maxMessagesInFrame = defaultMaxMessagesInFrame
}
for {
if ok := w.waitSendMessage(maxMessagesInFrame, writeDelay); !ok {
return
}
}
}
func (w *writer) enqueue(item queue.Item) *Disconnect {
ok := w.messages.Add(item)
if !ok {
return &DisconnectConnectionClosed
}
if w.config.MaxQueueSize > 0 && w.messages.Size() > w.config.MaxQueueSize {
return &DisconnectSlow
}
return nil
}
func (w *writer) close(flushRemaining bool) error {
w.mu.Lock()
defer w.mu.Unlock()
if w.closed {
return nil
}
w.closed = true
if flushRemaining {
remaining := w.messages.CloseRemaining()
if len(remaining) > 0 {
// TODO: make it respect MaxMessagesInFrame option.
_ = w.config.WriteManyFn(remaining...)
}
} else {
w.messages.Close()
}
close(w.closeCh)
return nil
}