Handle new rate limiting info from CDSI

This commit is contained in:
Fedor Indutny 2022-11-22 10:13:55 -08:00 committed by GitHub
parent b26f60d2fc
commit f0a3735ca2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 36 deletions

View file

@ -17,6 +17,7 @@ import type {
CDSResponseType,
CDSAuthType,
} from './Types.d';
import { RateLimitedError } from './RateLimitedError';
import { connect as connectWebSocket } from '../WebSocket';
const REQUEST_TIMEOUT = 10 * SECOND;
@ -65,19 +66,19 @@ export abstract class CDSSocketManagerBase<
}
// Send request
const { response, retryAfterSecs = 0 } = await pTimeout(
socket.request(options),
timeout
);
if (retryAfterSecs > 0) {
this.retryAfter = Math.max(
this.retryAfter ?? Date.now(),
Date.now() + retryAfterSecs * durations.SECOND
);
}
const response = await pTimeout(socket.request(options), timeout);
return response;
} catch (error) {
if (error instanceof RateLimitedError) {
if (error.retryAfterSecs > 0) {
this.retryAfter = Math.max(
this.retryAfter ?? Date.now(),
Date.now() + error.retryAfterSecs * durations.SECOND
);
}
}
throw error;
} finally {
log.info('CDSSocketManager: closing socket');
socket.close(3000, 'Normal');