-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathbinlogstreamer.go
123 lines (105 loc) · 2.65 KB
/
binlogstreamer.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
package replication
import (
"context"
"time"
"github.com/pingcap/errors"
"github.com/siddontang/go-log/log"
)
var (
ErrNeedSyncAgain = errors.New("Last sync error or closed, try sync and get event again")
ErrSyncClosed = errors.New("Sync was closed")
)
// BinlogStreamer gets the streaming event.
type BinlogStreamer struct {
ch chan *BinlogEvent
ech chan error
err error
}
// GetEvent gets the binlog event one by one, it will block until Syncer receives any events from MySQL
// or meets a sync error. You can pass a context (like Cancel or Timeout) to break the block.
func (s *BinlogStreamer) GetEvent(ctx context.Context) (*BinlogEvent, error) {
if s.err != nil {
return nil, ErrNeedSyncAgain
}
select {
case c := <-s.ch:
return c, nil
case s.err = <-s.ech:
return nil, s.err
case <-ctx.Done():
return nil, ctx.Err()
}
}
// GetEventWithStartTime gets the binlog event with starttime, if current binlog event timestamp smaller than specify starttime
// return nil event
func (s *BinlogStreamer) GetEventWithStartTime(ctx context.Context, startTime time.Time) (*BinlogEvent, error) {
if s.err != nil {
return nil, ErrNeedSyncAgain
}
startUnix := startTime.Unix()
select {
case c := <-s.ch:
if int64(c.Header.Timestamp) >= startUnix {
return c, nil
}
return nil, nil
case s.err = <-s.ech:
return nil, s.err
case <-ctx.Done():
return nil, ctx.Err()
}
}
// DumpEvents dumps all left events
func (s *BinlogStreamer) DumpEvents() []*BinlogEvent {
count := len(s.ch)
events := make([]*BinlogEvent, count)
for i := range events {
events[i] = <-s.ch
}
return events
}
func (s *BinlogStreamer) close() {
s.closeWithError(nil)
}
func (s *BinlogStreamer) closeWithError(err error) {
if err == nil {
err = ErrSyncClosed
} else {
log.Errorf("close sync with err: %v", err)
}
select {
case s.ech <- err:
default:
}
}
func NewBinlogStreamer() *BinlogStreamer {
return NewBinlogStreamerWithChanSize(10240)
}
func NewBinlogStreamerWithChanSize(chanSize int) *BinlogStreamer {
s := new(BinlogStreamer)
if chanSize <= 0 {
chanSize = 10240
}
s.ch = make(chan *BinlogEvent, chanSize)
s.ech = make(chan error, 4)
return s
}
// AddEventToStreamer adds a binlog event to the streamer. You can use it when you want to add an event to the streamer manually.
// can be used in replication handlers
func (s *BinlogStreamer) AddEventToStreamer(ev *BinlogEvent) error {
select {
case s.ch <- ev:
return nil
case err := <-s.ech:
return err
}
}
// AddErrorToStreamer adds an error to the streamer.
func (s *BinlogStreamer) AddErrorToStreamer(err error) bool {
select {
case s.ech <- err:
return true
default:
return false
}
}