diff --git a/synced-pool.go b/synced-pool.go index e7611bd..037cef9 100644 --- a/synced-pool.go +++ b/synced-pool.go @@ -369,18 +369,19 @@ func (p *SyncedPool) removeLocalGrain(id CartId) { } func (p *SyncedPool) AddRemote(host string) { - + p.mu.Lock() + defer p.mu.Unlock() _, hasHost := p.remotes[host] if host == "" || hasHost || host == p.Hostname { return } client := NewConnection(fmt.Sprintf("%s:1338", host)) - var r *FrameWithPayload + var err error pings := 3 for pings >= 0 { - r, err = client.Call(Ping, nil) + _, err = client.Call(Ping, nil) if err != nil { log.Printf("Ping failed when adding %s, trying %d more times\n", host, pings) pings-- @@ -389,39 +390,37 @@ func (p *SyncedPool) AddRemote(host string) { } break } - log.Printf("Connected to remote %s: %v\n", host, r) + log.Printf("Connected to remote %s", host) remote := RemoteHost{ Connection: client, MissedPings: 0, Host: host, } - p.mu.Lock() + p.remotes[host] = &remote - p.mu.Unlock() - - go func() { - for range time.Tick(time.Second * 3) { - - err := remote.Ping() - - for err != nil { - time.Sleep(time.Millisecond * 200) - if !remote.IsHealthy() { - log.Printf("Removing host, unable to communicate with %s", host) - p.RemoveHost(&remote) - return - } - err = remote.Ping() - } - } - }() connectedRemotes.Set(float64(len(p.remotes))) - log.Printf("Added remote %s\n", remote.Host) + go p.HandlePing(&remote) go remote.Initialize(p) - return +} + +func (p *SyncedPool) HandlePing(remote *RemoteHost) { + for range time.Tick(time.Second * 3) { + + err := remote.Ping() + + for err != nil { + time.Sleep(time.Millisecond * 200) + if !remote.IsHealthy() { + log.Printf("Removing host, unable to communicate with %s", remote.Host) + p.RemoveHost(remote) + return + } + err = remote.Ping() + } + } } func (p *SyncedPool) getGrain(id CartId) (Grain, error) {