Add context-aware versions of WaitUntilPos() and CatchMasterPos()#1168
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces context-aware versions of the wait and catch master position functions, namely WaitUntilPosContext and CatchMasterPosContext, to support cancellation and timeouts via context.Context. The feedback suggests stopping the newly created timer in WaitUntilPosContext using defer timer.Stop() to prevent resource leaks, and checking if the context is already canceled in CatchMasterPosContext before calling GetMasterPos().
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| func (c *Canal) WaitUntilPosContext(ctx context.Context, pos mysql.Position, timeout time.Duration) error { | ||
| timer := time.NewTimer(timeout) | ||
| for { |
There was a problem hiding this comment.
The time.NewTimer is created but never stopped. In Go, timers should be stopped using defer timer.Stop() to release resources and prevent potential memory leaks, especially if the function returns early.
func (c *Canal) WaitUntilPosContext(ctx context.Context, pos mysql.Position, timeout time.Duration) error {
timer := time.NewTimer(timeout)
defer timer.Stop()
for {| func (c *Canal) CatchMasterPosContext(ctx context.Context, timeout time.Duration) error { | ||
| pos, err := c.GetMasterPos() |
There was a problem hiding this comment.
It is a good practice to check if the context is already canceled or expired before performing a potentially expensive network operation like GetMasterPos().
| func (c *Canal) CatchMasterPosContext(ctx context.Context, timeout time.Duration) error { | |
| pos, err := c.GetMasterPos() | |
| func (c *Canal) CatchMasterPosContext(ctx context.Context, timeout time.Duration) error { | |
| if err := ctx.Err(); err != nil { | |
| return err | |
| } | |
| pos, err := c.GetMasterPos() |
WaitUntilPos()is a blocking call, so there should be a way to break out of it before it times out on its own. The patch adds new context-aware functions keeping the interface backwards compatible.