From 99b26db5bd31d6da088b277d35f52f0388fcc264 Mon Sep 17 00:00:00 2001 From: Manan Gupta Date: Thu, 29 Aug 2024 21:05:48 +0530 Subject: [PATCH] feat: fix error message and code for the case when context expires before the execution starts Signed-off-by: Manan Gupta --- go/vt/vttablet/tabletserver/connpool/dbconn.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/go/vt/vttablet/tabletserver/connpool/dbconn.go b/go/vt/vttablet/tabletserver/connpool/dbconn.go index 61816b16d08..531108b2348 100644 --- a/go/vt/vttablet/tabletserver/connpool/dbconn.go +++ b/go/vt/vttablet/tabletserver/connpool/dbconn.go @@ -166,7 +166,7 @@ func (dbc *Conn) execOnce(ctx context.Context, query string, maxrows int, wantfi // Check if the context is already past its deadline before // trying to execute the query. if err := ctx.Err(); err != nil { - return nil, fmt.Errorf("%v before execution started", err) + return nil, vterrors.Errorf(vtrpcpb.Code_CANCELED, "%s, before execution started", dbc.getErrorMessageFromContextError(ctx)) } now := time.Now() @@ -200,8 +200,8 @@ func (dbc *Conn) execOnce(ctx context.Context, query string, maxrows int, wantfi } } -// terminate kills the query or connection based on the transaction status -func (dbc *Conn) terminate(ctx context.Context, insideTxn bool, now time.Time) { +// getErrorMessageFromContextError gets the error message from context error. +func (dbc *Conn) getErrorMessageFromContextError(ctx context.Context) string { var errMsg string switch { case errors.Is(ctx.Err(), context.DeadlineExceeded): @@ -211,6 +211,12 @@ func (dbc *Conn) terminate(ctx context.Context, insideTxn bool, now time.Time) { default: errMsg = ctx.Err().Error() } + return errMsg +} + +// terminate kills the query or connection based on the transaction status +func (dbc *Conn) terminate(ctx context.Context, insideTxn bool, now time.Time) { + errMsg := dbc.getErrorMessageFromContextError(ctx) if insideTxn { // we can't safely kill a query in a transaction, we need to kill the connection _ = dbc.Kill(errMsg, time.Since(now))