github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/x/resource/lifetime.go (about)

     1  // Copyright (c) 2019 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
    22  
    23  import "sync"
    24  
    25  // CancellableLifetime describes a lifetime for a resource that
    26  // allows checking out the resource and returning it and once
    27  // cancelled will not allow any further checkouts.
    28  type CancellableLifetime struct {
    29  	mu        sync.RWMutex
    30  	cancelled bool
    31  }
    32  
    33  // NewCancellableLifetime returns a new cancellable resource lifetime.
    34  func NewCancellableLifetime() *CancellableLifetime {
    35  	return &CancellableLifetime{}
    36  }
    37  
    38  // TryCheckout will try to checkout the resource, if the lifetime
    39  // is already cancelled this will return false, otherwise it will return
    40  // true and guarantee the lifetime is not cancelled until the checkout
    41  // is returned.
    42  // If this returns true you MUST call ReleaseCheckout later, otherwise
    43  // the lifetime will never close and any caller calling Cancel will be
    44  // blocked indefinitely.
    45  func (l *CancellableLifetime) TryCheckout() bool {
    46  	l.mu.RLock()
    47  	if l.cancelled {
    48  		// Already cancelled, close the RLock don't need to keep it open
    49  		l.mu.RUnlock()
    50  		return false
    51  	}
    52  
    53  	// Keep the RLock open
    54  	return true
    55  }
    56  
    57  // ReleaseCheckout will decrement the number of current checkouts, it MUST
    58  // only be called after a call to TryCheckout and must not be called more
    59  // than once per call to TryCheckout or else it will panic as it will try
    60  // to unlock an unlocked resource.
    61  func (l *CancellableLifetime) ReleaseCheckout() {
    62  	l.mu.RUnlock()
    63  }
    64  
    65  // Cancel will wait for all current checkouts to be returned
    66  // and then will cancel the lifetime so that it cannot be
    67  // checked out any longer.
    68  func (l *CancellableLifetime) Cancel() {
    69  	l.mu.Lock()
    70  	l.cancelled = true
    71  	l.mu.Unlock()
    72  }