Skip to content

Commit a8aa17f

Browse files
committed
canal: remove no need context now
1 parent 1a46a5f commit a8aa17f

File tree

5 files changed

+17
-32
lines changed

5 files changed

+17
-32
lines changed

canal/canal_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/ngaut/log"
1111
. "github.com/pingcap/check"
1212
"github.com/siddontang/go-mysql/mysql"
13-
"golang.org/x/net/context"
1413
)
1514

1615
var testHost = flag.String("host", "127.0.0.1", "MySQL host")
@@ -74,7 +73,7 @@ type testEventHandler struct {
7473
DummyEventHandler
7574
}
7675

77-
func (h *testEventHandler) Do(_ context.Context, e *RowsEvent) error {
76+
func (h *testEventHandler) Do(e *RowsEvent) error {
7877
log.Infof("%s %v\n", e.Action, e.Rows)
7978
return nil
8079
}

canal/dump.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (h *dumpParseHandler) Data(db string, table string, values []string) error
6464
}
6565

6666
events := newRowsEvent(tableInfo, InsertAction, [][]interface{}{vs})
67-
return h.c.eventHandler.OnRow(h.c.ctx, events)
67+
return h.c.eventHandler.OnRow(events)
6868
}
6969

7070
func (c *Canal) AddDumpDatabases(dbs ...string) {

canal/handler.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,26 @@ package canal
33
import (
44
"github.com/siddontang/go-mysql/mysql"
55
"github.com/siddontang/go-mysql/replication"
6-
"golang.org/x/net/context"
76
)
87

98
type EventHandler interface {
10-
OnRotate(ctx context.Context, roateEvent *replication.RotateEvent) error
11-
OnDDL(ctx context.Context, nextPos mysql.Position, queryEvent *replication.QueryEvent) error
12-
OnRow(ctx context.Context, e *RowsEvent) error
13-
OnXID(ctx context.Context, nextPos mysql.Position) error
9+
OnRotate(roateEvent *replication.RotateEvent) error
10+
OnDDL(nextPos mysql.Position, queryEvent *replication.QueryEvent) error
11+
OnRow(e *RowsEvent) error
12+
OnXID(nextPos mysql.Position) error
1413
String() string
1514
}
1615

1716
type DummyEventHandler struct {
1817
}
1918

20-
func (h *DummyEventHandler) OnRotate(context.Context, *replication.RotateEvent) error { return nil }
21-
func (h *DummyEventHandler) OnDDL(context.Context, mysql.Position, *replication.QueryEvent) error {
19+
func (h *DummyEventHandler) OnRotate(*replication.RotateEvent) error { return nil }
20+
func (h *DummyEventHandler) OnDDL(mysql.Position, *replication.QueryEvent) error {
2221
return nil
2322
}
24-
func (h *DummyEventHandler) OnRow(context.Context, *RowsEvent) error { return nil }
25-
func (h *DummyEventHandler) OnXID(context.Context, mysql.Position) error { return nil }
26-
func (h *DummyEventHandler) String() string { return "DummyEventHandler" }
23+
func (h *DummyEventHandler) OnRow(*RowsEvent) error { return nil }
24+
func (h *DummyEventHandler) OnXID(mysql.Position) error { return nil }
25+
func (h *DummyEventHandler) String() string { return "DummyEventHandler" }
2726

2827
// `SetEventHandler` registers the sync handler, you must register your
2928
// own handler before starting Canal.

canal/sync.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"regexp"
55
"time"
66

7-
"golang.org/x/net/context"
8-
97
"github.com/juju/errors"
108
"github.com/ngaut/log"
119
"github.com/siddontang/go-mysql/mysql"
@@ -27,23 +25,13 @@ func (c *Canal) startSyncBinlog() error {
2725
return errors.Errorf("start sync replication at %v error %v", pos, err)
2826
}
2927

30-
timeout := time.Second
3128
for {
32-
ctx, cancel := context.WithTimeout(c.ctx, 2*time.Second)
33-
ev, err := s.GetEvent(ctx)
34-
cancel()
35-
36-
if err == context.DeadlineExceeded {
37-
timeout = 2 * timeout
38-
continue
39-
}
29+
ev, err := s.GetEvent(c.ctx)
4030

4131
if err != nil {
4232
return errors.Trace(err)
4333
}
4434

45-
timeout = time.Second
46-
4735
curPos := pos.Pos
4836
//next binlog pos
4937
pos.Pos = ev.Header.LogPos
@@ -58,7 +46,7 @@ func (c *Canal) startSyncBinlog() error {
5846
pos.Pos = uint32(e.Position)
5947
log.Infof("rotate binlog to %s", pos)
6048

61-
if err = c.eventHandler.OnRotate(c.ctx, e); err != nil {
49+
if err = c.eventHandler.OnRotate(e); err != nil {
6250
return errors.Trace(err)
6351
}
6452
case *replication.RowsEvent:
@@ -72,7 +60,7 @@ func (c *Canal) startSyncBinlog() error {
7260
continue
7361
case *replication.XIDEvent:
7462
// try to save the position later
75-
if err := c.eventHandler.OnXID(c.ctx, pos); err != nil {
63+
if err := c.eventHandler.OnXID(pos); err != nil {
7664
return errors.Trace(err)
7765
}
7866
case *replication.QueryEvent:
@@ -83,7 +71,7 @@ func (c *Canal) startSyncBinlog() error {
8371
}
8472
c.ClearTableCache(mb[1], mb[2])
8573
log.Infof("table structure changed, clear table cache: %s.%s\n", mb[1], mb[2])
86-
if err = c.eventHandler.OnDDL(c.ctx, pos, e); err != nil {
74+
if err = c.eventHandler.OnDDL(pos, e); err != nil {
8775
return errors.Trace(err)
8876
}
8977
} else {
@@ -123,7 +111,7 @@ func (c *Canal) handleRowsEvent(e *replication.BinlogEvent) error {
123111
return errors.Errorf("%s not supported now", e.Header.EventType)
124112
}
125113
events := newRowsEvent(t, action, ev.Rows)
126-
return c.eventHandler.OnRow(c.ctx, events)
114+
return c.eventHandler.OnRow(events)
127115
}
128116

129117
func (c *Canal) WaitUntilPos(pos mysql.Position, timeout time.Duration) error {

cmd/go-canal/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
"github.com/siddontang/go-mysql/canal"
1212
"github.com/siddontang/go-mysql/mysql"
13-
"golang.org/x/net/context"
1413
)
1514

1615
var host = flag.String("host", "127.0.0.1", "MySQL host")
@@ -98,7 +97,7 @@ type handler struct {
9897
canal.DummyEventHandler
9998
}
10099

101-
func (h *handler) OnRow(_ context.Context, e *canal.RowsEvent) error {
100+
func (h *handler) OnRow(e *canal.RowsEvent) error {
102101
fmt.Printf("%v\n", e)
103102

104103
return nil

0 commit comments

Comments
 (0)