mirror of
https://github.com/datarhei/core.git
synced 2025-10-05 16:07:07 +08:00
Prevent adding a node to itself
This commit is contained in:
@@ -500,6 +500,7 @@ func (a *api) start() error {
|
|||||||
a.restream = restream
|
a.restream = restream
|
||||||
|
|
||||||
if cluster, err := cluster.New(cluster.ClusterConfig{
|
if cluster, err := cluster.New(cluster.ClusterConfig{
|
||||||
|
ID: cfg.ID,
|
||||||
IPLimiter: a.sessionsLimiter,
|
IPLimiter: a.sessionsLimiter,
|
||||||
Logger: a.log.logger.core.WithComponent("Cluster"),
|
Logger: a.log.logger.core.WithComponent("Cluster"),
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
|
@@ -40,11 +40,14 @@ type Cluster interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ClusterConfig struct {
|
type ClusterConfig struct {
|
||||||
|
ID string
|
||||||
IPLimiter net.IPLimiter
|
IPLimiter net.IPLimiter
|
||||||
Logger log.Logger
|
Logger log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
type cluster struct {
|
type cluster struct {
|
||||||
|
id string
|
||||||
|
|
||||||
nodes map[string]*node // List of known nodes
|
nodes map[string]*node // List of known nodes
|
||||||
idfiles map[string][]string // Map from nodeid to list of files
|
idfiles map[string][]string // Map from nodeid to list of files
|
||||||
idupdate map[string]time.Time // Map from nodeid to time of last update
|
idupdate map[string]time.Time // Map from nodeid to time of last update
|
||||||
@@ -63,6 +66,7 @@ type cluster struct {
|
|||||||
|
|
||||||
func New(config ClusterConfig) (Cluster, error) {
|
func New(config ClusterConfig) (Cluster, error) {
|
||||||
c := &cluster{
|
c := &cluster{
|
||||||
|
id: config.ID,
|
||||||
nodes: map[string]*node{},
|
nodes: map[string]*node{},
|
||||||
idfiles: map[string][]string{},
|
idfiles: map[string][]string{},
|
||||||
idupdate: map[string]time.Time{},
|
idupdate: map[string]time.Time{},
|
||||||
@@ -145,6 +149,10 @@ func (c *cluster) AddNode(address, username, password string) (string, error) {
|
|||||||
|
|
||||||
id := node.ID()
|
id := node.ID()
|
||||||
|
|
||||||
|
if id == c.id {
|
||||||
|
return "", fmt.Errorf("can't add myself as node or a node with the same ID")
|
||||||
|
}
|
||||||
|
|
||||||
c.lock.Lock()
|
c.lock.Lock()
|
||||||
defer c.lock.Unlock()
|
defer c.lock.Unlock()
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user