Line data Source code
1 : // Copyright 2024 The LevelDB-Go and Pebble Authors. All rights reserved. Use 2 : // of this source code is governed by a BSD-style license that can be found in 3 : // the LICENSE file. 4 : 5 : package base 6 : 7 : import "io" 8 : 9 : // CloseHelper wraps an io.Closer in a wrapper that ignores extra calls to 10 : // Close. It is useful to ensure cleanup in error paths (using defer) without 11 : // double-closing. 12 1 : func CloseHelper(closer io.Closer) io.Closer { 13 1 : return &closeHelper{ 14 1 : Closer: closer, 15 1 : } 16 1 : } 17 : 18 : type closeHelper struct { 19 : Closer io.Closer 20 : } 21 : 22 : // Close the underlying Closer, unless it was already closed. 23 1 : func (h *closeHelper) Close() error { 24 1 : closer := h.Closer 25 1 : if closer == nil { 26 1 : return nil 27 1 : } 28 1 : h.Closer = nil 29 1 : return closer.Close() 30 : }