diff --git a/synced-pool.go b/synced-pool.go index c29b91e..8594377 100644 --- a/synced-pool.go +++ b/synced-pool.go @@ -74,8 +74,16 @@ func (p *SyncedPool) PongHandler(data []byte) (uint32, []byte, error) { func (p *SyncedPool) GetCartIdHandler(data []byte) (uint32, []byte, error) { ids := make([]string, 0, len(p.local.grains)) for id := range p.local.grains { - ids = append(ids, id.String()) + if p.local.grains[id] == nil { + continue + } + s := id.String() + if s == "" { + continue + } + ids = append(ids, s) } + log.Printf("Returning %d cart ids\n", len(ids)) return CartIdsResponse, []byte(strings.Join(ids, ";")), nil } @@ -83,7 +91,9 @@ 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), ";")) { - go p.AddRemote(host) + if !p.IsKnown(host) { + go p.AddRemote(host) + } } return RemoteNegotiateResponse, []byte("ok"), nil @@ -118,7 +128,9 @@ func (p *SyncedPool) GrainOwnerChangeHandler(data []byte) (uint32, []byte, error return AckChange, []byte("ok"), nil } } - return AckChange, []byte{}, fmt.Errorf("remote host not found") + + go p.AddRemote(host) + return AckChange, []byte("ok"), nil } func NewSyncedPool(local *GrainLocalPool, hostname string, discovery Discovery) (*SyncedPool, error) { @@ -264,6 +276,7 @@ func (g *RemoteHost) GetCartMappings() ([]CartId, error) { return nil, err } if reply.StatusCode != 200 { + log.Printf("Remote returned error on get cart mappings: %s", string(reply.Data)) return nil, fmt.Errorf("remote returned error: %s", string(reply.Data)) } parts := strings.Split(string(reply.Data), ";")