Skip to content

Commit

Permalink
Merge pull request #405 from jsdelivr/fetchsockets-fix
Browse files Browse the repository at this point in the history
feat: add option to force refresh fetchSockets value
  • Loading branch information
MartinKolarik authored Jul 25, 2023
2 parents e1d38c9 + 83c2da2 commit 63d3287
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/lib/ws/helper/probe-ip-limit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const verifyIpLimit = async (ip: string, socketId: string): Promise<void>
return;
}

const socketList = await fetchSockets();
const socketList = await fetchSockets({ forceRefresh: true });
const previousSocket = socketList.find(s => s.data.probe.ipAddress === ip && s.id !== socketId);

if (previousSocket) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ws/helper/throttle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const throttle = <Value>(func: () => Promise<Value>, time: number) => {
ttl: time,
fetchMethod: func,
});
return () => cache.fetch('') as Promise<Value>;
return (options?: {forceRefresh: true}) => cache.fetch('', options) as Promise<Value>;
};

export default throttle;
6 changes: 3 additions & 3 deletions src/lib/ws/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const TIME_UNTIL_VM_BECOMES_HEALTHY = 8000;
const logger = scopedLogger('ws-server');

let io: WsServer;
let throttledFetchSockets: () => Promise<RemoteSocket<DefaultEventsMap, SocketData>[]>;
let throttledFetchSockets: (options?: {forceRefresh: true}) => Promise<RemoteSocket<DefaultEventsMap, SocketData>[]>;

export const initWsServer = async () => {
const pubClient = getRedisClient().duplicate();
Expand Down Expand Up @@ -54,12 +54,12 @@ export const getWsServer = (): WsServer => {
return io;
};

export const fetchSockets = async () => {
export const fetchSockets = async (options?: {forceRefresh: true}) => {
if (!io || !throttledFetchSockets) {
throw new Error('WS server not initialized yet');
}

const sockets = await throttledFetchSockets();
const sockets = await throttledFetchSockets(options);

return sockets;
};
Expand Down

0 comments on commit 63d3287

Please sign in to comment.