@@ -120,70 +120,65 @@ func (self *Kademlia) On(node Node, cb func(*NodeRecord, Node) error) (err error
120
120
bucket := self .buckets [index ]
121
121
// if bucket is full insertion replaces the worst node
122
122
// 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
146
124
self .buckets [index ] = append (bucket , node )
147
125
glog .V (logger .Debug ).Infof ("[KΛÐ]: add node %v to table" , node )
148
- self .count ++
149
126
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
+ }
150
143
}
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
151
153
record .node = node
154
+ self .count ++
152
155
return nil
156
+
153
157
}
154
158
155
159
// Off is the called when a node is taken offline (from the protocol main loop exit)
156
160
func (self * Kademlia ) Off (node Node , cb func (* NodeRecord , Node )) (err error ) {
157
161
self .lock .Lock ()
158
162
defer self .lock .Unlock ()
159
163
160
- var found bool
161
164
index := self .proximityBin (node .Addr ())
162
165
bucket := self .buckets [index ]
163
166
for i := 0 ; i < len (bucket ); i ++ {
164
167
if node .Addr () == bucket [i ].Addr () {
165
- found = true
166
168
self .buckets [index ] = append (bucket [:i ], bucket [(i + 1 ):]... )
169
+ self .setProxLimit (index , false )
167
170
break
168
171
}
169
172
}
170
173
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
-
181
174
record := self .db .index [node .Addr ()]
182
175
// callback on remove
183
176
if cb != nil {
184
177
cb (record , record .node )
185
178
}
186
179
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 )
187
182
188
183
return
189
184
}
0 commit comments