Skip to content

Commit

Permalink
ULMS-3281 Updated tymeout and retry options for signaling requests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkonst committed Sep 2, 2024
1 parent 28e494a commit c5a905a
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 19 deletions.
4 changes: 2 additions & 2 deletions 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 package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ulms/api-clients",
"version": "7.15.0",
"version": "7.16.0",
"description": "JavaScript API clients for ULMS platform",
"keywords": [],
"homepage": "https://github.com/foxford/ulms-api-clients-js#readme",
Expand Down
26 changes: 26 additions & 0 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,29 @@ export const timeout = async (delay) => {

return promise
}

export function mergeSignals(signals) {
if (AbortSignal && AbortSignal.any) {
return AbortSignal.any(signals)
}

const controller = new AbortController()

for (const signal of signals) {
if (signal.aborted) {
controller.abort(signal.reason)

return controller.signal
}

signal.addEventListener(
'abort',
() => {
controller.abort(signal.reason)
},
{ once: true },
)
}

return controller.signal
}
23 changes: 19 additions & 4 deletions src/http-client.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/* eslint-disable class-methods-use-this */
import { mergeSignals } from './common'
import { NetworkError } from './error'

function createTimeoutSignal(timeout) {
if (AbortSignal && AbortSignal.timeout) {
return { signal: AbortSignal.timeout(timeout) }
}

const controller = new AbortController()
const { signal } = controller
const id = setTimeout(() => controller.abort(), timeout)
Expand All @@ -12,22 +17,32 @@ function createTimeoutSignal(timeout) {

class FetchHttpClient {
async request(url, config) {
const { timeout, ...requestConfig } = config
const { signal: appSignal, timeout, ...requestConfig } = config
const requestOptions = {
...requestConfig,
}
const signals = []
let onFinally
let response

if (appSignal) {
signals.push(appSignal)
}

if (timeout !== undefined) {
const { cleanup, signal } = createTimeoutSignal(timeout)
const { cleanup, signal: timeoutSignal } = createTimeoutSignal(timeout)

signals.push(timeoutSignal)

requestOptions.signal = signal
onFinally = cleanup
}

if (signals.length > 0) {
requestOptions.signal = mergeSignals(signals)
}

try {
response = fetch(url, requestOptions)
response = await fetch(url, requestOptions)
} catch (error) {
throw new NetworkError('network_error', { cause: error })
} finally {
Expand Down
35 changes: 23 additions & 12 deletions src/ulms.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ async function handleResponse(response) {
return data
}

const DEFAULT_REQUEST_TIMEOUT_MS = 10_000
const requestOptionsWithRetryAndTimeout = {
retry: true,
timeout: DEFAULT_REQUEST_TIMEOUT_MS,
}

class ULMS extends BasicClient {
agentLabel

Expand Down Expand Up @@ -282,38 +288,49 @@ class ULMS extends BasicClient {
* @param {Object|Object[]} jsep
* @param {String} intent
* @param {String} label
* @param {Object} requestOptions
* @returns {Promise}
*/
createSignal(
rtcId,
jsep,
intent = ULMS.intents.INTENT_READ, // eslint-disable-line default-param-last
label,
requestOptions = {},
) {
const payload = {
agent_label: this.agentLabel,
intent,
jsep,
label,
}
const { signal } = requestOptions

return this.post(this.url(`/conference_rtcs/${rtcId}/signal`), payload)
return this.post(this.url(`/conference_rtcs/${rtcId}/signal`), payload, {
...requestOptionsWithRetryAndTimeout,
signal,
})
}

/**
* Create trickle signal
* @param {String} handleId
* @param {Object|Object[]} candidates
* @param {Object} requestOptions
* @returns {Promise}
*/
createTrickleSignal(handleId, candidates) {
createTrickleSignal(handleId, candidates, requestOptions = {}) {
const payload = {
agent_label: this.agentLabel,
candidates,
handle_id: handleId,
}
const { signal } = requestOptions

return this.post(this.url('/conference_streams/trickle'), payload)
return this.post(this.url('/conference_streams/trickle'), payload, {
...requestOptionsWithRetryAndTimeout,
signal,
})
}

/**
Expand Down Expand Up @@ -498,7 +515,7 @@ class ULMS extends BasicClient {
readScope(kind, audience, scope, options) {
return this.get(
this.url(`/audiences/${audience}/${kind}/${scope}`, options),
{ timeout: 10_000, retry: true },
requestOptionsWithRetryAndTimeout,
)
}

Expand Down Expand Up @@ -690,17 +707,11 @@ class ULMS extends BasicClient {
* @returns {Promise}
*/
updatePosition(kind, classId, position) {
const controller = new AbortController()
const { signal } = controller
const timeoutId = setTimeout(() => controller.abort(), 10 * 1000)

return this.post(
`${this.baseUrl}/${kind}/${classId}/timestamps`,
{ position },
{ signal },
).finally(() => {
clearTimeout(timeoutId)
})
requestOptionsWithRetryAndTimeout,
)
}

/**
Expand Down

0 comments on commit c5a905a

Please sign in to comment.