2017-03-15 23:11:19 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2017-07-06 10:40:54 +00:00
|
|
|
"github.com/hashicorp/consul/agent/router"
|
2017-03-15 23:11:19 +00:00
|
|
|
"github.com/hashicorp/serf/serf"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FloodNotify lets all the waiting Flood goroutines know that some change may
|
|
|
|
// have affected them.
|
|
|
|
func (s *Server) FloodNotify() {
|
|
|
|
s.floodLock.RLock()
|
|
|
|
defer s.floodLock.RUnlock()
|
|
|
|
|
|
|
|
for _, ch := range s.floodCh {
|
|
|
|
select {
|
|
|
|
case ch <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flood is a long-running goroutine that floods servers from the LAN to the
|
|
|
|
// given global Serf instance, such as the WAN. This will exit once either of
|
|
|
|
// the Serf instances are shut down.
|
2020-04-27 08:21:05 +00:00
|
|
|
func (s *Server) Flood(addrFn router.FloodAddrFn, dstSerf *serf.Serf) {
|
2017-03-15 23:11:19 +00:00
|
|
|
s.floodLock.Lock()
|
|
|
|
floodCh := make(chan struct{})
|
|
|
|
s.floodCh = append(s.floodCh, floodCh)
|
|
|
|
s.floodLock.Unlock()
|
|
|
|
|
|
|
|
ticker := time.NewTicker(s.config.SerfFloodInterval)
|
|
|
|
defer ticker.Stop()
|
|
|
|
defer func() {
|
|
|
|
s.floodLock.Lock()
|
|
|
|
defer s.floodLock.Unlock()
|
|
|
|
|
|
|
|
for i, ch := range s.floodCh {
|
|
|
|
if ch == floodCh {
|
|
|
|
s.floodCh = append(s.floodCh[:i], s.floodCh[i+1:]...)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic("flood channels out of sync")
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-s.serfLAN.ShutdownCh():
|
|
|
|
return
|
|
|
|
|
2020-04-27 07:53:40 +00:00
|
|
|
case <-dstSerf.ShutdownCh():
|
2017-03-15 23:11:19 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
case <-ticker.C:
|
2020-04-27 08:21:05 +00:00
|
|
|
router.FloodJoins(s.logger, addrFn, s.config.Datacenter, s.serfLAN, dstSerf)
|
2017-03-15 23:11:19 +00:00
|
|
|
|
|
|
|
case <-floodCh:
|
2020-04-27 08:21:05 +00:00
|
|
|
router.FloodJoins(s.logger, addrFn, s.config.Datacenter, s.serfLAN, dstSerf)
|
2017-03-15 23:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|