-
-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
78 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* Divide pubkeys into batches, each batch contains at most 5 http requests, | ||
* each request can work on at most 40 pubkeys. | ||
*/ | ||
export function batchItems<T>(items: T[], opts: {batchSize: number; maxBatches?: number}): T[][] { | ||
const batches: T[][] = []; | ||
const maxBatches = opts.maxBatches ?? Math.ceil(items.length / opts.batchSize); | ||
|
||
for (let i = 0; i < maxBatches; i++) { | ||
const batch = items.slice(opts.batchSize * i, opts.batchSize * (i + 1)); | ||
if (batch.length === 0) break; | ||
batches.push(batch); | ||
} | ||
|
||
return batches; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import {expect} from "chai"; | ||
import {batchItems} from "../../../src/util/batch"; | ||
|
||
describe("util / batch", function () { | ||
const testCases: {items: string[]; expected: string[][]}[] = [ | ||
{items: [], expected: []}, | ||
{items: ["1"], expected: [["1"]]}, | ||
{items: ["1", "2"], expected: [["1", "2"]]}, | ||
{items: ["1", "2", "3"], expected: [["1", "2"], ["3"]]}, | ||
{ | ||
items: ["1", "2", "3", "4"], | ||
expected: [ | ||
["1", "2"], | ||
["3", "4"], | ||
], | ||
}, | ||
{items: ["1", "2", "3", "4", "5"], expected: [["1", "2"], ["3", "4"], ["5"]]}, | ||
{ | ||
items: ["1", "2", "3", "4", "5", "6"], | ||
expected: [ | ||
["1", "2"], | ||
["3", "4"], | ||
["5", "6"], | ||
], | ||
}, | ||
// Ignore item 7 since it's over the max | ||
{ | ||
items: ["1", "2", "3", "4", "5", "6", "7"], | ||
expected: [ | ||
["1", "2"], | ||
["3", "4"], | ||
["5", "6"], | ||
], | ||
}, | ||
]; | ||
|
||
for (const {items: pubkeys, expected} of testCases) { | ||
it(`Batch ${pubkeys.length} items`, () => { | ||
expect(batchItems(pubkeys, {batchSize: 2, maxBatches: 3})).to.deep.equal(expected); | ||
}); | ||
} | ||
}); |