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

Calling non-Close() methods on closed or written batches panic #71

Merged
merged 17 commits into from
Mar 10, 2020

Conversation

erikgrinaker
Copy link
Contributor

@erikgrinaker erikgrinaker commented Mar 10, 2020

See discussion in #66 (review).

@erikgrinaker erikgrinaker self-assigned this Mar 10, 2020
boltdb_batch.go Outdated Show resolved Hide resolved
return err
}
return nil
return b.Close()
Copy link
Contributor

Choose a reason for hiding this comment

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

do we even need a separate Close method? if we execute Close in Write/WriteSync

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, because the caller may e.g. return an error while they are building the batch, in which case they need to close it without calling Write.

Copy link
Contributor

Choose a reason for hiding this comment

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

I can easily envision cases where Close will be called twice... maybe it's okay, but I'd prefer to remove Close calls from Write/WriteSync. You're mixing responsibilities

Copy link
Contributor Author

@erikgrinaker erikgrinaker Mar 10, 2020

Choose a reason for hiding this comment

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

Calling Close() twice is fine, it is idempotent. It is a normal Go pattern to use defer c.Close() for error handling and then also do an explicit c.Close() as early as possible to free up resources before the function exits.

If we don't call Close() in Write(), then what should happen when a caller calls Write() twice?

Copy link
Contributor

Choose a reason for hiding this comment

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

It is a normal Go pattern to use defer c.Close() for error handling and then also do an explicit c.Close() as early as possible to free up resources before the function exits.

Didn't know about such.

If we don't call Close() in Write(), then what should happen when a caller calls Write() twice?

you can't prevent people from shooting themselves into the leg. I'd assume correct usage in most cases.

New -> Set/Delete -> Write(Sync) -> Close (via defer)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

you can't prevent people from shooting themselves into the leg. I'd assume correct usage in most cases.

Of course, we just have to specify what should happen, as unspecified behavior is the root of all evil. Either Write() also calls Close(), in which case a second Write() call errors, otherwise the second Write()call should write the same batch again.

I think erroring is safer, and easier to implement across all backends. The typical and recommended usage would still be the same flow that you outline.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think erroring is safer, and easier to implement across all backends.

Agree.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I think this should be good to go then.

CHANGELOG.md Outdated Show resolved Hide resolved
@erikgrinaker erikgrinaker changed the title Batch methods return errors Calling non-Close() methods on closed or written batches panic Mar 10, 2020
backend_test.go Outdated Show resolved Hide resolved
backend_test.go Outdated Show resolved Hide resolved
backend_test.go Outdated Show resolved Hide resolved
backend_test.go Outdated Show resolved Hide resolved
Copy link
Contributor

@melekes melekes left a comment

Choose a reason for hiding this comment

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

utACK 👍

boltdb_batch.go Outdated Show resolved Hide resolved
@erikgrinaker erikgrinaker merged commit bdf7336 into master Mar 10, 2020
@erikgrinaker erikgrinaker deleted the erik/batch-errors branch March 10, 2020 14:57
nddeluca referenced this pull request in Kava-Labs/tm-db Jan 8, 2024
Bumps [docker/setup-buildx-action](https://github.com/docker/setup-buildx-action) from 2.8.0 to 2.9.0.
- [Release notes](https://github.com/docker/setup-buildx-action/releases)
- [Commits](docker/setup-buildx-action@v2.8.0...v2.9.0)

---
updated-dependencies:
- dependency-name: docker/setup-buildx-action
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
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.

4 participants