mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
retry watch when it makes sense
Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
parent
701259695c
commit
73195c2f14
@ -96,6 +96,7 @@ export function ensureObjectSelfLink(api: KubeApi, object: KubeJsonApiData) {
|
|||||||
type KubeApiWatchOptions = {
|
type KubeApiWatchOptions = {
|
||||||
namespace: string;
|
namespace: string;
|
||||||
callback?: (data: IKubeWatchEvent) => void;
|
callback?: (data: IKubeWatchEvent) => void;
|
||||||
|
abortController?: AbortController
|
||||||
};
|
};
|
||||||
|
|
||||||
export class KubeApi<T extends KubeObject = any> {
|
export class KubeApi<T extends KubeObject = any> {
|
||||||
@ -366,13 +367,31 @@ export class KubeApi<T extends KubeObject = any> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
watch(opts: KubeApiWatchOptions = { namespace: "" }): () => void {
|
watch(opts: KubeApiWatchOptions = { namespace: "" }): () => void {
|
||||||
const { namespace, callback } = opts;
|
if (!opts.abortController) {
|
||||||
|
opts.abortController = new AbortController();
|
||||||
|
}
|
||||||
|
const { abortController, namespace, callback } = opts;
|
||||||
const watchUrl = this.getWatchUrl(namespace);
|
const watchUrl = this.getWatchUrl(namespace);
|
||||||
const abortController = new AbortController();
|
|
||||||
const responsePromise = this.request.getReadableStream(watchUrl, null, { signal: abortController.signal });
|
const responsePromise = this.request.getReadableStream(watchUrl, null, { signal: abortController.signal });
|
||||||
let disposed = false;
|
let disposed = false;
|
||||||
|
|
||||||
responsePromise.then((response) => {
|
responsePromise.then((response) => {
|
||||||
|
if (!response.ok && !disposed) {
|
||||||
|
if (response.status === 410) { // resourceVersion has gone
|
||||||
|
setTimeout(() => {
|
||||||
|
this.refreshResourceVersion().then(() => {
|
||||||
|
this.watch({...opts, abortController});
|
||||||
|
});
|
||||||
|
}, 1000);
|
||||||
|
|
||||||
|
} else if (response.status >= 500) { // k8s is having hard time
|
||||||
|
setTimeout(() => {
|
||||||
|
this.watch({...opts, abortController});
|
||||||
|
}, 5000);
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
const nodeStream = new ReadableWebToNodeStream(response.body);
|
const nodeStream = new ReadableWebToNodeStream(response.body);
|
||||||
const stream = byline(nodeStream);
|
const stream = byline(nodeStream);
|
||||||
|
|
||||||
@ -392,13 +411,9 @@ export class KubeApi<T extends KubeObject = any> {
|
|||||||
|
|
||||||
stream.on("close", () => {
|
stream.on("close", () => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
if (!disposed) this.watch({namespace, callback});
|
if (!disposed) this.watch({...opts, namespace, callback});
|
||||||
}, 1000);
|
}, 1000);
|
||||||
});
|
});
|
||||||
|
|
||||||
stream.on("error", (error) => {
|
|
||||||
console.error("stream error", error);
|
|
||||||
});
|
|
||||||
}, (error) => {
|
}, (error) => {
|
||||||
if (error instanceof DOMException) return; // AbortController rejects, we can ignore it
|
if (error instanceof DOMException) return; // AbortController rejects, we can ignore it
|
||||||
|
|
||||||
|
|||||||
@ -267,6 +267,7 @@ export abstract class KubeObjectStore<T extends KubeObject = any> extends ItemSt
|
|||||||
}
|
}
|
||||||
|
|
||||||
subscribe(apis = this.getSubscribeApis()) {
|
subscribe(apis = this.getSubscribeApis()) {
|
||||||
|
const abortController = new AbortController();
|
||||||
let disposers: {(): void}[] = [];
|
let disposers: {(): void}[] = [];
|
||||||
|
|
||||||
const callback = (data: IKubeWatchEvent) => {
|
const callback = (data: IKubeWatchEvent) => {
|
||||||
@ -275,13 +276,15 @@ export abstract class KubeObjectStore<T extends KubeObject = any> extends ItemSt
|
|||||||
|
|
||||||
if (this.context.cluster.isGlobalWatchEnabled) {
|
if (this.context.cluster.isGlobalWatchEnabled) {
|
||||||
disposers = apis.map(api => api.watch({
|
disposers = apis.map(api => api.watch({
|
||||||
|
abortController,
|
||||||
namespace: "",
|
namespace: "",
|
||||||
callback: (data) => callback(data)
|
callback: (data) => callback(data),
|
||||||
}));
|
}));
|
||||||
} else {
|
} else {
|
||||||
apis.map(api => {
|
apis.map(api => {
|
||||||
this.loadedNamespaces.forEach((namespace) => {
|
this.loadedNamespaces.forEach((namespace) => {
|
||||||
disposers.push(api.watch({
|
disposers.push(api.watch({
|
||||||
|
abortController,
|
||||||
namespace,
|
namespace,
|
||||||
callback: (data) => callback(data)
|
callback: (data) => callback(data)
|
||||||
}));
|
}));
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user