diff --git a/synced-pool.go b/synced-pool.go index 9d13da3..1374d24 100644 --- a/synced-pool.go +++ b/synced-pool.go @@ -84,10 +84,8 @@ func NewSyncedPool(local *GrainLocalPool, hostname string, d Discovery) (*Synced if err != nil { log.Printf("Error discovering hosts: %v", err) } - for _, h := range hosts { - if h == hostname { - continue - } + for _, h := range pool.ExcludeKnown(hosts) { + log.Printf("Discovered host %s", h) err := pool.AddRemote(h) @@ -115,6 +113,23 @@ func NewSyncedPool(local *GrainLocalPool, hostname string, d Discovery) (*Synced return pool, nil } +func (p *SyncedPool) ExcludeKnown(hosts []string) []string { + ret := make([]string, 0, len(hosts)) + for _, h := range hosts { + found := false + for _, r := range p.remotes { + if r.Host == h { + found = true + break + } + } + if !found && h != p.Hostname { + ret = append(ret, h) + } + } + return ret +} + func (p *SyncedPool) RemoveHost(host *RemoteHost) { for i, r := range p.remotes { @@ -432,7 +447,9 @@ func DoPing(host *RemoteHost) error { } func (p *SyncedPool) addRemoteHost(address string, remote *RemoteHost) error { + known := make([]string, 0, len(p.remotes)) for _, r := range p.remotes { + known = append(known, r.Host) if r.Host == address { log.Printf("Remote %s already exists\n", address) return fmt.Errorf("remote %s already exists", address) @@ -447,7 +464,9 @@ func (p *SyncedPool) addRemoteHost(address string, remote *RemoteHost) error { p.remotes = append(p.remotes, remote) connectedRemotes.Set(float64(len(p.remotes))) log.Printf("Added remote %s\n", remote.Host) + go func() { + p.Negotiate(known) ids := remote.GetCartMappings() p.mu.Lock() for _, id := range ids {