Skip to content

Commit

Permalink
Merge branch 'main' into feat/race-nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
AmeanAsad committed Oct 26, 2023
2 parents 57697d2 + 897481c commit c0e3e93
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@filecoin-saturn/js-client",
"version": "0.3.0",
"version": "0.3.1",
"description": "Filecoin Saturn Client",
"homepage": "https://github.com/filecoin-saturn/js-client",
"main": "src/index.js",
Expand Down
26 changes: 26 additions & 0 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { getJWT } from './utils/jwt.js'
import { parseUrl, addHttpPrefix } from './utils/url.js'
import { isBrowserContext } from './utils/runtime.js'

const MAX_NODE_WEIGHT = 100

export class Saturn {
static nodesListKey = 'saturn-nodes'
static defaultRaceCount = 3
Expand Down Expand Up @@ -486,6 +488,29 @@ export class Saturn {
}
}

_sortNodes (nodes) {
// Determine the maximum distance for normalization
const maxDistance = Math.max(...nodes.map(node => node.distance))

// These weights determine how important each factor is in determining
const distanceImportanceFactor = 0.8
const weightImportanceFactor = 1 - distanceImportanceFactor

return nodes.slice().sort((a, b) => {
const normalizedDistanceA = a.distance / maxDistance
const normalizedDistanceB = b.distance / maxDistance
const normalizedWeightA = a.weight / MAX_NODE_WEIGHT
const normalizedWeightB = b.weight / MAX_NODE_WEIGHT

const metricA =
distanceImportanceFactor * normalizedDistanceA - weightImportanceFactor * normalizedWeightA
const metricB =
distanceImportanceFactor * normalizedDistanceB - weightImportanceFactor * normalizedWeightB

return metricA - metricB
})
}

async _loadNodes (opts) {
let origin = opts.orchURL

Expand Down Expand Up @@ -524,6 +549,7 @@ export class Saturn {
}
// we always want to update from the orchestrator regardless.
nodes = await orchNodesListPromise
nodes = this._sortNodes(nodes)
this.nodes = nodes
this.storage?.set(Saturn.nodesListKey, nodes)
}
Expand Down
2 changes: 1 addition & 1 deletion test/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function generateNodes (count, originDomain) {
const nodeIp = `node${i}`
const node = {
ip: nodeIp,
weight: i,
weight: 50,
distance: 100,
url: `https://${nodeIp}.${originDomain}`
}
Expand Down

0 comments on commit c0e3e93

Please sign in to comment.