From e6d0995b65d255b2d8c4ce2cd5dc0c26b00a6a70 Mon Sep 17 00:00:00 2001 From: flike Date: Thu, 10 Mar 2016 16:44:37 +0800 Subject: [PATCH] bugfix[salve add/del] --- backend/node.go | 19 +++++++++++++++++-- core/errors/errors.go | 2 ++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/backend/node.go b/backend/node.go index b253887c..7f9ce5f7 100644 --- a/backend/node.go +++ b/backend/node.go @@ -176,6 +176,11 @@ func (n *Node) AddSlave(addr string) error { } n.Lock() defer n.Unlock() + for _, v := range n.Slave { + if v.addr == addr { + return errors.ErrSlaveExist + } + } addrAndWeight := strings.Split(addr, WeightSplit) if len(addrAndWeight) == 2 { weight, err = strconv.Atoi(addrAndWeight[1]) @@ -196,12 +201,22 @@ func (n *Node) AddSlave(addr string) error { } func (n *Node) DeleteSlave(addr string) error { + var i int n.Lock() defer n.Unlock() slaveCount := len(n.Slave) if slaveCount == 0 { return errors.ErrNoSlaveDB - } else if slaveCount == 1 { + } + for i = 0; i < slaveCount; i++ { + if n.Slave[i].addr == addr { + break + } + } + if i == slaveCount { + return errors.ErrSlaveNotExist + } + if slaveCount == 1 { n.Slave = nil n.SlaveWeights = nil n.RoundRobinQ = nil @@ -210,7 +225,7 @@ func (n *Node) DeleteSlave(addr string) error { s := make([]*DB, 0, slaveCount-1) sw := make([]int, 0, slaveCount-1) - for i := 0; i < slaveCount; i++ { + for i = 0; i < slaveCount; i++ { if n.Slave[i].addr != addr { s = append(s, n.Slave[i]) sw = append(sw, n.SlaveWeights[i]) diff --git a/core/errors/errors.go b/core/errors/errors.go index c03b967e..a34cfd24 100644 --- a/core/errors/errors.go +++ b/core/errors/errors.go @@ -64,4 +64,6 @@ var ( ErrDateIllegal = errors.New("date format illegal") ErrDateRangeIllegal = errors.New("date range format illegal") ErrDateRangeCount = errors.New("date range count is not equal") + ErrSlaveExist = errors.New("slave has exist") + ErrSlaveNotExist = errors.New("slave has not exist") )