Skip to content

Commit

Permalink
bugfix[salve add/del]
Browse files Browse the repository at this point in the history
  • Loading branch information
flike committed Mar 10, 2016
1 parent 8742f7a commit e6d0995
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
19 changes: 17 additions & 2 deletions backend/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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
Expand All @@ -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])
Expand Down
2 changes: 2 additions & 0 deletions core/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)

0 comments on commit e6d0995

Please sign in to comment.