Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spanner: close row iterators when done #1212

Merged
merged 1 commit into from
Mar 20, 2023
Merged

Conversation

ecordell
Copy link
Contributor

@ecordell ecordell commented Mar 20, 2023

There were a couple of places where we build an iterator but then don't use the Do consumer.

Any time we don't use the Do consumer, we need to explicitly close the iterator to avoid leaking a session. In all of the places where we weren't already using Do, we were only reading one row anyway, so I just switched to Do for those cases too.

This should fix #1196, but we'll want to some testing to confirm.

From Spanner Docs:

Read returns a RowIterator. You can call the Do method on the iterator and pass a callback:

err := iter.Do(func(row *Row) error {
  // TODO: use row
  return nil
})

RowIterator also follows the standard pattern for the Google Cloud Client Libraries:

defer iter.Stop()
for {
   row, err := iter.Next()
   if err == iterator.Done {
       break
   }
   if err != nil {
       // TODO: Handle error.
   }
   // TODO: use row
}

Always call Stop when you finish using an iterator this way, whether or not you iterate to the end. (Failing to call Stop could lead you to exhaust the database's session quota.)

@github-actions github-actions bot added the area/datastore Affects the storage system label Mar 20, 2023
@ecordell ecordell marked this pull request as ready for review March 20, 2023 18:59
@ecordell ecordell requested a review from a team as a code owner March 20, 2023 18:59
vroldanbet
vroldanbet previously approved these changes Mar 20, 2023
@ecordell ecordell enabled auto-merge March 20, 2023 19:35
@ecordell ecordell merged commit 03896f6 into authzed:main Mar 20, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Mar 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area/datastore Affects the storage system
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Periodic timeouts with the Spanner datastore
2 participants