2
0

punchy.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package nebula
  2. import (
  3. "sync/atomic"
  4. "time"
  5. "github.com/sirupsen/logrus"
  6. "github.com/slackhq/nebula/config"
  7. )
  8. type Punchy struct {
  9. punch atomic.Bool
  10. respond atomic.Bool
  11. delay atomic.Int64
  12. l *logrus.Logger
  13. }
  14. func NewPunchyFromConfig(l *logrus.Logger, c *config.C) *Punchy {
  15. p := &Punchy{l: l}
  16. p.reload(c, true)
  17. c.RegisterReloadCallback(func(c *config.C) {
  18. p.reload(c, false)
  19. })
  20. return p
  21. }
  22. func (p *Punchy) reload(c *config.C, initial bool) {
  23. if initial {
  24. var yes bool
  25. if c.IsSet("punchy.punch") {
  26. yes = c.GetBool("punchy.punch", false)
  27. } else {
  28. // Deprecated fallback
  29. yes = c.GetBool("punchy", false)
  30. }
  31. p.punch.Store(yes)
  32. } else if c.HasChanged("punchy.punch") || c.HasChanged("punchy") {
  33. //TODO: it should be relatively easy to support this, just need to be able to cancel the goroutine and boot it up from here
  34. p.l.Warn("Changing punchy.punch with reload is not supported, ignoring.")
  35. }
  36. if initial || c.HasChanged("punchy.respond") || c.HasChanged("punch_back") {
  37. var yes bool
  38. if c.IsSet("punchy.respond") {
  39. yes = c.GetBool("punchy.respond", false)
  40. } else {
  41. // Deprecated fallback
  42. yes = c.GetBool("punch_back", false)
  43. }
  44. p.respond.Store(yes)
  45. if !initial {
  46. p.l.Infof("punchy.respond changed to %v", p.GetRespond())
  47. }
  48. }
  49. //NOTE: this will not apply to any in progress operations, only the next one
  50. if initial || c.HasChanged("punchy.delay") {
  51. p.delay.Store((int64)(c.GetDuration("punchy.delay", time.Second)))
  52. if !initial {
  53. p.l.Infof("punchy.delay changed to %s", p.GetDelay())
  54. }
  55. }
  56. }
  57. func (p *Punchy) GetPunch() bool {
  58. return p.punch.Load()
  59. }
  60. func (p *Punchy) GetRespond() bool {
  61. return p.respond.Load()
  62. }
  63. func (p *Punchy) GetDelay() time.Duration {
  64. return (time.Duration)(p.delay.Load())
  65. }