Skip to content

Commit

Permalink
Add custom headers to all axios calls to health check in content node (
Browse files Browse the repository at this point in the history
…#3688)

* Add custom headers to all axios calls to health check in content node

* lint

* Standardize messages

* more lint
  • Loading branch information
dmanjunath authored Aug 12, 2022
1 parent 82dcf35 commit 1a38384
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const axios = require('axios')
const { promisify } = require('util')
const crypto = require('crypto')
const randomBytes = promisify(crypto.randomBytes)
const config = require('../../config')

const {
parseCNodeResponse,
Expand Down Expand Up @@ -101,6 +102,11 @@ const respondToURSMRequestForSignature = async (
timeout: 1000,
params: {
randomBytesToSign
},
headers: {
'User-Agent': `Axios - @audius/content-node - ${config.get(
'creatorNodeEndpoint'
)} - URSMRegistrationComponentService#respondToURSMRequestForSignature`
}
})
const { responseData, signatureData } = parseCNodeResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@ class CNodeHealthManager {
baseURL: endpoint,
url: '/health_check/verbose',
method: 'get',
timeout: PEER_HEALTH_CHECK_REQUEST_TIMEOUT_MS
timeout: PEER_HEALTH_CHECK_REQUEST_TIMEOUT_MS,
headers: {
'User-Agent': `Axios - @audius/content-node - ${config.get(
'creatorNodeEndpoint'
)} - CNodeHealthManager#queryVerboseHealthCheck`
}
})

return resp.data.data
Expand Down
7 changes: 6 additions & 1 deletion creator-node/src/snapbackSM/peerSetManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,12 @@ class PeerSetManager {
baseURL: endpoint,
url: '/health_check/verbose',
method: 'get',
timeout: PEER_HEALTH_CHECK_REQUEST_TIMEOUT_MS
timeout: PEER_HEALTH_CHECK_REQUEST_TIMEOUT_MS,
headers: {
'User-Agent': `Axios - @audius/content-node - ${config.get(
'creatorNodeEndpoint'
)} - peerSetManager#queryVerboseHealthCheck`
}
})

return resp.data.data
Expand Down
6 changes: 5 additions & 1 deletion libs/src/api/ServiceProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ export class ServiceProvider extends Base {
url: `${node.endpoint}/health_check/verbose`
})),
sortByVersion: true,
timeout
timeout,
headers: {
'User-Agent':
'Axios - @audius/sdk - ServiceProvider.ts#getSelectableCreatorNodes'
}
})

const services: { [id: string]: any } = {}
Expand Down
6 changes: 5 additions & 1 deletion libs/src/services/creatorNode/CreatorNodeSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,11 @@ export class CreatorNodeSelection extends ServiceSelection {
sortByVersion: false,
currentVersion: this.currentVersion,
timeout: this.timeout,
equivalencyDelta: this.equivalencyDelta
equivalencyDelta: this.equivalencyDelta,
headers: {
'User-Agent':
'Axios - @audius/sdk - CreatorNodeSelection.ts#_performHealthChecks'
}
})

const healthyServices = healthCheckedServices.filter((resp) => {
Expand Down
17 changes: 12 additions & 5 deletions libs/src/utils/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ interface Request {
url: string
}

interface TimingConfig {
interface AxiosConfig {
timeout?: number
headers?: object
}

export interface Timing {
Expand All @@ -38,15 +39,17 @@ export interface Timing {
*/
async function timeRequest(
request: Request,
timeout?: number | null
timeout?: number | null,
headers?: object | null
): Promise<Timing> {
// This is non-perfect because of the js event loop, but enough
// of a proximation. Don't use for mission-critical timing.
const startTime = new Date().getTime()
const config: TimingConfig = {}
const config: AxiosConfig = {}
if (timeout !== null && timeout !== undefined) {
config.timeout = timeout
}
if (headers) config.headers = headers
let response
try {
response = await axios.get(request.url, config)
Expand Down Expand Up @@ -131,6 +134,7 @@ interface TimeRequestsConfig {
* (even if by 1ms) will be picked always.
*/
equivalencyDelta?: number | null
headers?: object | null
}

/**
Expand All @@ -143,10 +147,13 @@ async function timeRequests({
currentVersion = null, // only required if `sortByVersion` = false
filterNonResponsive = false,
timeout = null,
equivalencyDelta = null
equivalencyDelta = null,
headers = null
}: TimeRequestsConfig) {
let serviceTimings = await Promise.all(
requests.map(async (request) => await timeRequest(request, timeout))
requests.map(
async (request) => await timeRequest(request, timeout, headers)
)
)

if (filterNonResponsive) {
Expand Down

0 comments on commit 1a38384

Please sign in to comment.