From f89130d5f9fdf58ea62075f14cdef9825c6c411b Mon Sep 17 00:00:00 2001 From: matst80 Date: Sat, 9 Nov 2024 23:20:22 +0100 Subject: [PATCH] sync ids on add --- synced-pool.go | 45 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/synced-pool.go b/synced-pool.go index ce6aa19..d1bb003 100644 --- a/synced-pool.go +++ b/synced-pool.go @@ -164,8 +164,10 @@ const ( RemoteGrainChanged = uint16(4) AckChange = uint16(5) //AckError = uint16(6) - Ping = uint16(7) - Pong = uint16(8) + Ping = uint16(7) + Pong = uint16(8) + GetCartIds = uint16(9) + CartIdsResponse = uint16(10) ) type PacketWithData struct { @@ -317,6 +319,15 @@ func (p *SyncedPool) handleConnection(conn net.Conn) { }) } + case GetCartIds: + ids := make([]string, 0, len(p.local.grains)) + for id := range p.local.grains { + ids = append(ids, id.String()) + } + SendPacket(conn, CartIdsResponse, func(w io.Writer) error { + _, err := w.Write([]byte(strings.Join(ids, ";"))) + return err + }) } } } @@ -338,6 +349,27 @@ func (h *RemoteHost) Negotiate(knownHosts []string) ([]string, error) { return strings.Split(string(packet.Data), ";"), nil } +func (g *RemoteHost) GetCartMappings() []CartId { + err := SendPacket(g.connection, GetCartIds, func(w io.Writer) error { + return nil + }) + if err != nil { + log.Printf("Error getting mappings: %v\n", err) + return nil + } + packet, err := g.queue.Expect(CartIdsResponse, time.Second*3) + if err != nil { + log.Printf("Error getting mappings: %v\n", err) + return nil + } + parts := strings.Split(string(packet.Data), ";") + ids := make([]CartId, 0, len(parts)) + for _, p := range parts { + ids = append(ids, ToCartId(p)) + } + return ids +} + func (p *SyncedPool) Negotiate(knownHosts []string) ([]string, error) { allHosts := make(map[string]struct{}, 0) for _, r := range p.remotes { @@ -425,7 +457,14 @@ 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() { + ids := remote.GetCartMappings() + p.mu.Lock() + for _, id := range ids { + p.remoteIndex[id] = remote.Pool + } + p.mu.Unlock() + }() return nil }