Skip to content

Commit

Permalink
Respond to local announces of new nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
calmh committed Feb 23, 2014
1 parent c6e396e commit 645233e
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion discover/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ type Discoverer struct {
registry map[string][]string
registryLock sync.RWMutex
extServer string

localBroadcastTick <-chan time.Time
forcedBroadcastTick chan time.Time
}

var (
Expand Down Expand Up @@ -60,6 +63,8 @@ func NewDiscoverer(id string, port int, extServer string) (*Discoverer, error) {
go disc.recvAnnouncements()

if disc.ListenPort > 0 {
disc.localBroadcastTick = time.Tick(disc.BroadcastIntv)
disc.forcedBroadcastTick = make(chan time.Time)
go disc.sendAnnouncements()
}
if len(disc.extServer) > 0 {
Expand Down Expand Up @@ -96,7 +101,11 @@ func (d *Discoverer) sendAnnouncements() {
errCounter = 0
}
}
time.Sleep(d.BroadcastIntv)

select {
case <-d.localBroadcastTick:
case <-d.forcedBroadcastTick:
}
}
log.Println("discover/write: local: stopping due to too many errors:", err)
}
Expand Down Expand Up @@ -173,6 +182,13 @@ func (d *Discoverer) recvAnnouncements() {
log.Printf("register: %#v", addrs)
}
d.registryLock.Lock()
_, seen := d.registry[pkt.NodeID]
if !seen {
fmt.Println("new node seen, forced announce")
select {
case d.forcedBroadcastTick <- time.Now():
}
}
d.registry[pkt.NodeID] = addrs
d.registryLock.Unlock()
}
Expand Down

0 comments on commit 645233e

Please sign in to comment.