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

     1  // Copyright (c) 2016 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 instrument
    22  
    23  import (
    24  	"errors"
    25  	"sync"
    26  	"time"
    27  )
    28  
    29  type reporterState int
    30  
    31  const (
    32  	reporterStateNotStarted reporterState = iota
    33  	reporterStateStarted
    34  	reporterStateStopped
    35  )
    36  
    37  var (
    38  	errReporterAlreadyStartedOrStopped = errors.New("reporter already started or stopped")
    39  	errReporterNotRunning              = errors.New("reporter not running")
    40  	errReporterReportIntervalInvalid   = errors.New("reporter report interval is invalid")
    41  )
    42  
    43  type baseReporter struct {
    44  	sync.Mutex
    45  
    46  	state          reporterState
    47  	reportInterval time.Duration
    48  	closeCh        chan struct{}
    49  	doneCh         chan struct{}
    50  
    51  	// fn is the only field required to set
    52  	fn func()
    53  }
    54  
    55  func (r *baseReporter) init(
    56  	reportInterval time.Duration,
    57  	fn func(),
    58  ) {
    59  	r.reportInterval = reportInterval
    60  	r.closeCh = make(chan struct{})
    61  	r.doneCh = make(chan struct{})
    62  	r.fn = fn
    63  }
    64  
    65  func (r *baseReporter) Start() error {
    66  	r.Lock()
    67  	defer r.Unlock()
    68  
    69  	if r.state != reporterStateNotStarted {
    70  		return errReporterAlreadyStartedOrStopped
    71  	}
    72  
    73  	if r.reportInterval <= 0 {
    74  		return errReporterReportIntervalInvalid
    75  	}
    76  
    77  	r.state = reporterStateStarted
    78  
    79  	go func() {
    80  		ticker := time.NewTicker(r.reportInterval)
    81  		defer func() {
    82  			ticker.Stop()
    83  			r.doneCh <- struct{}{}
    84  		}()
    85  
    86  		for {
    87  			select {
    88  			case <-ticker.C:
    89  				r.fn()
    90  			case <-r.closeCh:
    91  				return
    92  			}
    93  		}
    94  	}()
    95  
    96  	return nil
    97  }
    98  
    99  func (r *baseReporter) Stop() error {
   100  	r.Lock()
   101  	defer r.Unlock()
   102  
   103  	if r.state != reporterStateStarted {
   104  		return errReporterNotRunning
   105  	}
   106  
   107  	r.state = reporterStateStopped
   108  	close(r.closeCh)
   109  	<-r.doneCh
   110  
   111  	return nil
   112  }