Skip to content

Commit

Permalink
fix(eventsub): raise timeout and retry if twitch have issues
Browse files Browse the repository at this point in the history
  • Loading branch information
sogehige committed Oct 13, 2022
1 parent d050596 commit 8070ca4
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/services/twitch/eventsub.ts
@@ -1,7 +1,7 @@
import crypto from 'crypto';

import { MINUTE } from '@sogebot/ui-helpers/constants';
import axios from 'axios';
import axios, { AxiosError } from 'axios';
import type { Request, Response } from 'express';
import localtunnel from 'localtunnel';
import type QueryString from 'qs';
Expand All @@ -21,6 +21,7 @@ import {
} from '~/helpers/log';
import { ioServer } from '~/helpers/panel';
import { variables } from '~/watchers';
import { setImmediateAwait } from '~/helpers/setImmediateAwait.js';

export const eventErrorShown = new Set<string>();

Expand Down Expand Up @@ -205,7 +206,7 @@ class EventSub {
});
}

async onStartup() {
async onStartup(): Promise<void> {
const broadcasterId = variables.get('services.twitch.broadcasterId') as string;
const clientId = variables.get('services.twitch.eventSubClientId') as string;
const useTunneling = variables.get('services.twitch.useTunneling') as string;
Expand Down Expand Up @@ -267,7 +268,7 @@ class EventSub {
'Authorization': 'Bearer ' + token,
'Client-ID': clientId,
},
timeout: 20000,
timeout: 60000,
});

const eventsList = [
Expand Down Expand Up @@ -303,7 +304,7 @@ class EventSub {
'Authorization': 'Bearer ' + token,
'Client-ID': clientId,
},
timeout: 20000,
timeout: 60000,
});
} catch (e) {
if (e instanceof Error) {
Expand All @@ -328,8 +329,16 @@ class EventSub {
await this.subscribe(event);
}
}
} catch (e: any) {
error(e);
} catch (e: unknown) {
if (e instanceof AxiosError) {
if (e.code === 'ECONNABORTED') {
warning(`eventsub(onStartup) => Connection to Twitch timed out. Will retry request.`); // ignore etimedout error
await setImmediateAwait();
return this.onStartup();
}
} else {
error(e);
}
}
}

Expand Down Expand Up @@ -364,7 +373,7 @@ class EventSub {
'secret': secret,
},
},
timeout: 20000,
timeout: 60000,
});
eventErrorShown.delete(event);
} catch (e: any) {
Expand Down

0 comments on commit 8070ca4

Please sign in to comment.