-
Notifications
You must be signed in to change notification settings - Fork 466
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sstable: detect double Close on sstable iterators
A flag indicates if an iterator is in the pool; if we call `Close()` twice and the iterator has not been reused yet, we panic. We add a `CloseHelper` which can absorb multiple `Close()` calls, which is convenient to use with `defer`. Also update the `InternalIterator` documentation to reflect the reality that multiple `Close()` calls are not supported by all implementations.
- Loading branch information
1 parent
eb8e9db
commit 8471e8d
Showing
5 changed files
with
53 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2024 The LevelDB-Go and Pebble Authors. All rights reserved. Use | ||
// of this source code is governed by a BSD-style license that can be found in | ||
// the LICENSE file. | ||
|
||
package base | ||
|
||
import "io" | ||
|
||
// CloseHelper wraps an io.Closer in a wrapper that ignores extra calls to | ||
// Close. It is useful to ensure cleanup in error paths (using defer) without | ||
// double-closing. | ||
func CloseHelper(closer io.Closer) io.Closer { | ||
return &closeHelper{ | ||
Closer: closer, | ||
} | ||
} | ||
|
||
type closeHelper struct { | ||
Closer io.Closer | ||
} | ||
|
||
// Close the underlying Closer, unless it was already closed. | ||
func (h *closeHelper) Close() error { | ||
closer := h.Closer | ||
if closer == nil { | ||
return nil | ||
} | ||
h.Closer = nil | ||
return closer.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters