-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwrite_buffer.go
More file actions
284 lines (245 loc) · 6.94 KB
/
write_buffer.go
File metadata and controls
284 lines (245 loc) · 6.94 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package blockqueue
import (
"context"
"fmt"
"log/slog"
"strings"
"sync"
"time"
"github.com/google/uuid"
"github.com/jmoiron/sqlx"
)
// WriteBuffer collects messages and batch inserts them for improved throughput
type WriteBuffer struct {
messages chan writeRequest
db *db
batchSize int
flushInterval time.Duration
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
notify func(topicId uuid.UUID)
}
// writeRequest represents a single message to be enqueued
type writeRequest struct {
TopicId uuid.UUID
MessageId string
Message string
VisibleAt time.Time
}
// WriteBufferConfig holds configuration for the write buffer
type WriteBufferConfig struct {
BatchSize int // Max messages before flush (default: 100)
FlushInterval time.Duration // Max time before flush (default: 50ms)
BufferSize int // Channel buffer size (default: 10000)
Notify func(topicId uuid.UUID)
}
// DefaultWriteBufferConfig returns sensible defaults
func DefaultWriteBufferConfig() WriteBufferConfig {
return WriteBufferConfig{
BatchSize: 100,
FlushInterval: 50 * time.Millisecond,
BufferSize: 10000,
Notify: func(topicId uuid.UUID) {}, // No-op default
}
}
// NewWriteBuffer creates a new write buffer
func NewWriteBuffer(ctx context.Context, database *db, config WriteBufferConfig) *WriteBuffer {
if config.BatchSize <= 0 {
config.BatchSize = 100
}
if config.FlushInterval <= 0 {
config.FlushInterval = 50 * time.Millisecond
}
if config.BufferSize <= 0 {
config.BufferSize = 10000
}
bufCtx, cancel := context.WithCancel(ctx)
wb := &WriteBuffer{
messages: make(chan writeRequest, config.BufferSize),
db: database,
batchSize: config.BatchSize,
flushInterval: config.FlushInterval,
ctx: bufCtx,
cancel: cancel,
notify: config.Notify,
}
wb.wg.Add(1)
go wb.run()
return wb
}
// Enqueue adds a message to the buffer for batch insertion
func (w *WriteBuffer) Enqueue(topicId uuid.UUID, messageId, message string, delay time.Duration) {
select {
case w.messages <- writeRequest{
TopicId: topicId,
MessageId: messageId,
Message: message,
VisibleAt: time.Now().UTC().Add(delay).Truncate(time.Second),
}:
case <-w.ctx.Done():
slog.Warn("write buffer closed, message dropped",
"topic_id", topicId,
"message_id", messageId,
)
}
}
// Close gracefully shuts down the write buffer
func (w *WriteBuffer) Close() {
w.cancel()
w.wg.Wait()
}
// run is the main goroutine that batches and flushes messages
func (w *WriteBuffer) run() {
defer w.wg.Done()
batch := make([]writeRequest, 0, w.batchSize)
ticker := time.NewTicker(w.flushInterval)
defer ticker.Stop()
for {
select {
case <-w.ctx.Done():
// Flush remaining messages before exit
if len(batch) > 0 {
w.flush(batch)
}
// Drain channel
for {
select {
case req := <-w.messages:
batch = append(batch, req)
if len(batch) >= w.batchSize {
w.flush(batch)
batch = batch[:0]
}
default:
if len(batch) > 0 {
w.flush(batch)
}
return
}
}
case req := <-w.messages:
batch = append(batch, req)
if len(batch) >= w.batchSize {
w.flush(batch)
batch = batch[:0]
}
case <-ticker.C:
if len(batch) > 0 {
w.flush(batch)
batch = batch[:0]
}
}
}
}
// flush performs batch insert of messages
func (w *WriteBuffer) flush(batch []writeRequest) {
if len(batch) == 0 {
return
}
ctx := context.Background()
err := w.db.batchEnqueueToSubscribers(ctx, batch)
if err != nil {
slog.Error("write buffer flush failed",
logPrefixErr, err,
"batch_size", len(batch),
)
} else {
slog.Debug("write buffer flushed",
"batch_size", len(batch),
)
// Notify subscribers
if w.notify != nil {
uniqueTopics := make(map[uuid.UUID]struct{})
for _, req := range batch {
uniqueTopics[req.TopicId] = struct{}{}
}
for topicId := range uniqueTopics {
w.notify(topicId)
}
}
}
}
// batchEnqueueToSubscribers performs efficient batch insert
func (d *db) batchEnqueueToSubscribers(ctx context.Context, requests []writeRequest) error {
if len(requests) == 0 {
return nil
}
// Group by topic for efficient insertion
byTopic := make(map[uuid.UUID][]writeRequest)
for _, req := range requests {
byTopic[req.TopicId] = append(byTopic[req.TopicId], req)
}
return d.tx(ctx, func(ctx context.Context, tx *sqlx.Tx) error {
for topicId, topicRequests := range byTopic {
// Get all subscribers for this topic (cached)
subscribers, err := d.getSubscribersForTopicCached(ctx, topicId)
if err != nil {
return err
}
if len(subscribers) == 0 {
continue
}
valueStrings := make([]string, 0, len(topicRequests)*len(subscribers))
valueArgs := make([]interface{}, 0, len(topicRequests)*len(subscribers)*5)
for _, req := range topicRequests {
for _, subId := range subscribers {
valueStrings = append(valueStrings, "(?, ?, ?, ?, 'pending', ?)")
valueArgs = append(valueArgs, subId, topicId, req.MessageId, req.Message, req.VisibleAt.Format("2006-01-02 15:04:05"))
}
}
query := fmt.Sprintf(
"INSERT INTO subscriber_messages (subscriber_id, topic_id, message_id, message, status, visible_at) VALUES %s",
strings.Join(valueStrings, ", "),
)
_, err = tx.ExecContext(ctx, tx.Rebind(query), valueArgs...)
if err != nil {
return err
}
}
return nil
})
}
// subscriberCache caches subscriber IDs per topic to avoid DB queries on every flush
// This is inspired by NATS/NSQ routing table caching pattern
var subscriberCache sync.Map // topicId -> cachedSubscribers
type cachedSubscribers struct {
ids []uuid.UUID
expiresAt time.Time
}
const subscriberCacheTTL = 5 * time.Second
// getSubscribersForTopicCached returns subscriber IDs with caching
func (d *db) getSubscribersForTopicCached(ctx context.Context, topicId uuid.UUID) ([]uuid.UUID, error) {
// Check cache first
if cached, ok := subscriberCache.Load(topicId); ok {
cs := cached.(cachedSubscribers)
if time.Now().Before(cs.expiresAt) {
return cs.ids, nil
}
}
// Cache miss or expired - fetch from DB
ids, err := d.getSubscribersForTopic(ctx, topicId)
if err != nil {
return nil, err
}
// Store in cache
subscriberCache.Store(topicId, cachedSubscribers{
ids: ids,
expiresAt: time.Now().Add(subscriberCacheTTL),
})
return ids, nil
}
// InvalidateSubscriberCache removes a topic from cache (call when subscribers change)
func InvalidateSubscriberCache(topicId uuid.UUID) {
subscriberCache.Delete(topicId)
}
// getSubscribersForTopic returns subscriber IDs for a topic (uncached)
func (d *db) getSubscribersForTopic(ctx context.Context, topicId uuid.UUID) ([]uuid.UUID, error) {
var ids []uuid.UUID
query := "SELECT id FROM topic_subscribers WHERE topic_id = ? AND deleted_at IS NULL"
err := d.Database.Conn().SelectContext(ctx, &ids,
d.Database.Conn().Rebind(query),
topicId,
)
return ids, err
}