This repository was archived by the owner on Jul 21, 2023. It is now read-only.
Commit 534a2d9 1 parent eacdc6b commit 534a2d9 Copy full SHA for 534a2d9
File tree 3 files changed +23
-4
lines changed
3 files changed +23
-4
lines changed Original file line number Diff line number Diff line change @@ -109,15 +109,15 @@ module.exports = (dht) => {
109
109
let counterAll = 0
110
110
let counterSuccess = 0
111
111
112
- for await ( const peer of dht . getClosestPeers ( key , { shallow : true } ) ) {
112
+ await utils . mapParallel ( dht . getClosestPeers ( key , { shallow : true } ) , async ( peer ) => {
113
113
try {
114
114
counterAll += 1
115
115
await dht . _putValueToPeer ( key , record , peer )
116
116
counterSuccess += 1
117
117
} catch ( err ) {
118
118
dht . _log . error ( 'Failed to put to peer (%b): %s' , peer . id , err )
119
119
}
120
- }
120
+ } )
121
121
122
122
// verify if we were able to put to enough peers
123
123
const minPeers = options . minPeers || counterAll // Ensure we have a default `minPeers`
Original file line number Diff line number Diff line change @@ -44,14 +44,14 @@ module.exports = (dht) => {
44
44
} ]
45
45
46
46
// Notify closest peers
47
- for await ( const peer of dht . getClosestPeers ( key . buffer ) ) {
47
+ await utils . mapParallel ( dht . getClosestPeers ( key . buffer ) , async ( peer ) => {
48
48
dht . _log ( 'putProvider %s to %s' , key . toBaseEncodedString ( ) , peer . toB58String ( ) )
49
49
try {
50
50
await dht . network . sendMessage ( peer , msg )
51
51
} catch ( err ) {
52
52
errors . push ( err )
53
53
}
54
- }
54
+ } )
55
55
56
56
if ( errors . length ) {
57
57
// TODO:
Original file line number Diff line number Diff line change @@ -206,3 +206,22 @@ exports.withTimeout = (asyncFn, time) => {
206
206
] )
207
207
}
208
208
}
209
+
210
+ /**
211
+ * Iterates the given `asyncIterator` and runs each item through the given `asyncFn` in parallel.
212
+ * Returns a promise that resolves when all items of the `asyncIterator` have been passed
213
+ * through `asyncFn`.
214
+ *
215
+ * @param {AsyncIterable } [asyncIterator]
216
+ * @param {Function } [asyncFn]
217
+ * @returns {Array }
218
+ *
219
+ * @private
220
+ */
221
+ exports . mapParallel = async function ( asyncIterator , asyncFn ) {
222
+ const tasks = [ ]
223
+ for await ( const item of asyncIterator ) {
224
+ tasks . push ( asyncFn ( item ) )
225
+ }
226
+ return Promise . all ( tasks )
227
+ }
You can’t perform that action at this time.
0 commit comments