Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions daemon/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import (
"sync"
"time"

. "github.com/etix/mirrorbits/config"
"github.com/etix/mirrorbits/database"
"github.com/etix/mirrorbits/mirrors"
"github.com/etix/mirrorbits/utils"
)

const (
clusterAnnounce = "HELLO"
clusterAnnouncePrefix = "HELLOMB"
)

type cluster struct {
Expand All @@ -33,6 +34,7 @@ type cluster struct {
wg sync.WaitGroup
running bool
StartStopLock sync.Mutex
announceText string
}

type node struct {
Expand All @@ -59,6 +61,7 @@ func NewCluster(r *database.Redis) *cluster {
hostname = "unknown"
}
c.nodeID = fmt.Sprintf("%s-%05d", hostname, rand.Intn(32000))
c.announceText = clusterAnnouncePrefix + fmt.Sprintf("%d", GetConfig().RedisDB)
return c
}

Expand Down Expand Up @@ -106,18 +109,18 @@ func (c *cluster) clusterLoop() {
case <-announceTicker.C:
c.announce()
case data := <-clusterChan:
if !strings.HasPrefix(data, clusterAnnounce+" ") {
if !strings.HasPrefix(data, c.announceText+" ") {
// Garbage
continue
}
c.refreshNodeList(data[len(clusterAnnounce)+1:], c.nodeID)
c.refreshNodeList(data[len(c.announceText)+1:], c.nodeID)
}
}
}

func (c *cluster) announce() {
r := c.redis.Get()
database.Publish(r, database.CLUSTER, fmt.Sprintf("%s %s", clusterAnnounce, c.nodeID))
database.Publish(r, database.CLUSTER, fmt.Sprintf("%s %s", c.announceText, c.nodeID))
r.Close()
}

Expand Down
2 changes: 1 addition & 1 deletion daemon/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestClusterLoop(t *testing.T) {

c := NewCluster(conn)

cmdPublish := mock.Command("PUBLISH", string(database.CLUSTER), fmt.Sprintf("%s %s", clusterAnnounce, c.nodeID)).Expect("1")
cmdPublish := mock.Command("PUBLISH", string(database.CLUSTER), fmt.Sprintf("%s %s", c.announceText, c.nodeID)).Expect("1")

c.Start()
defer c.Stop()
Expand Down