Skip to content

Conversation

@olavloite
Copy link
Collaborator

@olavloite olavloite commented Oct 23, 2025

Add a statement_timeout connection property that is used as the default timeout
for all statements that are executed on a connection. If both a context timeout
and a statement_timeout is specified, then the lower of the two will be used.

Also adds a transaction_timeout property that is additionally used for all
statements in a read/write transaction. The deadline of the transaction is
calculated at the start of the transaction, and all statements in the transaction
get this deadline, unless the statement already has an earlier deadline from
for example a statement_timeout or a context deadline.

Fixes #574
Fixes #575

@olavloite olavloite requested a review from a team as a code owner October 23, 2025 15:59
@olavloite olavloite force-pushed the statement-timeout branch 2 times, most recently from 80dbaa0 to 0d69f22 Compare October 24, 2025 14:00
@olavloite olavloite changed the title feat: support statement_timeout property feat: support statement_timeout and transaction_timeout property Oct 24, 2025
@olavloite olavloite force-pushed the statement-timeout branch 8 times, most recently from 1791559 to 4667c42 Compare October 27, 2025 13:30
@olavloite olavloite force-pushed the statement-timeout branch 2 times, most recently from 1b7b5f0 to 1fc9908 Compare October 27, 2025 13:42
Add a statement_timeout connection property that is used as the default timeout
for the execution of all statements that are executed on a connection. The
timeout is only used for the actual execution, and not attached to the iterator
that is returned for a query. This also means that a query that is executed
without the DirectExecuteQuery option, will ignore the statement_timeout value.

Also adds a transaction_timeout property that is additionally used for all
statements in a read/write transaction. The deadline of the transaction is
calculated at the start of the transaction, and all statements in the transaction
get this deadline, unless the statement already has an earlier deadline from
for example a statement_timeout or a context deadline.

This change also fixes some issues with deadlines when using the gRPC API of
SpannerLib. The context that is used for an RPC invocation is cancelled after
the RPC has finished. This context should therefore not be used as the context
for any query execution, as the context is attached to the row iterator, and
would cancel the query execution halfway.

Fixes #574
Fixes #575
conn.go Outdated
// one error. This will preserve the DeadlineExceeded error code from statementErr, and include the request
// ID from the Spanner error.
s := status.FromContextError(statementErr)
return fmt.Errorf("%w: %w", s.Err(), res.dirtyErr)
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you check this... fmt.Errorf allows at most one %w verb. Using two %w will cause a runtime panic: "fmt: multiple %w verbs in format".

return fmt.Errorf("%v: %w", s.Err(), res.dirtyErr)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hmmm... Interesting point. This was actually changed in Go 1.20: https://go.dev/doc/go1.20#errors

So anyone using the correct minimum version of Go should not run into that problem. But a safer way to solve this might be to just use the errors.Join function, as that would then give a compile-time error instead of a runtime panic.

@olavloite olavloite requested a review from bhatt4982 October 31, 2025 09:59
Copy link
Contributor

@bhatt4982 bhatt4982 left a comment

Choose a reason for hiding this comment

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

LGTM...

@olavloite olavloite merged commit b542646 into main Oct 31, 2025
31 checks passed
@olavloite olavloite deleted the statement-timeout branch October 31, 2025 10:10
aakashanandg pushed a commit to aakashanandg/go-sql-spanner that referenced this pull request Oct 31, 2025
…gleapis#578)

* feat: support statement_timeout and transaction_timeout property

Add a statement_timeout connection property that is used as the default timeout
for the execution of all statements that are executed on a connection. The
timeout is only used for the actual execution, and not attached to the iterator
that is returned for a query. This also means that a query that is executed
without the DirectExecuteQuery option, will ignore the statement_timeout value.

Also adds a transaction_timeout property that is additionally used for all
statements in a read/write transaction. The deadline of the transaction is
calculated at the start of the transaction, and all statements in the transaction
get this deadline, unless the statement already has an earlier deadline from
for example a statement_timeout or a context deadline.

This change also fixes some issues with deadlines when using the gRPC API of
SpannerLib. The context that is used for an RPC invocation is cancelled after
the RPC has finished. This context should therefore not be used as the context
for any query execution, as the context is attached to the row iterator, and
would cancel the query execution halfway.

Fixes googleapis#574
Fixes googleapis#575

* chore: use errors.Join instead of two %w verbs
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.

Add support for transaction_timeout connection variable Support statement_timeout connection variable

3 participants