punchy.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. respondDelay atomic.Int64
  13. l *logrus.Logger
  14. }
  15. func NewPunchyFromConfig(l *logrus.Logger, c *config.C) *Punchy {
  16. p := &Punchy{l: l}
  17. p.reload(c, true)
  18. c.RegisterReloadCallback(func(c *config.C) {
  19. p.reload(c, false)
  20. })
  21. return p
  22. }
  23. func (p *Punchy) reload(c *config.C, initial bool) {
  24. if initial {
  25. var yes bool
  26. if c.IsSet("punchy.punch") {
  27. yes = c.GetBool("punchy.punch", false)
  28. } else {
  29. // Deprecated fallback
  30. yes = c.GetBool("punchy", false)
  31. }
  32. p.punch.Store(yes)
  33. } else if c.HasChanged("punchy.punch") || c.HasChanged("punchy") {
  34. //TODO: it should be relatively easy to support this, just need to be able to cancel the goroutine and boot it up from here
  35. p.l.Warn("Changing punchy.punch with reload is not supported, ignoring.")
  36. }
  37. if initial || c.HasChanged("punchy.respond") || c.HasChanged("punch_back") {
  38. var yes bool
  39. if c.IsSet("punchy.respond") {
  40. yes = c.GetBool("punchy.respond", false)
  41. } else {
  42. // Deprecated fallback
  43. yes = c.GetBool("punch_back", false)
  44. }
  45. p.respond.Store(yes)
  46. if !initial {
  47. p.l.Infof("punchy.respond changed to %v", p.GetRespond())
  48. }
  49. }
  50. //NOTE: this will not apply to any in progress operations, only the next one
  51. if initial || c.HasChanged("punchy.delay") {
  52. p.delay.Store((int64)(c.GetDuration("punchy.delay", time.Second)))
  53. if !initial {
  54. p.l.Infof("punchy.delay changed to %s", p.GetDelay())
  55. }
  56. }
  57. if initial || c.HasChanged("punchy.respond_delay") {
  58. p.respondDelay.Store((int64)(c.GetDuration("punchy.respond_delay", 5*time.Second)))
  59. if !initial {
  60. p.l.Infof("punchy.respond_delay changed to %s", p.GetRespondDelay())
  61. }
  62. }
  63. }
  64. func (p *Punchy) GetPunch() bool {
  65. return p.punch.Load()
  66. }
  67. func (p *Punchy) GetRespond() bool {
  68. return p.respond.Load()
  69. }
  70. func (p *Punchy) GetDelay() time.Duration {
  71. return (time.Duration)(p.delay.Load())
  72. }
  73. func (p *Punchy) GetRespondDelay() time.Duration {
  74. return (time.Duration)(p.respondDelay.Load())
  75. }