-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: retryable location hint (#1505)
Sometimes, it may take longer than 2000 ms. to retrieve the approximate location of the user. We now try up to three times before we eventually give up.
- Loading branch information
Showing
3 changed files
with
33 additions
and
22 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 |
---|---|---|
@@ -1,26 +1,32 @@ | ||
import { getLogger } from '../../logger'; | ||
|
||
const logger = getLogger(['location']); | ||
const HINT_URL = `https://hint.stream-io-video.com/`; | ||
|
||
export const getLocationHint = async ( | ||
hintUrl: string = HINT_URL, | ||
timeout: number = 2000, | ||
) => { | ||
const abortController = new AbortController(); | ||
const timeoutId = setTimeout(() => abortController.abort(), timeout); | ||
try { | ||
const response = await fetch(hintUrl, { | ||
method: 'HEAD', | ||
signal: abortController.signal, | ||
}); | ||
const awsPop = response.headers.get('x-amz-cf-pop') || 'ERR'; | ||
logger('debug', `Location header: ${awsPop}`); | ||
return awsPop.substring(0, 3); // AMS1-P2 -> AMS | ||
} catch (e) { | ||
logger('warn', `Failed to get location hint from ${hintUrl}`, e); | ||
return 'ERR'; | ||
} finally { | ||
clearTimeout(timeoutId); | ||
} | ||
hintUrl = `https://hint.stream-io-video.com/`, | ||
timeout = 2000, | ||
maxAttempts = 3, | ||
): Promise<string> => { | ||
const logger = getLogger(['location-hint']); | ||
|
||
let attempt = 0; | ||
let locationHint = 'ERR'; | ||
do { | ||
const abortController = new AbortController(); | ||
const timeoutId = setTimeout(() => abortController.abort(), timeout); | ||
try { | ||
const response = await fetch(hintUrl, { | ||
method: 'HEAD', | ||
signal: abortController.signal, | ||
}); | ||
const awsPop = response.headers.get('x-amz-cf-pop') || 'ERR'; | ||
logger('debug', `Location header: ${awsPop}`); | ||
locationHint = awsPop.substring(0, 3); // AMS1-P2 -> AMS | ||
} catch (e) { | ||
logger('warn', `Failed to get location hint from ${hintUrl}`, e); | ||
locationHint = 'ERR'; | ||
} finally { | ||
clearTimeout(timeoutId); | ||
} | ||
} while (locationHint === 'ERR' && ++attempt < maxAttempts); | ||
|
||
return locationHint; | ||
}; |
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