Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions replication/binlogsyncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ type BinlogSyncer struct {

nextPos Position

running bool
running bool
semiSyncEnabled bool

stopCh chan struct{}
}

func NewBinlogSyncer(serverID uint32, flavor string) *BinlogSyncer {
Expand All @@ -55,6 +57,8 @@ func NewBinlogSyncer(serverID uint32, flavor string) *BinlogSyncer {
b.running = false
b.semiSyncEnabled = false

b.stopCh = make(chan struct{}, 1)

return b
}

Expand All @@ -70,6 +74,11 @@ func (b *BinlogSyncer) close() {
b.c.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
}

select {
case b.stopCh <- struct{}{}:
default:
}

b.wg.Wait()

if b.c != nil {
Expand Down Expand Up @@ -213,16 +222,17 @@ func (b *BinlogSyncer) EnableSemiSync() error {
}

_, err := b.c.Execute(`SET @rpl_semi_sync_slave = 1;`)

if err != nil {
b.semiSyncEnabled = true
}

return err
}

func (b *BinlogSyncer) startDumpStream() *BinlogStreamer {
b.running = true
b.stopCh = make(chan struct{}, 1)

s := newBinlogStreamer()

Expand Down Expand Up @@ -508,7 +518,12 @@ func (b *BinlogSyncer) parseEvent(s *BinlogStreamer, data []byte) error {
b.nextPos.Pos = uint32(re.Position)
}

s.ch <- e
needStop := false
select {
case s.ch <- e:
case <-b.stopCh:
needStop = true
}

if needACK {
err := b.replySemiSyncACK(b.nextPos)
Expand All @@ -517,5 +532,9 @@ func (b *BinlogSyncer) parseEvent(s *BinlogStreamer, data []byte) error {
}
}

if needStop {
return errors.New("sync is been closing...")
}

return nil
}