github.com/m3db/m3@v1.5.0/src/m3ninx/x/safe_closer.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package x
    22  
    23  import (
    24  	"io"
    25  
    26  	xerrors "github.com/m3db/m3/src/x/errors"
    27  )
    28  
    29  // SafeCloser is a reuesable safe closer.
    30  type SafeCloser interface {
    31  	io.Closer
    32  	Reset(closer io.Closer)
    33  }
    34  
    35  // NewSafeCloser returns a io.Closer which ensures the
    36  // underlying Close() is only called once. It's
    37  // useful for cleanup of resources in functions.
    38  func NewSafeCloser(x io.Closer) SafeCloser {
    39  	return &safeCloser{Closer: x}
    40  }
    41  
    42  // safeCloser ensures Close() is only called once. It's
    43  // useful for cleanup of resources in functions.
    44  type safeCloser struct {
    45  	io.Closer
    46  	closed bool
    47  }
    48  
    49  func (c *safeCloser) Reset(closer io.Closer) {
    50  	c.Closer = closer
    51  	c.closed = false
    52  }
    53  
    54  // Close guarantees the underlying Closable's Close() is
    55  // only executed the first time it's called.
    56  func (c *safeCloser) Close() error {
    57  	if c.closed {
    58  		return nil
    59  	}
    60  	c.closed = true
    61  	return c.Closer.Close()
    62  }
    63  
    64  // NewSafeMultiCloser returns an io.Closer which ensures the that calling Close() on it
    65  // calls Close() on each of the provided io.Closer(s) exactly once. Subsequent calls
    66  // are not executed.
    67  func NewSafeMultiCloser(closers ...io.Closer) io.Closer {
    68  	return NewSafeCloser(ioClosers(closers))
    69  }
    70  
    71  type ioClosers []io.Closer
    72  
    73  var _ io.Closer = ioClosers{}
    74  
    75  func (ioc ioClosers) Close() error {
    76  	var multiErr xerrors.MultiError
    77  	for _, c := range ioc {
    78  		multiErr = multiErr.Add(c.Close())
    79  	}
    80  	return multiErr.FinalError()
    81  }