forked from ava-labs/hypersdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage_buffer.go
155 lines (134 loc) · 3.15 KB
/
message_buffer.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
// Copyright (C) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package pubsub
import (
"sync"
"time"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/timer"
"github.com/ava-labs/hypersdk/codec"
"github.com/ava-labs/hypersdk/consts"
"go.uber.org/zap"
)
type MessageBuffer struct {
Queue chan []byte
l sync.Mutex
log logging.Logger
pending [][]byte
pendingSize int
maxSize int
timeout time.Duration
pendingTimer *timer.Timer
closed bool
}
func NewMessageBuffer(log logging.Logger, pending int, maxSize int, timeout time.Duration) *MessageBuffer {
m := &MessageBuffer{
Queue: make(chan []byte, pending),
log: log,
pending: [][]byte{},
maxSize: maxSize,
timeout: timeout,
}
m.pendingTimer = timer.NewTimer(func() {
m.l.Lock()
defer m.l.Unlock()
if m.closed {
log.Debug("unable to clear pending messages", zap.Error(ErrClosed))
return
}
l := len(m.pending)
if l == 0 {
return
}
if err := m.clearPending(); err != nil {
log.Debug("unable to clear pending messages", zap.Error(err))
} else {
log.Debug("sent messages", zap.Int("count", l))
}
})
go m.pendingTimer.Dispatch()
return m
}
func (m *MessageBuffer) Close() error {
m.l.Lock()
defer m.l.Unlock()
if m.closed {
return ErrClosed
}
// Flush anything left
//
// It is up to the caller to ensure all of these items actually are written
// to the connection before it is closed.
if err := m.clearPending(); err != nil {
m.log.Debug("unable to clear pending messages", zap.Error(err))
return err
}
m.pendingTimer.Stop()
m.closed = true
close(m.Queue)
return nil
}
func (m *MessageBuffer) clearPending() error {
bm, err := CreateBatchMessage(m.maxSize, m.pending)
if err != nil {
return err
}
select {
case m.Queue <- bm:
default:
m.log.Debug("dropped pending message")
}
m.pendingSize = 0
m.pending = [][]byte{}
return nil
}
func (m *MessageBuffer) Send(msg []byte) error {
m.l.Lock()
defer m.l.Unlock()
if m.closed {
return ErrClosed
}
l := len(msg)
if l > m.maxSize {
return ErrMessageTooLarge
}
// Clear existing buffer if too large
if m.pendingSize+l > m.maxSize {
m.pendingTimer.Cancel()
if err := m.clearPending(); err != nil {
return err
}
}
m.pendingSize += l
m.pending = append(m.pending, msg)
if len(m.pending) == 1 {
m.pendingTimer.SetTimeoutIn(m.timeout)
}
return nil
}
func CreateBatchMessage(maxSize int, msgs [][]byte) ([]byte, error) {
size := consts.IntLen
for _, msg := range msgs {
size += codec.BytesLen(msg)
}
msgBatch := codec.NewWriter(size, maxSize)
msgBatch.PackInt(len(msgs))
for _, msg := range msgs {
msgBatch.PackBytes(msg)
}
return msgBatch.Bytes(), msgBatch.Err()
}
func ParseBatchMessage(maxSize int, msg []byte) ([][]byte, error) {
msgBatch := codec.NewReader(msg, maxSize)
msgLen := msgBatch.UnpackInt(true)
msgs := [][]byte{}
for i := 0; i < msgLen; i++ {
var nextMsg []byte
msgBatch.UnpackBytes(-1, true, &nextMsg)
if err := msgBatch.Err(); err != nil {
return nil, err
}
msgs = append(msgs, nextMsg)
}
return msgs, msgBatch.Err()
}