-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbinlogstreamer.go
More file actions
72 lines (59 loc) · 1.32 KB
/
binlogstreamer.go
File metadata and controls
72 lines (59 loc) · 1.32 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
package replication
import (
"time"
"github.com/juju/errors"
)
var (
ErrGetEventTimeout = errors.New("Get event timeout, try get later")
ErrNeedSyncAgain = errors.New("Last sync error or closed, try sync and get event again")
ErrSyncClosed = errors.New("Sync was closed")
)
type BinlogStreamer struct {
ch chan *BinlogEvent
ech chan error
err error
}
func (s *BinlogStreamer) GetEvent() (*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
}
}
// if timeout, ErrGetEventTimeout will returns
// timeout value won't be set too large, otherwise it may waste lots of memory
func (s *BinlogStreamer) GetEventTimeout(d time.Duration) (*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 <-time.After(d):
return nil, ErrGetEventTimeout
}
}
func (s *BinlogStreamer) close() {
s.closeWithError(ErrSyncClosed)
}
func (s *BinlogStreamer) closeWithError(err error) {
if err == nil {
err = ErrSyncClosed
}
select {
case s.ech <- err:
default:
}
}
func newBinlogStreamer() *BinlogStreamer {
s := new(BinlogStreamer)
s.ch = make(chan *BinlogEvent, 1024)
s.ech = make(chan error, 4)
return s
}