Skip to content

Commit

Permalink
fix: gossip protocol implement.
Browse files Browse the repository at this point in the history
  • Loading branch information
auula committed Nov 2, 2024
1 parent 905a4b4 commit 7d2689b
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions server/gossip/gossip.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
"time"
)

const MinNeighbors = 3 // 最小节点数,奇数
const minNeighbors = 3 // 最小节点数,奇数

type Node struct {
// 类似于私有化 Node {} 构造器
// 只能通过 NewNode 进行实例化一个节点
type innerNode struct {
ID string // 节点唯一标识
Address string // 节点地址
Heartbeat int // 心跳计数
Expand All @@ -19,7 +21,7 @@ type Node struct {
Hash uint32 // 节点哈希值,用于一致性哈希
}

type Cluster struct {
type innerCluster struct {
Neighbors map[string]*Node // 所有已知节点的信息
Self *Node // 本节点信息
Mutex sync.Mutex // 保护 Nodes 的并发访问
Expand All @@ -28,27 +30,40 @@ type Cluster struct {
HashRing []*Node // 一致性哈希存储范围的键
}

type Node struct {
innerNode
}

type Cluster struct {
innerCluster
}

func NewNode(id, addr string) *Node {
node := &Node{
ID: id,
Address: addr,
Heartbeat: 0,
Timestamp: time.Now().Unix(),
Alive: true,
innerNode{
ID: id,
Address: addr,
Heartbeat: 0,
Timestamp: time.Now().Unix(),
Alive: true,
},
}
// 计算节点的哈希值
// 通过节点 ID 算出在哈希环中的值
node.Hash = NodeHash(id)
return node
}

func NewCluster(id, addr string, interval, timeout time.Duration) *Cluster {
self := NewNode(id, addr)
return &Cluster{
Neighbors: map[string]*Node{id: self},
Self: self,
Interval: interval,
Timeout: timeout,
HashRing: []*Node{self}, // 将自身添加到哈希环,多个节点形成一个哈希环
innerCluster{
Neighbors: map[string]*Node{id: self},
Self: self,
Interval: interval,
Timeout: timeout,
// 将自身添加到哈希环,多个节点形成一个哈希环
HashRing: []*Node{self},
},
}
}

Expand All @@ -65,14 +80,12 @@ func (c *Cluster) AddNodes(nodes ...Node) error {
defer c.Mutex.Unlock()

// 必须是奇数数量的节点集群
if len(nodes) < MinNeighbors {
if len(nodes) < minNeighbors {
return errors.New("")
}

// 设置节点哈希值并将节点加入到邻居中
for _, node := range nodes {
// 通过节点 ID 算出在哈希环中的值
node.Hash = NodeHash(node.ID)
c.Neighbors[node.ID] = &node
// 节点对应哈希值也要放到一致性节点环中
c.HashRing = append(c.HashRing, &node)
Expand Down

0 comments on commit 7d2689b

Please sign in to comment.