Skip to content

Commit

Permalink
fix(subscriptions): Do not retry mqtt disconnections (#319)
Browse files Browse the repository at this point in the history
- If auto-reconnect when coming back online is needed, users should use  `client.sync` (deltaSync) instead.
  • Loading branch information
manueliglesias authored Jan 11, 2019
1 parent 8dbe01d commit b933379
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
6 changes: 6 additions & 0 deletions packages/aws-appsync/src/link/retry-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const MAX_DELAY_MS = 5 * 60 * 1000;
const getDelay = count => ((2 ** count) * BASE_TIME_MS) + (JITTER_FACTOR * Math.random());

export const SKIP_RETRY_KEY = '@@skipRetry';
export const PERMANENT_ERROR_KEY = typeof Symbol !== 'undefined' ? Symbol('permanentError') : '@@permanentError';

export const getEffectDelay = (_action: OfflineAction, retries: number) => {
const delay = getDelay(retries);
Expand All @@ -30,8 +31,13 @@ export const createRetryLink = (origLink: ApolloLink) => {

const retryLink = new RetryLink({
attempts: (count, operation, error) => {
const { [PERMANENT_ERROR_KEY]: permanent = false } = error;
const { [SKIP_RETRY_KEY]: skipRetry = false } = operation.variables;

if (permanent) {
return false;
}

if (error.statusCode >= 400 && error.statusCode < 500) {
return false;
}
Expand Down
13 changes: 7 additions & 6 deletions packages/aws-appsync/src/link/subscription-handshake-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import * as Paho from '../vendor/paho-mqtt';
import { ApolloError } from "apollo-client";
import { FieldNode } from "graphql";
import { getMainDefinition } from "apollo-utilities";
import { PERMANENT_ERROR_KEY } from "./retry-link";

const logger = rootLogger.extend('subscriptions');
const mqttLogger = logger.extend('mqtt');
Expand Down Expand Up @@ -66,11 +67,11 @@ export class SubscriptionHandshakeLink extends ApolloLink {
} = { subscription: { newSubscriptions: {}, mqttConnections: [] } },
errors = [],
}: {
extensions?: {
subscription: SubscriptionExtension
},
errors: any[]
} = subsInfo;
extensions?: {
subscription: SubscriptionExtension
},
errors: any[]
} = subsInfo;

if (errors && errors.length) {
return new Observable(observer => {
Expand Down Expand Up @@ -170,7 +171,7 @@ export class SubscriptionHandshakeLink extends ApolloLink {
if (errorCode !== 0) {
topics.forEach(t => {
if (this.topicObservers.has(t)) {
this.topicObservers.get(t).forEach(observer => observer.error(args));
this.topicObservers.get(t).forEach(observer => observer.error({ ...args, [PERMANENT_ERROR_KEY]: true }));
}
});
}
Expand Down

0 comments on commit b933379

Please sign in to comment.