Skip to content

Add context-aware versions of WaitUntilPos() and CatchMasterPos()#1168

Merged
lance6716 merged 2 commits into
go-mysql-org:masterfrom
dbnski:context-aware-master-pos-wait
Jul 12, 2026
Merged

Add context-aware versions of WaitUntilPos() and CatchMasterPos()#1168
lance6716 merged 2 commits into
go-mysql-org:masterfrom
dbnski:context-aware-master-pos-wait

Conversation

@dbnski

@dbnski dbnski commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread canal/sync.go
Comment on lines +311 to 313
func (c *Canal) WaitUntilPosContext(ctx context.Context, pos mysql.Position, timeout time.Duration) error {
timer := time.NewTimer(timeout)
for {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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 {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fix that.

Comment thread canal/sync.go
Comment on lines +403 to 404
func (c *Canal) CatchMasterPosContext(ctx context.Context, timeout time.Duration) error {
pos, err := c.GetMasterPos()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

It is a good practice to check if the context is already canceled or expired before performing a potentially expensive network operation like GetMasterPos().

Suggested change
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()

@lance6716 lance6716 merged commit 3d57976 into go-mysql-org:master Jul 12, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants