Implementing a Time-Gated Message Batcher
Owner: SnippetBot
Created: 2026-07-26 00:01:23
Size: 1.91 KB
Expires: Never
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
package main
import (
"fmt"
"time"
)
// Message represents a simple data structure
type Message struct {
ID int
Data string
}
func main() {
const (
batchSize = 3
batchDelay = 200 * time.Millisecond
)
inputCh := make(chan Message, 10) // Buffered channel for incoming messages
// Simulate a producer sending messages rapidly
go func() {
for i := 0; i < 10; i++ {
inputCh <- Message{ID: i, Data: fmt.Sprintf("Item %d", i)}
time.Sleep(50 * time.Millisecond)
}
close(inputCh)
}()
// Batch processor
go func() {
var batch []Message
timer := time.NewTimer(batchDelay)
defer timer.Stop()
for {
select {
case msg, ok := <-inputCh:
if !ok {
// Input channel closed, process any remaining batch
if len(batch) > 0 {
processBatch(batch)
}
fmt.Println("Batch processor exiting.")
return
}
batch = append(batch, msg)
// If batch size reached, process immediately
if len(batch) >= batchSize {
processBatch(batch)
batch = nil // Clear batch
timer.Reset(batchDelay) // Reset timer after processing
} else {
// Reset timer if not processing immediately, to wait for more messages or timeout
// Stop any existing timer first to avoid multiple expirations
if !timer.Stop() {
<-timer.C // Drain the channel if timer already fired
}
timer.Reset(batchDelay)
}
case <-timer.C:
// Timer fired, process whatever is in the batch
if len(batch) > 0 {
processBatch(batch)
batch = nil // Clear batch
}
timer.Reset(batchDelay) // Reset timer for next cycle
}
}
}()
time.Sleep(1 * time.Second) // Let producer and processor run
fmt.Println("Main: Program finished.")
}
func processBatch(msgs []Message) {
fmt.Printf("Processing batch of %d messages: ", len(msgs))
for _, msg := range msgs {
fmt.Printf("%d ", msg.ID)
}
fmt.Println()
time.Sleep(50 * time.Millisecond) // Simulate work
}