@@ -35,7 +35,7 @@ export interface FetcherOptions {
35
35
* request(), process() and store() methods. Tasks can be arbitrary objects whose structure
36
36
* is defined by subclasses. A priority queue is used to ensure tasks are fetched
37
37
* inorder. Three types need to be provided: the JobTask, which describes a task the job should perform,
38
- * a JobResult, which is the direct result when a Peer replies to a Task, and a StorageItem, which
38
+ * a JobResult, which is the direct result when a Peer replies to a Task, and a StorageItem, which
39
39
* represents the to-be-stored items.
40
40
* @memberof module:sync/fetcher
41
41
*/
@@ -48,8 +48,8 @@ export abstract class Fetcher<JobTask, JobResult, StorageItem> extends Readable
48
48
protected banTime : number
49
49
protected maxQueue : number
50
50
protected maxPerRequest : number
51
- protected in : QHeap < Job < JobTask , JobResult > >
52
- protected out : QHeap < Job < JobTask , JobResult > >
51
+ protected in : QHeap < Job < JobTask , JobResult , StorageItem > >
52
+ protected out : QHeap < Job < JobTask , JobResult , StorageItem > >
53
53
protected total : number
54
54
protected processed : number // number of processed tasks, awaiting the write job
55
55
protected finished : number // number of tasks which are both processed and also finished writing
@@ -76,10 +76,16 @@ export abstract class Fetcher<JobTask, JobResult, StorageItem> extends Readable
76
76
this . maxPerRequest = options . maxPerRequest ?? 128
77
77
78
78
this . in = new Heap ( {
79
- comparBefore : ( a : Job < JobTask , JobResult > , b : Job < JobTask , JobResult > ) => a . index < b . index ,
79
+ comparBefore : (
80
+ a : Job < JobTask , JobResult , StorageItem > ,
81
+ b : Job < JobTask , JobResult , StorageItem >
82
+ ) => a . index < b . index ,
80
83
} )
81
84
this . out = new Heap ( {
82
- comparBefore : ( a : Job < JobTask , JobResult > , b : Job < JobTask , JobResult > ) => a . index < b . index ,
85
+ comparBefore : (
86
+ a : Job < JobTask , JobResult , StorageItem > ,
87
+ b : Job < JobTask , JobResult , StorageItem >
88
+ ) => a . index < b . index ,
83
89
} )
84
90
this . total = 0
85
91
this . processed = 0
@@ -89,26 +95,34 @@ export abstract class Fetcher<JobTask, JobResult, StorageItem> extends Readable
89
95
}
90
96
91
97
/**
92
- * Request results from peer for the given job. Resolves with the raw result.
98
+ * Request results from peer for the given job. Resolves with the raw result. If `undefined` is returned,
99
+ * re-queue the job.
93
100
* @param job
94
101
* @param peer
95
102
* @return {Promise }
96
103
*/
97
- abstract request ( _job ?: Job < JobTask , JobResult > , _peer ?: Peer ) : Promise < JobResult | undefined >
104
+ abstract request (
105
+ _job ?: Job < JobTask , JobResult , StorageItem > ,
106
+ _peer ?: Peer
107
+ ) : Promise < JobResult | undefined >
98
108
99
109
/**
100
- * Process the reply for the given job
110
+ * Process the reply for the given job. If the reply contains unexpected data, return `undefined`, this
111
+ * re-queues the job.
101
112
* @param job fetch job
102
113
* @param result result data
103
114
*/
104
- abstract process ( _job ?: Job < JobTask , JobResult > , _result ?: JobResult ) : JobResult | null
115
+ abstract process (
116
+ _job ?: Job < JobTask , JobResult , StorageItem > ,
117
+ _result ?: JobResult
118
+ ) : StorageItem [ ] | undefined
105
119
106
- /**
120
+ /**
107
121
* Store fetch result. Resolves once store operation is complete.
108
122
* @param result fetch result
109
123
* @return {Promise }
110
124
*/
111
- abstract async store ( _result ? : StorageItem [ ] ) : Promise < void >
125
+ abstract async store ( _result : StorageItem [ ] ) : Promise < void >
112
126
113
127
/**
114
128
* Generate list of tasks to fetch
@@ -122,13 +136,12 @@ export abstract class Fetcher<JobTask, JobResult, StorageItem> extends Readable
122
136
* Enqueue job
123
137
* @param job
124
138
*/
125
- enqueue ( job : Job < JobTask , JobResult > ) {
139
+ enqueue ( job : Job < JobTask , JobResult , StorageItem > ) {
126
140
if ( this . running ) {
127
141
this . in . insert ( {
128
142
...job ,
129
143
time : Date . now ( ) ,
130
144
state : 'idle' ,
131
- result : null ,
132
145
} )
133
146
}
134
147
}
@@ -160,7 +173,7 @@ export abstract class Fetcher<JobTask, JobResult, StorageItem> extends Readable
160
173
* @param job successful job
161
174
* @param result job result
162
175
*/
163
- success ( job : Job < JobTask , JobResult > , result ?: JobResult ) {
176
+ success ( job : Job < JobTask , JobResult , StorageItem > , result ?: JobResult ) {
164
177
if ( job . state !== 'active' ) return
165
178
if ( result === undefined ) {
166
179
this . enqueue ( job )
@@ -188,7 +201,7 @@ export abstract class Fetcher<JobTask, JobResult, StorageItem> extends Readable
188
201
* @param job failed job
189
202
* @param [error] error
190
203
*/
191
- failure ( job : Job < JobTask , JobResult > , error ?: Error ) {
204
+ failure ( job : Job < JobTask , JobResult , StorageItem > , error ?: Error ) {
192
205
if ( job . state !== 'active' ) return
193
206
job . peer ! . idle = true
194
207
this . pool . ban ( job . peer ! , this . banTime )
@@ -235,7 +248,7 @@ export abstract class Fetcher<JobTask, JobResult, StorageItem> extends Readable
235
248
* @param {Error } error error object
236
249
* @param {Object } job task
237
250
*/
238
- error ( error : Error , job ?: Job < JobTask , JobResult > ) {
251
+ error ( error : Error , job ?: Job < JobTask , JobResult , StorageItem > ) {
239
252
if ( this . running ) {
240
253
this . emit ( 'error' , error , job && job . task , job && job . peer )
241
254
}
@@ -293,11 +306,10 @@ export abstract class Fetcher<JobTask, JobResult, StorageItem> extends Readable
293
306
}
294
307
this . write ( )
295
308
this . tasks ( ) . forEach ( ( task : JobTask ) => {
296
- const job : Job < JobTask , JobResult > = {
309
+ const job : Job < JobTask , JobResult , StorageItem > = {
297
310
task,
298
311
time : Date . now ( ) ,
299
312
index : this . total ++ ,
300
- result : null ,
301
313
state : 'idle' ,
302
314
peer : null ,
303
315
}
@@ -321,15 +333,15 @@ export abstract class Fetcher<JobTask, JobResult, StorageItem> extends Readable
321
333
* @return {Peer }
322
334
*/
323
335
// TODO: what is job supposed to be?
324
- peer ( _job ?: Job < JobTask , JobResult > ) {
336
+ peer ( _job ?: Job < JobTask , JobResult , StorageItem > ) {
325
337
return this . pool . idle ( )
326
338
}
327
339
328
340
/**
329
341
* Expire job that has timed out and ban associated peer. Timed out tasks will
330
342
* be re-inserted into the queue.
331
343
*/
332
- expire ( job : Job < JobTask , JobResult > ) {
344
+ expire ( job : Job < JobTask , JobResult , StorageItem > ) {
333
345
job . state = 'expired'
334
346
if ( this . pool . contains ( job . peer ! ) ) {
335
347
this . config . logger . debug (
0 commit comments