diff --git a/synced-pool.go b/synced-pool.go index 8594377..c3573a5 100644 --- a/synced-pool.go +++ b/synced-pool.go @@ -91,9 +91,11 @@ func (p *SyncedPool) NegotiateHandler(data []byte) (uint32, []byte, error) { negotiationCount.Inc() log.Printf("Handling negotiation\n") for _, host := range p.ExcludeKnown(strings.Split(string(data), ";")) { - if !p.IsKnown(host) { - go p.AddRemote(host) + if host == "" { + continue } + go p.AddRemote(host) + } return RemoteNegotiateResponse, []byte("ok"), nil @@ -298,6 +300,7 @@ func (p *SyncedPool) Negotiate(knownHosts []string) ([]string, error) { allHosts[h] = struct{}{} } } + ret := make([]string, 0, len(allHosts)) for h := range allHosts { ret = append(ret, h) @@ -355,7 +358,7 @@ func (p *SyncedPool) removeLocalGrain(id CartId) { } func (p *SyncedPool) addRemoteHost(address string, remote *RemoteHost) error { - known := make([]string, 0, len(p.remotes)) + known := make([]string, 0, len(p.remotes)+1) for _, r := range p.remotes { known = append(known, r.Host) @@ -364,6 +367,7 @@ func (p *SyncedPool) addRemoteHost(address string, remote *RemoteHost) error { return fmt.Errorf("remote %s already exists", address) } } + known = append([]string{p.Hostname}, known...) p.remotes = append(p.remotes, remote) connectedRemotes.Set(float64(len(p.remotes))) @@ -395,7 +399,16 @@ func (p *SyncedPool) addRemoteHost(address string, remote *RemoteHost) error { } log.Printf("Removed %d local grains, added %d remote grains\n", local, remoteNo) p.mu.Unlock() - go p.Negotiate(known) + go func() { + other, err := p.Negotiate(known) + if err != nil { + log.Printf("Error negotiating with remote %s: %v\n", remote.Host, err) + return + } + for _, o := range p.ExcludeKnown(other) { + p.AddRemote(o) + } + }() }() return nil }