From 73eb2044da903fbaa1b2cd3ee95341eaa8052ee1 Mon Sep 17 00:00:00 2001 From: Dmitry Leleko Date: Thu, 30 Jun 2022 14:47:04 +0300 Subject: [PATCH 1/6] OP-2481 Fix ws subscription issue --- src/services/OrionAggregator/ws/index.ts | 4 ++-- src/services/PriceFeed/ws/PriceFeedSubscription.ts | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/services/OrionAggregator/ws/index.ts b/src/services/OrionAggregator/ws/index.ts index 27611e2..4ea7e0b 100644 --- a/src/services/OrionAggregator/ws/index.ts +++ b/src/services/OrionAggregator/ws/index.ts @@ -254,14 +254,14 @@ class OrionAggregatorWS { } destroy() { - this.ws?.close(4000); + this.ws?.close(); delete this.ws; } init(isReconnect = false) { this.ws = new WebSocket(this.wsUrl); this.ws.onclose = (e) => { - if (e.code !== 4000) this.init(true); + if (this.ws) this.init(true); }; this.ws.onopen = () => { // Re-subscribe to all subscriptions diff --git a/src/services/PriceFeed/ws/PriceFeedSubscription.ts b/src/services/PriceFeed/ws/PriceFeedSubscription.ts index 4530915..513f9f8 100644 --- a/src/services/PriceFeed/ws/PriceFeedSubscription.ts +++ b/src/services/PriceFeed/ws/PriceFeedSubscription.ts @@ -108,7 +108,7 @@ export default class PriceFeedSubscription { if (this.heartbeatInterval) clearInterval(this.heartbeatInterval); - if (e.code !== 4000) this.init(); + if (this.ws) this.init(); }; this.heartbeatInterval = setInterval(() => { @@ -117,6 +117,7 @@ export default class PriceFeedSubscription Date: Thu, 30 Jun 2022 15:08:07 +0300 Subject: [PATCH 2/6] OP-2481 Fixup --- src/services/OrionAggregator/ws/index.ts | 5 ++++- src/services/PriceFeed/ws/PriceFeedSubscription.ts | 6 ++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/services/OrionAggregator/ws/index.ts b/src/services/OrionAggregator/ws/index.ts index 4ea7e0b..db8e06b 100644 --- a/src/services/OrionAggregator/ws/index.ts +++ b/src/services/OrionAggregator/ws/index.ts @@ -144,6 +144,8 @@ const isSubType = (subType: string): subType is keyof Subscription => Object.val class OrionAggregatorWS { private ws: WebSocket | undefined; + private isClosedIntentionally: boolean = false; + private subscriptions: Partial<{ [K in keyof Subscription]: Partial> }> = {}; @@ -254,6 +256,7 @@ class OrionAggregatorWS { } destroy() { + this.isClosedIntentionally = true; this.ws?.close(); delete this.ws; } @@ -261,7 +264,7 @@ class OrionAggregatorWS { init(isReconnect = false) { this.ws = new WebSocket(this.wsUrl); this.ws.onclose = (e) => { - if (this.ws) this.init(true); + if (!this.isClosedIntentionally) this.init(true); }; this.ws.onopen = () => { // Re-subscribe to all subscriptions diff --git a/src/services/PriceFeed/ws/PriceFeedSubscription.ts b/src/services/PriceFeed/ws/PriceFeedSubscription.ts index 513f9f8..36f257b 100644 --- a/src/services/PriceFeed/ws/PriceFeedSubscription.ts +++ b/src/services/PriceFeed/ws/PriceFeedSubscription.ts @@ -74,6 +74,8 @@ export default class PriceFeedSubscription { if (this.heartbeatInterval) clearInterval(this.heartbeatInterval); - if (this.ws) this.init(); + if (!this.isClosedIntentionally) this.init(); }; this.heartbeatInterval = setInterval(() => { @@ -117,7 +119,7 @@ export default class PriceFeedSubscription Date: Thu, 30 Jun 2022 15:21:35 +0300 Subject: [PATCH 3/6] OP-2481 Cleanup --- src/services/OrionAggregator/ws/index.ts | 4 ++-- src/services/PriceFeed/ws/PriceFeedSubscription.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/services/OrionAggregator/ws/index.ts b/src/services/OrionAggregator/ws/index.ts index db8e06b..5432043 100644 --- a/src/services/OrionAggregator/ws/index.ts +++ b/src/services/OrionAggregator/ws/index.ts @@ -144,7 +144,7 @@ const isSubType = (subType: string): subType is keyof Subscription => Object.val class OrionAggregatorWS { private ws: WebSocket | undefined; - private isClosedIntentionally: boolean = false; + private isClosedIntentionally = false; private subscriptions: Partial<{ [K in keyof Subscription]: Partial> @@ -263,7 +263,7 @@ class OrionAggregatorWS { init(isReconnect = false) { this.ws = new WebSocket(this.wsUrl); - this.ws.onclose = (e) => { + this.ws.onclose = () => { if (!this.isClosedIntentionally) this.init(true); }; this.ws.onopen = () => { diff --git a/src/services/PriceFeed/ws/PriceFeedSubscription.ts b/src/services/PriceFeed/ws/PriceFeedSubscription.ts index 36f257b..116fe15 100644 --- a/src/services/PriceFeed/ws/PriceFeedSubscription.ts +++ b/src/services/PriceFeed/ws/PriceFeedSubscription.ts @@ -74,7 +74,7 @@ export default class PriceFeedSubscription { + this.ws.onclose = () => { if (this.heartbeatInterval) clearInterval(this.heartbeatInterval); if (!this.isClosedIntentionally) this.init(); }; From 2976f422c4f4575d1d3855e8ccc54965e817b90d Mon Sep 17 00:00:00 2001 From: Dmitry Leleko Date: Thu, 30 Jun 2022 15:39:58 +0300 Subject: [PATCH 4/6] OP-2481 Fixup --- src/services/OrionAggregator/ws/index.ts | 1 + src/services/PriceFeed/ws/PriceFeedSubscription.ts | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/services/OrionAggregator/ws/index.ts b/src/services/OrionAggregator/ws/index.ts index 5432043..5303ef6 100644 --- a/src/services/OrionAggregator/ws/index.ts +++ b/src/services/OrionAggregator/ws/index.ts @@ -262,6 +262,7 @@ class OrionAggregatorWS { } init(isReconnect = false) { + this.isClosedIntentionally = false; this.ws = new WebSocket(this.wsUrl); this.ws.onclose = () => { if (!this.isClosedIntentionally) this.init(true); diff --git a/src/services/PriceFeed/ws/PriceFeedSubscription.ts b/src/services/PriceFeed/ws/PriceFeedSubscription.ts index 116fe15..55bd87d 100644 --- a/src/services/PriceFeed/ws/PriceFeedSubscription.ts +++ b/src/services/PriceFeed/ws/PriceFeedSubscription.ts @@ -93,6 +93,8 @@ export default class PriceFeedSubscription Date: Thu, 30 Jun 2022 16:12:27 +0300 Subject: [PATCH 5/6] OP-2481 Minor update --- src/services/OrionAggregator/ws/index.ts | 4 ++++ src/services/PriceFeed/ws/PriceFeedSubscription.ts | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/src/services/OrionAggregator/ws/index.ts b/src/services/OrionAggregator/ws/index.ts index 5303ef6..faff4f4 100644 --- a/src/services/OrionAggregator/ws/index.ts +++ b/src/services/OrionAggregator/ws/index.ts @@ -144,6 +144,10 @@ const isSubType = (subType: string): subType is keyof Subscription => Object.val class OrionAggregatorWS { private ws: WebSocket | undefined; + // is used to make sure we do not need to renew ws subscription + // we can not be sure that onclose event will recieve our code when we do `ws.close(4000)` + // since sometimes it can be replaced with system one. + // https://stackoverflow.com/questions/19304157/getting-the-reason-why-websockets-closed-with-close-code-1006 private isClosedIntentionally = false; private subscriptions: Partial<{ diff --git a/src/services/PriceFeed/ws/PriceFeedSubscription.ts b/src/services/PriceFeed/ws/PriceFeedSubscription.ts index 55bd87d..11574a5 100644 --- a/src/services/PriceFeed/ws/PriceFeedSubscription.ts +++ b/src/services/PriceFeed/ws/PriceFeedSubscription.ts @@ -74,6 +74,10 @@ export default class PriceFeedSubscription Date: Thu, 30 Jun 2022 16:17:24 +0300 Subject: [PATCH 6/6] OP-2481 Bump version to 0.12.8 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 99347fa..d377d7b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.12.7", + "version": "0.12.8", "description": "Orion Protocol SDK", "main": "./lib/esm/index.js", "module": "./lib/esm/index.js",