Skip to content

Commit

Permalink
Merge pull request #2902 from Hyperkid123/limit-retry-connection
Browse files Browse the repository at this point in the history
Limit WS connection retries.
  • Loading branch information
Hyperkid123 authored Jul 23, 2024
2 parents 9f951a6 + 24beadf commit 74c258b
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/hooks/useChromeServiceEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useSetAtom } from 'jotai';
import { NotificationData, addNotificationAtom } from '../state/atoms/notificationDrawerAtom';
import { AddChromeWsEventListener, ChromeWsEventListener, ChromeWsEventTypes, ChromeWsPayload } from '@redhat-cloud-services/types';

const RETRY_LIMIT = 5;
const NOTIFICATION_DRAWER: ChromeWsEventTypes = 'com.redhat.console.notifications.drawer';
const ALL_TYPES: ChromeWsEventTypes[] = [NOTIFICATION_DRAWER];
type Payload = NotificationData;
Expand All @@ -28,6 +29,7 @@ const useChromeServiceEvents = (): AddChromeWsEventListener => {
const addNotification = useSetAtom(addNotificationAtom);
const isNotificationsEnabled = useFlag('platform.chrome.notifications-drawer');
const { token, tokenExpires } = useContext(ChromeAuthContext);
const retries = useRef(0);

const removeEventListener = (id: symbol) => {
const type = id.description as ChromeWsEventTypes;
Expand Down Expand Up @@ -71,6 +73,7 @@ const useChromeServiceEvents = (): AddChromeWsEventListener => {
connection.current = socket;

socket.onmessage = (event) => {
retries.current = 0;
const { data } = event;
try {
const payload = JSON.parse(data);
Expand All @@ -97,7 +100,11 @@ const useChromeServiceEvents = (): AddChromeWsEventListener => {
// renew connection on error
// data was unable to be sent
setTimeout(() => {
createConnection();
if (retries.current < RETRY_LIMIT) {
createConnection();
}

retries.current += 1;
}, 2000);
};
}
Expand Down

0 comments on commit 74c258b

Please sign in to comment.