github.com/m3db/m3@v1.5.0/src/x/resource/types.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 resource describes require for object lifecycle management.
    22  // Both Finalizer and Closer have similar concepts, they both exist so that
    23  // different types can be used for resource cleanup with different method names
    24  // as for some things like iterators, the verb close makes more sense than
    25  // finalize and is more consistent with other types.
    26  package resource
    27  
    28  // Finalizer finalizes a checked resource.
    29  type Finalizer interface {
    30  	Finalize()
    31  }
    32  
    33  // FinalizerFn is a function literal that is a finalizer.
    34  type FinalizerFn func()
    35  
    36  // Finalize will call the function literal as a finalizer.
    37  func (fn FinalizerFn) Finalize() {
    38  	fn()
    39  }
    40  
    41  // SimpleCloser is an object that can be closed.
    42  type SimpleCloser interface {
    43  	Close()
    44  }
    45  
    46  // SimpleCloserFn is a function literal that is a closer.
    47  type SimpleCloserFn func()
    48  
    49  // Close will call the function literal as a closer.
    50  func (fn SimpleCloserFn) Close() {
    51  	fn()
    52  }
    53  
    54  // Closer is an object that can be closed which returns an error.
    55  type Closer interface {
    56  	Close() error
    57  }
    58  
    59  // CloserFn is a function literal that is a closer which returns an error.
    60  type CloserFn func() error
    61  
    62  // Close will call the function literal as a closer.
    63  func (fn CloserFn) Close() error {
    64  	return fn()
    65  }
    66  
    67  // NoopCloser is a no-op closer.
    68  type NoopCloser struct {
    69  	// Calls is the number of times this closer was called.
    70  	Calls int
    71  }
    72  
    73  // Close closes the no-op closer.
    74  func (c *NoopCloser) Close() { c.Calls++ }