v2.3.0: fix(#158), disable time sync by default. Optionally avoid hitting time API when connecting authenticated websocket.

This commit is contained in:
tiagosiebler
2022-07-12 11:32:17 +01:00
parent 7c531161b5
commit 43c1f91b50
18 changed files with 50 additions and 60 deletions

View File

@@ -61,10 +61,13 @@ export default abstract class BaseRestClient {
this.options = {
recv_window: 5000,
// how often to sync time drift with bybit servers
sync_interval_ms: 3600000,
// if true, we'll throw errors if any params are undefined
/** Throw errors if any params are undefined */
strict_param_validation: false,
/** Disable time sync by default */
enable_time_sync: false,
/** How often to sync time drift with bybit servers (if time sync is enabled) */
sync_interval_ms: 3600000,
...options,
};
@@ -86,7 +89,7 @@ export default abstract class BaseRestClient {
);
}
if (this.options.disable_time_sync !== true) {
if (this.options.enable_time_sync) {
this.syncTime();
setInterval(this.syncTime.bind(this), +this.options.sync_interval_ms!);
}
@@ -254,10 +257,11 @@ export default abstract class BaseRestClient {
}
/**
* Trigger time sync and store promise
* Trigger time sync and store promise. Use force: true, if automatic time sync is disabled
*/
private syncTime(): Promise<any> {
if (this.options.disable_time_sync === true) {
private syncTime(force?: boolean): Promise<any> {
if (!force && !this.options.enable_time_sync) {
this.timeOffset = 0;
return Promise.resolve(false);
}

View File

@@ -1,21 +1,26 @@
export interface RestClientOptions {
// override the max size of the request window (in ms)
/** Override the max size of the request window (in ms) */
recv_window?: number;
// how often to sync time drift with bybit servers
sync_interval_ms?: number | string;
// Default: false. Disable above sync mechanism if true.
/** @deprecated Time sync is now disabled by default. To re-enable it, use enable_time_sync instead. */
disable_time_sync?: boolean;
// Default: false. If true, we'll throw errors if any params are undefined
/** Disabled by default. This can help on machines with consistent latency problems. */
enable_time_sync?: boolean;
/** How often to sync time drift with bybit servers */
sync_interval_ms?: number | string;
/** Default: false. If true, we'll throw errors if any params are undefined */
strict_param_validation?: boolean;
// Optionally override API protocol + domain
// e.g 'https://api.bytick.com'
/**
* Optionally override API protocol + domain
* e.g baseUrl: 'https://api.bytick.com'
**/
baseUrl?: string;
// Default: true. whether to try and post-process request exceptions.
/** Default: true. whether to try and post-process request exceptions. */
parse_exceptions?: boolean;
}

View File

@@ -4,6 +4,7 @@ import WebSocket from 'isomorphic-ws';
import { InverseClient } from './inverse-client';
import { LinearClient } from './linear-client';
import { DefaultLogger } from './logger';
import { SpotClient } from './spot-client';
import { KlineInterval } from './types/shared';
import { signMessage } from './util/node-support';
import {
@@ -148,6 +149,8 @@ export interface WSClientConfigurableOptions {
restOptions?: RestClientOptions;
requestOptions?: any;
wsUrl?: string;
/** If true, fetch server time before trying to authenticate (disabled by default) */
fetchTimeOffsetBeforeAuth?: boolean;
}
export interface WebsocketClientOptions extends WSClientConfigurableOptions {
@@ -229,7 +232,7 @@ function resolveMarket(options: WSClientConfigurableOptions): APIMarket {
export class WebsocketClient extends EventEmitter {
private logger: typeof DefaultLogger;
private restClient: InverseClient | LinearClient;
private restClient: InverseClient | LinearClient | SpotClient;
private options: WebsocketClientOptions;
private wsStore: WsStore;
@@ -247,6 +250,7 @@ export class WebsocketClient extends EventEmitter {
pongTimeout: 1000,
pingInterval: 10000,
reconnectTimeout: 500,
fetchTimeOffsetBeforeAuth: false,
...options,
};
@@ -263,8 +267,7 @@ export class WebsocketClient extends EventEmitter {
this.options.requestOptions
);
} else if (this.isSpot()) {
// TODO: spot client
this.restClient = new LinearClient(
this.restClient = new SpotClient(
undefined,
undefined,
this.isLivenet(),
@@ -474,7 +477,9 @@ export class WebsocketClient extends EventEmitter {
wsKey,
});
const timeOffset = await this.restClient.fetchTimeOffset();
const timeOffset = this.options.fetchTimeOffsetBeforeAuth
? await this.restClient.fetchTimeOffset()
: 0;
const params: any = {
api_key: this.options.key,