@@ -23,7 +23,7 @@ func (b *BinlogSyncer) StartBackup(backupDir string, p Position, timeout time.Du
2323 return os .OpenFile (path .Join (backupDir , filename ), os .O_CREATE | os .O_WRONLY , 0644 )
2424 })
2525 } else {
26- return b .StartSynchronousBackup (p )
26+ return b .StartSynchronousBackup (p , timeout )
2727 }
2828}
2929
@@ -89,7 +89,7 @@ func (b *BinlogSyncer) StartBackupWithHandler(p Position, timeout time.Duration,
8989}
9090
9191// StartSynchronousBackup starts the backup process using the SynchronousEventHandler in the BinlogSyncerConfig.
92- func (b * BinlogSyncer ) StartSynchronousBackup (p Position ) error {
92+ func (b * BinlogSyncer ) StartSynchronousBackup (p Position , timeout time. Duration ) error {
9393 if b .cfg .SynchronousEventHandler == nil {
9494 return errors .New ("SynchronousEventHandler must be set in BinlogSyncerConfig to use StartSynchronousBackup" )
9595 }
@@ -99,10 +99,25 @@ func (b *BinlogSyncer) StartSynchronousBackup(p Position) error {
9999 return errors .Trace (err )
100100 }
101101
102+ var ctx context.Context
103+ var cancel context.CancelFunc
104+
105+ if timeout > 0 {
106+ ctx , cancel = context .WithTimeout (context .Background (), timeout )
107+ defer cancel ()
108+ } else {
109+ ctx = context .Background ()
110+ }
111+
102112 select {
113+ case <- ctx .Done ():
114+ // The timeout has been reached
115+ return nil
103116 case <- b .ctx .Done ():
117+ // The BinlogSyncer has been closed
104118 return nil
105119 case err := <- s .ech :
120+ // An error occurred during streaming
106121 return errors .Trace (err )
107122 }
108123}
0 commit comments