punchy.go 2.6 KB

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