@@ -45,7 +45,8 @@ class Grape extends Events {
45
45
host : this . conf . host || false ,
46
46
bootstrap : this . conf . dht_bootstrap ,
47
47
timeBucketOutdated : this . conf . dht_nodeLiveness ,
48
- verify : crypto . verify
48
+ verify : crypto . verify ,
49
+ maxAge : this . conf . timeslot
49
50
} )
50
51
51
52
dht . on ( 'announce' , ( _peer , ih ) => {
@@ -54,6 +55,12 @@ class Grape extends Events {
54
55
this . emit ( 'announce' , val )
55
56
} )
56
57
58
+ dht . on ( 'unannounce' , ( _peer , ih ) => {
59
+ const val = this . hex2str ( ih )
60
+ debug ( this . conf . dht_port , 'unannounce' , val )
61
+ this . emit ( 'unannounce' , val )
62
+ } )
63
+
57
64
dht . on ( 'warning' , ( ) => {
58
65
debug ( this . conf . dht_port , 'warning' )
59
66
this . emit ( 'warning' )
@@ -128,15 +135,6 @@ class Grape extends Events {
128
135
}
129
136
}
130
137
131
- timeslot ( offset , ts ) {
132
- offset = offset || 0
133
- ts = ts || Date . now ( )
134
- ts -= offset * this . conf . timeslot * - 1
135
- ts = ts - ( ts % this . conf . timeslot )
136
-
137
- return ts
138
- }
139
-
140
138
onRequest ( type , data , cb ) {
141
139
const met = `handlePeer${ _ . upperFirst ( _ . camelCase ( `-${ type } ` ) ) } `
142
140
@@ -152,17 +150,10 @@ class Grape extends Events {
152
150
return cb ( new Error ( 'ERR_GRAPE_LOOKUP' ) )
153
151
}
154
152
155
- async . reduce ( [
156
- `${ _val } -${ this . timeslot ( 0 ) } ` ,
157
- `${ _val } -${ this . timeslot ( - 1 ) } `
158
- ] , [ ] , ( acc , slot , next ) => {
159
- this . lookup (
160
- slot ,
161
- ( err , res ) => {
162
- next ( null , err ? acc : _ . union ( acc , res ) )
163
- }
164
- )
165
- } , cb )
153
+ this . lookup (
154
+ _val ,
155
+ cb
156
+ )
166
157
}
167
158
168
159
handlePeerAnnounce ( data , cb ) {
@@ -171,7 +162,16 @@ class Grape extends Events {
171
162
}
172
163
173
164
const [ val , port ] = [ data [ 0 ] , data [ 1 ] ]
174
- this . announce ( `${ val } -${ this . timeslot ( 0 ) } ` , port , cb )
165
+ this . announce ( val , port , cb )
166
+ }
167
+
168
+ handlePeerUnannounce ( data , cb ) {
169
+ if ( ! data || ! _ . isArray ( data ) ) {
170
+ return cb ( new Error ( 'ERR_GRAPE_UNANNOUNCE' ) )
171
+ }
172
+
173
+ const [ val , port ] = [ data [ 0 ] , data [ 1 ] ]
174
+ this . unannounce ( val , port , cb )
175
175
}
176
176
177
177
handlePeerPut ( opts , cb ) {
@@ -209,6 +209,19 @@ class Grape extends Events {
209
209
)
210
210
}
211
211
212
+ unannounce ( val , port , cb ) {
213
+ if ( ! _ . isInteger ( port ) ) {
214
+ return cb ( new Error ( 'ERR_GRAPE_SERVICE_PORT' ) )
215
+ }
216
+
217
+ cb = cb || noop
218
+ this . node . unannounce (
219
+ this . str2hex ( val ) ,
220
+ port || this . conf . dht_port ,
221
+ cb
222
+ )
223
+ }
224
+
212
225
lookup ( val , cb ) {
213
226
const ih = this . str2hex ( val )
214
227
@@ -219,6 +232,7 @@ class Grape extends Events {
219
232
const kcnt = this . cbq0 . cnt ( ih )
220
233
if ( kcnt > 1 ) return
221
234
235
+ this . _mem . remove ( ih )
222
236
this . node . lookup ( ih , ( err , cnt ) => {
223
237
debug ( `lookup ${ val } found ${ cnt } nodes` )
224
238
0 commit comments