Skip to content

Commit 03f6752

Browse files
committed
swarm/network: simplify reg/unreg code, fix count and node assoc
1 parent 36b6592 commit 03f6752

File tree

1 file changed

+31
-36
lines changed

1 file changed

+31
-36
lines changed

swarm/network/kademlia/kademlia.go

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -120,70 +120,65 @@ func (self *Kademlia) On(node Node, cb func(*NodeRecord, Node) error) (err error
120120
bucket := self.buckets[index]
121121
// if bucket is full insertion replaces the worst node
122122
// TODO: give priority to peers with active traffic
123-
if len(bucket) >= self.BucketSize { // >= allows us to add peers beyond the bucketsize limitation
124-
// always rotate peers
125-
idle := self.MaxIdleInterval
126-
var pos int
127-
var replaced Node
128-
for i, p := range bucket {
129-
idleInt := time.Since(p.LastActive())
130-
if idleInt > idle {
131-
idle = idleInt
132-
pos = i
133-
replaced = p
134-
}
135-
}
136-
if replaced == nil {
137-
glog.V(logger.Debug).Infof("[KΛÐ]: all peers wanted, PO%03d bucket full", index)
138-
return fmt.Errorf("bucket full")
139-
}
140-
glog.V(logger.Debug).Infof("[KΛÐ]: node %v replaced by %v (idle for %v > %v)", replaced, node, idle, self.MaxIdleInterval)
141-
replaced.Drop()
142-
self.buckets[index] = append(bucket[:pos], bucket[(pos+1):]...)
143-
// there is no change in bucket cardinalities so no prox limit adjustment is needed
144-
return nil
145-
} else {
123+
if len(bucket) < self.BucketSize { // >= allows us to add peers beyond the bucketsize limitation
146124
self.buckets[index] = append(bucket, node)
147125
glog.V(logger.Debug).Infof("[KΛÐ]: add node %v to table", node)
148-
self.count++
149126
self.setProxLimit(index, true)
127+
record.node = node
128+
self.count++
129+
return nil
130+
}
131+
132+
// always rotate peers
133+
idle := self.MaxIdleInterval
134+
var pos int
135+
var replaced Node
136+
for i, p := range bucket {
137+
idleInt := time.Since(p.LastActive())
138+
if idleInt > idle {
139+
idle = idleInt
140+
pos = i
141+
replaced = p
142+
}
150143
}
144+
if replaced == nil {
145+
glog.V(logger.Debug).Infof("[KΛÐ]: all peers wanted, PO%03d bucket full", index)
146+
return fmt.Errorf("bucket full")
147+
}
148+
glog.V(logger.Debug).Infof("[KΛÐ]: node %v replaced by %v (idle for %v > %v)", replaced, node, idle, self.MaxIdleInterval)
149+
replaced.Drop()
150+
// actually replace in the row. When off(node) is called, the peer is no longer in the row
151+
bucket[pos] = node
152+
// there is no change in bucket cardinalities so no prox limit adjustment is needed
151153
record.node = node
154+
self.count++
152155
return nil
156+
153157
}
154158

155159
// Off is the called when a node is taken offline (from the protocol main loop exit)
156160
func (self *Kademlia) Off(node Node, cb func(*NodeRecord, Node)) (err error) {
157161
self.lock.Lock()
158162
defer self.lock.Unlock()
159163

160-
var found bool
161164
index := self.proximityBin(node.Addr())
162165
bucket := self.buckets[index]
163166
for i := 0; i < len(bucket); i++ {
164167
if node.Addr() == bucket[i].Addr() {
165-
found = true
166168
self.buckets[index] = append(bucket[:i], bucket[(i+1):]...)
169+
self.setProxLimit(index, false)
167170
break
168171
}
169172
}
170173

171-
if !found {
172-
// gracefully return without error if peer already unregistered
173-
glog.V(logger.Warn).Infof("[KΛÐ]: remove node %v not in table, population now is %v", node, self.count)
174-
return nil
175-
}
176-
177-
self.count--
178-
glog.V(logger.Debug).Infof("[KΛÐ]: remove node %v from table, population now is %v", node, self.count)
179-
self.setProxLimit(index, false)
180-
181174
record := self.db.index[node.Addr()]
182175
// callback on remove
183176
if cb != nil {
184177
cb(record, record.node)
185178
}
186179
record.node = nil
180+
self.count--
181+
glog.V(logger.Debug).Infof("[KΛÐ]: remove node %v from table, population now is %v", node, self.count)
187182

188183
return
189184
}

0 commit comments

Comments
 (0)