增加下载图片时3次重试

master v1.0.2
落雨楓 3 years ago
parent 0b96c79ccd
commit d6d443279c

@ -1133,6 +1133,8 @@ export class RemoteImageGenerationRequest {
onError: (err: { status: number; message: string }) => void, onError: (err: { status: number; message: string }) => void,
onClose: () => void onClose: () => void
): Promise<void> { ): Promise<void> {
const MAX_RETRY_TIMES = 3;
const requestStartGeneration: RequestInit = { const requestStartGeneration: RequestInit = {
mode: 'cors', mode: 'cors',
cache: 'no-store', cache: 'no-store',
@ -1203,6 +1205,7 @@ export class RemoteImageGenerationRequest {
const wsConnect = () => { const wsConnect = () => {
let ws: WebSocket | undefined = undefined; let ws: WebSocket | undefined = undefined;
let willClose = false; let willClose = false;
let hasError = false;
ws = new WebSocket(wsUrl.href) ws = new WebSocket(wsUrl.href)
@ -1212,7 +1215,8 @@ export class RemoteImageGenerationRequest {
} }
}, 15000) }, 15000)
ws.addEventListener('error', () => { ws.addEventListener('error', () => {
// reconnect hasError = true
// reconnect on error
sleep(2000).then(() => { sleep(2000).then(() => {
wsConnect() wsConnect()
}) })
@ -1222,6 +1226,12 @@ export class RemoteImageGenerationRequest {
clearInterval(heartbeatTimer) clearInterval(heartbeatTimer)
heartbeatTimer = undefined heartbeatTimer = undefined
} }
if (!willClose && !hasError) {
// reconnect on abnormal disconnect
sleep(2000).then(() => {
wsConnect()
})
}
}) })
ws.addEventListener('message', (event) => { ws.addEventListener('message', (event) => {
if (!event.data) return if (!event.data) return
@ -1317,15 +1327,6 @@ export class RemoteImageGenerationRequest {
} }
} }
const timeout = setTimeout(() => {
source.close()
onError({
status: 408,
message:
'Error: Timeout - Unable to reach Naifu servers. Please wait for a moment and try again',
})
}, 30 * 1000)
const requestGetGenerationOutput: RequestInit = { const requestGetGenerationOutput: RequestInit = {
mode: 'cors', mode: 'cors',
cache: 'no-store', cache: 'no-store',
@ -1344,28 +1345,50 @@ export class RemoteImageGenerationRequest {
}*/), }*/),
} }
const source = new SSE(BackendURLGetGenerateImageOutput, { let retryTimes = 0;
headers: requestGetGenerationOutput.headers,
payload: requestGetGenerationOutput.body const requestOutput = () => {
}) const timeout = setTimeout(() => {
source.addEventListener('newImage', (message: any) => { source.close()
clearTimeout(timeout) onError({
onImage(Buffer.from(message.data, 'base64'), message.id) status: 408,
}) message:
source.addEventListener('error', (err: any) => { 'Error: Timeout - Unable to reach Naifu servers. Please wait for a moment and try again',
clearTimeout(timeout) })
source.close() }, 30 * 1000)
onError({
status: err.detail.statusCode ?? 'unknown status', const source = new SSE(BackendURLGetGenerateImageOutput, {
message: err.detail.message || err.detail.error, headers: requestGetGenerationOutput.headers,
payload: requestGetGenerationOutput.body
}) })
logWarning(err, true, 'streaming error') source.addEventListener('newImage', (message: any) => {
}) clearTimeout(timeout)
source.addEventListener('readystatechange', (e: any) => { onImage(Buffer.from(message.data, 'base64'), message.id)
if (source.readyState === 2) { })
onClose() source.addEventListener('error', async (err: any) => {
} clearTimeout(timeout)
}) source.close()
source.stream()
logWarning(err, true, 'streaming error')
if (retryTimes < MAX_RETRY_TIMES) { // Should retry
retryTimes ++
await sleep(2000)
requestOutput()
} else {
onError({
status: err.detail.statusCode ?? 'unknown status',
message: err.detail.message || err.detail.error,
})
}
})
source.addEventListener('readystatechange', (e: any) => {
if (source.readyState === 2) {
onClose()
}
})
source.stream()
}
requestOutput()
} }
} }

@ -78,5 +78,5 @@ export const LoadingBarInner = styled.div<{ visible: boolean }>`
background-repeat: repeat-x; background-repeat: repeat-x;
animation: ${Gradient} 2s linear infinite; animation: ${Gradient} 2s linear infinite;
opacity: ${(props) => (props.visible ? '1' : '0')}; opacity: ${(props) => (props.visible ? '1' : '0')};
transition: width 250ms ease-in-out; transition: width 250ms linear;
` `
Loading…
Cancel
Save