Skip to content

Commit

Permalink
Add whitelist/blacklist capacity to cn selection and rollover (#57)
Browse files Browse the repository at this point in the history
* Add whitelist/blacklist capacity to cn selection and rollover

* Bump version to 0.11.6

* Make blacklist null by default
  • Loading branch information
raymondjacobson authored Sep 16, 2019
1 parent 8b00598 commit bc1904e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion libs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion libs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@audius/libs",
"version": "0.11.5",
"version": "0.11.6",
"description": "",
"main": "src/index.js",
"browser": {
Expand Down
19 changes: 16 additions & 3 deletions libs/src/api/serviceProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,29 @@ class ServiceProvider extends Base {
* Fetches healthy creator nodes and autoselects a primary
* and two secondaries
* @param {number} numberOfNodes total number of nodes to fetch (2 secondaries means 3 total)
* @param {Set<string} blacklist whether or not to exclude any nodes
* @param {Set<string>?} whitelist whether or not to include only specified nodes (default no whiltelist)
* @param {Set<string?} blacklist whether or not to exclude any nodes (default no blacklist)
* @returns { primary, secondaries, services }
* // primary: string
* // secondaries: Array<string>
* // services: { creatorNodeEndpoint: versionInfo }
*/
async autoSelectCreatorNodes (numberOfNodes = 3, blacklist = new Set([])) {
async autoSelectCreatorNodes (
numberOfNodes = 3,
whitelist = null,
blacklist = null
) {
let creatorNodes = await this.listCreatorNodes()

// Filter whitelist
if (whitelist) {
creatorNodes = creatorNodes.filter(node => whitelist.has(node.endpoint))
}
// Filter blacklist
if (blacklist) {
creatorNodes = creatorNodes.filter(node => !blacklist.has(node.endpoint))
}

// Filter to healthy nodes
creatorNodes = (await Promise.all(
creatorNodes.map(async node => {
Expand All @@ -44,7 +58,6 @@ class ServiceProvider extends Base {
})
))
.filter(Boolean)
.filter(c => !blacklist.has(c))

// Time requests and autoselect nodes
const timings = await Utils.timeRequests(
Expand Down
8 changes: 6 additions & 2 deletions libs/src/sanityChecks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ class SanityChecks {
this.libs = libsInstance
}

async run () {
/**
* Runs sanity checks
* @param {Set<string>} creatorNodeWhitelist
*/
async run (creatorNodeWhitelist = null) {
await isCreator(this.libs)
await syncNodes(this.libs)
await rolloverNodes(this.libs)
await rolloverNodes(this.libs, creatorNodeWhitelist)
}
}

Expand Down
3 changes: 2 additions & 1 deletion libs/src/sanityChecks/rolloverNodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const getNewPrimary = async (libs, secondaries) => {
throw new Error(`Could not find valid secondaries for user ${secondaries}`)
}

const rolloverNodes = async (libs) => {
const rolloverNodes = async (libs, creatorNodeWhitelist) => {
const user = libs.userStateManager.getCurrentUser()

if (!user || !user.is_creator) return
Expand All @@ -48,6 +48,7 @@ const rolloverNodes = async (libs) => {
newSecondaries.splice(index, 1)
const autoselect = await libs.ServiceProvider.autoSelectCreatorNodes(
2 - newSecondaries.length,
creatorNodeWhitelist,
// Exclude ones we currently have
new Set([newPrimary, ...newSecondaries])
)
Expand Down

0 comments on commit bc1904e

Please sign in to comment.