metricChan.go 833 B

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