Skip to content

Commit bb94c41

Browse files
authored
Merge pull request #2759 from devtron-labs/feat/online-connectivity-fallback
feat: online connectivity fallback endpoint
2 parents 32b8aac + d30b96d commit bb94c41

File tree

5 files changed

+71
-30
lines changed

5 files changed

+71
-30
lines changed

src/components/common/hooks/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ export const FILE_READING_FAILED_STATUS = {
3333
}
3434

3535
export const ONLINE_BANNER_TIMEOUT = 3000
36-
export const INTERNET_CONNECTIVITY_INTERVAL = 10000
36+
export const INTERNET_CONNECTIVITY_INTERVAL = 30000
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Routes } from '@Config/constants'
2+
3+
import { INTERNET_CONNECTIVITY_INTERVAL } from '../constants'
4+
import { CheckConnectivityParamsType, FetchConnectivityParamsType } from './types'
5+
6+
const fetchWithTimeout = ({ url, options, controller }: FetchConnectivityParamsType): Promise<any> => {
7+
const timeoutId = setTimeout(() => controller.abort(), INTERNET_CONNECTIVITY_INTERVAL)
8+
9+
return fetch(url, { ...options, signal: controller.signal }).finally(() => {
10+
clearTimeout(timeoutId)
11+
})
12+
}
13+
14+
export const getFallbackInternetConnectivity = ({ controller }: CheckConnectivityParamsType): Promise<any> =>
15+
fetchWithTimeout({
16+
url: 'https://www.google.com/favicon.ico',
17+
options: {
18+
method: 'HEAD',
19+
mode: 'no-cors',
20+
},
21+
controller,
22+
})
23+
24+
export const getInternetConnectivity = ({ controller }: CheckConnectivityParamsType): Promise<any> => {
25+
const baseUrl = window._env_?.CENTRAL_API_ENDPOINT ?? 'https://api.devtron.ai'
26+
const url = `${baseUrl}/${Routes.HEALTH}`
27+
28+
return fetchWithTimeout({ url, options: {}, controller })
29+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export interface FetchConnectivityParamsType {
2+
url: string
3+
options: RequestInit
4+
controller: AbortController
5+
}
6+
7+
export interface CheckConnectivityParamsType extends Pick<FetchConnectivityParamsType, 'controller'> {}

src/components/common/hooks/useOnline/useOnline.ts

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { useEffect, useRef, useState } from 'react'
22

3-
import { getIsRequestAborted, noop, useMainContext } from '@devtron-labs/devtron-fe-common-lib'
4-
5-
import { getInternetConnectivity } from '@Services/service'
3+
import { noop, useMainContext } from '@devtron-labs/devtron-fe-common-lib'
64

75
import { INTERNET_CONNECTIVITY_INTERVAL } from '../constants'
6+
import { getFallbackInternetConnectivity, getInternetConnectivity } from './service'
87

98
export const useOnline = ({ onOnline = noop }: { onOnline?: () => void }) => {
109
const [online, setOnline] = useState(structuredClone(navigator.onLine))
@@ -19,6 +18,14 @@ export const useOnline = ({ onOnline = noop }: { onOnline?: () => void }) => {
1918
abortControllerRef.current.abort()
2019
}
2120

21+
const onConnectivitySuccess = (checkConnectivity) => {
22+
setOnline((prev) => {
23+
if (!prev) onOnline()
24+
return true
25+
})
26+
timeoutRef.current = setTimeout(checkConnectivity, INTERNET_CONNECTIVITY_INTERVAL)
27+
}
28+
2229
const checkConnectivity = async () => {
2330
if (isAirgapped) return
2431

@@ -27,21 +34,34 @@ export const useOnline = ({ onOnline = noop }: { onOnline?: () => void }) => {
2734
abortControllerRef.current = controller
2835

2936
try {
30-
await getInternetConnectivity(abortControllerRef.current)
31-
setOnline((prev) => {
32-
if (!prev) {
33-
onOnline()
34-
}
35-
return true
37+
await getFallbackInternetConnectivity({
38+
controller: abortControllerRef.current,
3639
})
37-
timeoutRef.current = setTimeout(checkConnectivity, INTERNET_CONNECTIVITY_INTERVAL)
38-
} catch (error) {
39-
setOnline(false)
40-
if (!getIsRequestAborted(error)) {
41-
timeoutRef.current = setTimeout(checkConnectivity, INTERNET_CONNECTIVITY_INTERVAL)
40+
onConnectivitySuccess(checkConnectivity)
41+
} catch {
42+
if (abortControllerRef.current.signal.aborted) {
43+
if (timeoutRef.current) {
44+
timeoutRef.current = setTimeout(checkConnectivity, INTERNET_CONNECTIVITY_INTERVAL)
45+
}
46+
return
47+
}
48+
const fallbackController = new AbortController()
49+
abortControllerRef.current = fallbackController
50+
try {
51+
await getInternetConnectivity({
52+
controller: abortControllerRef.current,
53+
})
54+
onConnectivitySuccess(checkConnectivity)
55+
} catch {
56+
if (!abortControllerRef.current.signal.aborted) {
57+
setOnline(false)
58+
} else if (timeoutRef.current) {
59+
timeoutRef.current = setTimeout(checkConnectivity, INTERNET_CONNECTIVITY_INTERVAL)
60+
}
4261
}
4362
}
4463
}
64+
4565
const handleOffline = () => {
4666
handleClearTimeout()
4767
setOnline(false)

src/services/service.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,11 @@ import { Routes } from '../config'
3737
import {
3838
CDPipelines,
3939
AppListMin,
40-
ProjectFilteredApps,
4140
AppOtherEnvironment,
4241
ClusterEnvironmentDetailList,
4342
ClusterListResponse,
4443
LoginCountType,
4544
ConfigOverrideWorkflowDetailsResponse,
46-
AllWorkflows,
4745
MinChartRefDTO,
4846
ClusterEnvTeams,
4947
} from './service.types'
@@ -558,16 +556,3 @@ export const validateContainerConfiguration = (request: any): Promise<any> => {
558556
export const getTemplateOptions = (appId: number, envId: number): Promise<ResponseType<TemplateListDTO[]>> =>
559557
get(getUrlWithSearchParams(Routes.DEPLOYMENT_OPTIONS, { appId, envId }))
560558

561-
export const getInternetConnectivity = (controller: AbortController): Promise<any> => {
562-
const timeoutId = setTimeout(() => {
563-
controller.abort()
564-
}, 10000)
565-
566-
return fetch(`${window._env_?.CENTRAL_API_ENDPOINT ?? 'https://api.devtron.ai'}/${Routes.HEALTH}`, {
567-
signal: controller.signal,
568-
})
569-
.then((res) => res.json())
570-
.finally(() => {
571-
clearTimeout(timeoutId)
572-
})
573-
}

0 commit comments

Comments
 (0)