github.com/aavshr/aws-sdk-go@v1.41.3/aws/csm/metric_chan.go (about)

     1  package csm
     2  
     3  import (
     4  	"sync/atomic"
     5  )
     6  
     7  const (
     8  	runningEnum = iota
     9  	pausedEnum
    10  )
    11  
    12  var (
    13  	// MetricsChannelSize of metrics to hold in the channel
    14  	MetricsChannelSize = 100
    15  )
    16  
    17  type metricChan struct {
    18  	ch     chan metric
    19  	paused *int64
    20  }
    21  
    22  func newMetricChan(size int) metricChan {
    23  	return metricChan{
    24  		ch:     make(chan metric, size),
    25  		paused: new(int64),
    26  	}
    27  }
    28  
    29  func (ch *metricChan) Pause() {
    30  	atomic.StoreInt64(ch.paused, pausedEnum)
    31  }
    32  
    33  func (ch *metricChan) Continue() {
    34  	atomic.StoreInt64(ch.paused, runningEnum)
    35  }
    36  
    37  func (ch *metricChan) IsPaused() bool {
    38  	v := atomic.LoadInt64(ch.paused)
    39  	return v == pausedEnum
    40  }
    41  
    42  // Push will push metrics to the metric channel if the channel
    43  // is not paused
    44  func (ch *metricChan) Push(m metric) bool {
    45  	if ch.IsPaused() {
    46  		return false
    47  	}
    48  
    49  	select {
    50  	case ch.ch <- m:
    51  		return true
    52  	default:
    53  		return false
    54  	}
    55  }