Skip to content

Commit d5dd3d6

Browse files
authored
Add EventCacheCount as member of BinlogSyncerConfig to limit streamer's event channel size (#830)
* lower memory usage by reducing event chan size in streamer When consuming events from stream is blocked, the chan may use too much memory * make streamer's event chan size configurable When the consumption speed of the event chan in streamer cannot catch up with its production speed, leading to an accumulation of events, the current fixed channel size of 10240 might occupy significant memory, potentially triggering an Out Of Memory (OOM) condition. Making the size of the event chan configurable would allow for controlling the memory usage of the streamer. * update cfg name StreamerChanSize to EventCacheSize * EventCacheSize is renamed to EventCacheCount, to make the variable name more reflective of its actual meaning.
1 parent f0f8617 commit d5dd3d6

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

replication/binlogstreamer.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,17 @@ func (s *BinlogStreamer) closeWithError(err error) {
8585
}
8686

8787
func NewBinlogStreamer() *BinlogStreamer {
88+
return NewBinlogStreamerWithChanSize(10240)
89+
}
90+
91+
func NewBinlogStreamerWithChanSize(chanSize int) *BinlogStreamer {
8892
s := new(BinlogStreamer)
8993

90-
s.ch = make(chan *BinlogEvent, 10240)
94+
if chanSize <= 0 {
95+
chanSize = 10240
96+
}
97+
98+
s.ch = make(chan *BinlogEvent, chanSize)
9199
s.ech = make(chan error, 4)
92100

93101
return s

replication/binlogsyncer.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ type BinlogSyncerConfig struct {
122122
RowsEventDecodeFunc func(*RowsEvent, []byte) error
123123

124124
DiscardGTIDSet bool
125+
126+
EventCacheCount int
125127
}
126128

127129
// BinlogSyncer syncs binlog event from server.
@@ -166,6 +168,9 @@ func NewBinlogSyncer(cfg BinlogSyncerConfig) *BinlogSyncer {
166168
dialer := &net.Dialer{}
167169
cfg.Dialer = dialer.DialContext
168170
}
171+
if cfg.EventCacheCount == 0 {
172+
cfg.EventCacheCount = 10240
173+
}
169174

170175
// Clear the Password to avoid outputing it in log.
171176
pass := cfg.Password
@@ -393,7 +398,7 @@ func (b *BinlogSyncer) prepare() error {
393398
func (b *BinlogSyncer) startDumpStream() *BinlogStreamer {
394399
b.running = true
395400

396-
s := NewBinlogStreamer()
401+
s := NewBinlogStreamerWithChanSize(b.cfg.EventCacheCount)
397402

398403
b.wg.Add(1)
399404
go b.onStream(s)

0 commit comments

Comments
 (0)