Skip to content

Commit b17817e

Browse files
committed
fix(options): use exponential backoff on subscribe error retry
1 parent 72d4df5 commit b17817e

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

packages/vue-apollo-option/src/smart-subscription.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import SmartApollo from './smart-apollo'
22

3+
const MAX_RETRIES = 5
4+
const DELAY_MS = 500
5+
36
export default class SmartSubscription extends SmartApollo {
47
type = 'subscription'
58
vueApolloSpecialKeys = [
@@ -14,6 +17,8 @@ export default class SmartSubscription extends SmartApollo {
1417
constructor (vm, key, options, autostart = true) {
1518
super(vm, key, options)
1619

20+
this.attempts = 0
21+
1722
if (autostart) {
1823
this.autostart()
1924
}
@@ -73,6 +78,8 @@ export default class SmartSubscription extends SmartApollo {
7378
nextResult (data) {
7479
super.nextResult(data)
7580

81+
this.attempts = 0
82+
7683
if (typeof this.options.result === 'function') {
7784
this.options.result.call(this.vm, data, this.key)
7885
}
@@ -81,9 +88,19 @@ export default class SmartSubscription extends SmartApollo {
8188
catchError (error) {
8289
super.catchError(error)
8390
// Restart the subscription
84-
if (!this.skip) {
85-
this.stop()
86-
this.start()
91+
if (this.skip || this.attempts >= MAX_RETRIES) {
92+
return
8793
}
94+
95+
this.stop()
96+
97+
// Restart the subscription with exponential backoff
98+
this.retryTimeout = setTimeout(this.start.bind(this), Math.pow(2, this.attempts) * DELAY_MS)
99+
this.attempts++
100+
}
101+
102+
stop () {
103+
super.stop()
104+
clearTimeout(this.retryTimeout)
88105
}
89106
}

0 commit comments

Comments
 (0)