Skip to content

Commit

Permalink
fix(options): use exponential backoff on subscribe error retry
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Jan 15, 2024
1 parent 72d4df5 commit b17817e
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions packages/vue-apollo-option/src/smart-subscription.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import SmartApollo from './smart-apollo'

const MAX_RETRIES = 5
const DELAY_MS = 500

export default class SmartSubscription extends SmartApollo {
type = 'subscription'
vueApolloSpecialKeys = [
Expand All @@ -14,6 +17,8 @@ export default class SmartSubscription extends SmartApollo {
constructor (vm, key, options, autostart = true) {
super(vm, key, options)

this.attempts = 0

if (autostart) {
this.autostart()
}
Expand Down Expand Up @@ -73,6 +78,8 @@ export default class SmartSubscription extends SmartApollo {
nextResult (data) {
super.nextResult(data)

this.attempts = 0

if (typeof this.options.result === 'function') {
this.options.result.call(this.vm, data, this.key)
}
Expand All @@ -81,9 +88,19 @@ export default class SmartSubscription extends SmartApollo {
catchError (error) {
super.catchError(error)
// Restart the subscription
if (!this.skip) {
this.stop()
this.start()
if (this.skip || this.attempts >= MAX_RETRIES) {
return
}

this.stop()

// Restart the subscription with exponential backoff
this.retryTimeout = setTimeout(this.start.bind(this), Math.pow(2, this.attempts) * DELAY_MS)
this.attempts++
}

stop () {
super.stop()
clearTimeout(this.retryTimeout)
}
}

0 comments on commit b17817e

Please sign in to comment.