Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix cache quest lock condition #35

Merged
merged 2 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/global-store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { MetaQuery } from '@refinedev/core';
import { isEqual } from 'lodash';
import {
KubeApi,
UnstructuredList,
Expand Down Expand Up @@ -74,7 +73,11 @@ export class GlobalStore {
}

const cacheRequest = this.requestsCache.find(f => {
return f.resource === resource && isEqual(f.meta, meta);
// TODO: Its supposed to be strictly comparison of query path
return (
f.resource === resource &&
f.meta?.resourceBasePath === meta?.resourceBasePath
);
});

// return cached fetching request
Expand Down
43 changes: 32 additions & 11 deletions src/kube-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,13 @@ class ApiVersionResourceCache {
return resources.find(r => r.kind === kind)!;
}

public async resourceByName(resourcePath: string, name: string) {
public async resourceByName(
resourcePath: string,
name: string,
signal?: AbortSignal
) {
const localVarPath = `${this.basePath}${resourcePath}`;
const { resources } = await this.fetchAndCache(localVarPath);
const { resources } = await this.fetchAndCache(localVarPath, signal);
return resources.find(r => r.name === name)!;
}

Expand All @@ -214,9 +218,11 @@ class ApiVersionResourceCache {
return [this.basePath, api, apiVersion].join('/');
}

private async fetchAndCache(localVarPath: string) {
private async fetchAndCache(localVarPath: string, signal?: AbortSignal) {
if (!this.cache[localVarPath]) {
const resources = await ky.get(localVarPath).json<APIResourceList>();
const resources = await ky
.get(localVarPath, { signal })
.json<APIResourceList>();
this.cache[localVarPath] = resources;
}
return this.cache[localVarPath];
Expand Down Expand Up @@ -299,7 +305,8 @@ export class KubeApi<T extends UnstructuredList> {
query,
onResponse,
onEvent,
})
}),
signal
);
const stop = () => {
stopWatch?.();
Expand All @@ -315,7 +322,8 @@ export class KubeApi<T extends UnstructuredList> {
onResponse: KubeApiListWatchOptions<T>['onResponse'],
onEvent: KubeApiListWatchOptions<T>['onEvent'],
// let listwatch know it needs retry
retry: () => Promise<StopWatchHandler>
retry: () => Promise<StopWatchHandler>,
signal?: AbortSignal
): Promise<StopWatchHandler> {
let { items } = response as unknown as UnstructuredList;
const stops: Array<() => void> = [];
Expand Down Expand Up @@ -384,14 +392,17 @@ export class KubeApi<T extends UnstructuredList> {
// server-side watch
const resource = await this.apiVersionResourceCache.resourceByName(
this.resourceBasePath,
this.resource
this.resource,
signal
);
const verbs = resource?.verbs || [];
const supportServerWatch = verbs.includes('watch');

if (supportServerWatch) {
if (this.watchWsBasePath) {
stops.push(this.watchByWebsocket(url, response, handleEvent, retry));
stops.push(
this.watchByWebsocket(url, response, handleEvent, retry, signal)
);
} else {
stops.push(this.watchByHttp(url, response, handleEvent, retry));
}
Expand Down Expand Up @@ -429,7 +440,8 @@ export class KubeApi<T extends UnstructuredList> {
res: T,
handleEvent: (event: WatchEvent) => void,
// let listwatch know it needs retry
retry: () => Promise<StopWatchHandler>
retry: () => Promise<StopWatchHandler>,
signal?: AbortSignal
) {
const { resourceVersion = '' } = (res as unknown as UnstructuredList)
.metadata;
Expand All @@ -440,6 +452,15 @@ export class KubeApi<T extends UnstructuredList> {
: `${protocol}://${location.host}/${url}?resourceVersion=${resourceVersion}&watch=1`
);
let shouldCloseAfterConnected = false;
const abortListener = () => {
if (socket.readyState === socket.CONNECTING) {
shouldCloseAfterConnected = true;
}
};
const removeAbortListener = () => {
signal?.removeEventListener('abort', abortListener);
};
signal?.addEventListener('abort', abortListener);
let stopWatch: () => void = () => {
if (socket.readyState === socket.OPEN) {
socket.close(3001, 'KUBEAPI_MANUAL_CLOSE');
Expand All @@ -458,6 +479,7 @@ export class KubeApi<T extends UnstructuredList> {
return;
}
heartbeat(socket);
removeAbortListener();
});
socket.addEventListener('message', function (msg) {
const event = JSON.parse(msg.data) as WatchEvent;
Expand All @@ -467,11 +489,10 @@ export class KubeApi<T extends UnstructuredList> {
});
socket.addEventListener('close', evt => {
clearTimeout((socket as ExtendedWebsocketClient).pingTimeout);

if (evt.reason === 'KUBEAPI_MANUAL_CLOSE') {
return;
}

removeAbortListener();
this.retryFunc(async () => {
stopWatch = await retry();
});
Expand Down
Loading