Skip to content

Commit

Permalink
FIX #2336 graphql replication creates increasing requests when offline
Browse files Browse the repository at this point in the history
  • Loading branch information
pubkey committed Jul 13, 2020
1 parent 5af5b2c commit 927d0d2
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

### 9.X.X (comming soon)

Bugfixes:
- GraphQL replication run increasing requests when offline [#2336](https://github.com/pubkey/rxdb/issues/2336)

### 9.3.0 (26 June 2020)

Features:
Expand Down
16 changes: 10 additions & 6 deletions src/plugins/replication-graphql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export class RxGraphQLReplicationState {
}

// ensures this._run() does not run in parallel
async run(): Promise<void> {
async run(retryOnFail = true): Promise<void> {
if (this.isStopped()) {
return;
}
Expand All @@ -158,7 +158,7 @@ export class RxGraphQLReplicationState {
this._runQueueCount++;
this._runningPromise = this._runningPromise.then(async () => {
this._subjects.active.next(true);
const willRetry = await this._run();
const willRetry = await this._run(retryOnFail);
this._subjects.active.next(false);
if (!willRetry && this._subjects.initialReplicationComplete['_value'] === false) {
this._subjects.initialReplicationComplete.next(true);
Expand All @@ -171,12 +171,12 @@ export class RxGraphQLReplicationState {
/**
* returns true if retry must be done
*/
async _run(): Promise<boolean> {
async _run(retryOnFail = true): Promise<boolean> {
this._runCount++;

if (this.push) {
const ok = await this.runPush();
if (!ok) {
if (!ok && retryOnFail) {
setTimeout(() => this.run(), this.retryTime);
/*
Because we assume that conflicts are solved on the server side,
Expand All @@ -189,7 +189,7 @@ export class RxGraphQLReplicationState {

if (this.pull) {
const ok = await this.runPull();
if (!ok) {
if (!ok && retryOnFail) {
setTimeout(() => this.run(), this.retryTime);
return true;
}
Expand Down Expand Up @@ -499,7 +499,11 @@ export function syncGraphQL(
while (!replicationState.isStopped()) {
await promiseWait(replicationState.liveInterval);
if (replicationState.isStopped()) return;
await replicationState.run();
await replicationState.run(
// do not retry on liveInterval-runs because they might stack up
// when failing
false
);
}
})();
}
Expand Down
47 changes: 47 additions & 0 deletions test/unit/replication-graphql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2256,6 +2256,53 @@ describe('replication-graphql.test.js', () => {
assert.ok(replicationState._runCount >= 9, replicationState._runCount.toString());
assert.ok(replicationState._runCount <= 11, replicationState._runCount.toString());

c.database.destroy();
});
it('#2336 liveInterval-retries should not stack up', async () => {
if (config.isFastMode()) {
// this test takes too long, do not run in fast mode
return;
}

const [c, server] = await Promise.all([
humansCollection.createHumanWithTimestamp(batchSize),
SpawnServer.spawn()
]);
const pullQueryBuilderFailing = (doc: any) => {
// Note: setHumanFail will error out
const query = `
mutation CreateHuman($human: HumanInput) {
setHumanFail(human: $human) {
id,
updatedAt
}
}
`;
const variables = {
human: doc
};

return {
query,
variables
};
};

const replicationState = c.syncGraphQL({
url: server.url,
pull: {
queryBuilder: pullQueryBuilderFailing
},
live: true,
deletedFlag: 'deleted',
retryTime: 1000,
liveInterval: 500
});

// Since push will error out we expect it there to be around 5000/500 = 10 runs with some retries
await AsyncTestUtil.wait(5000);
assert.ok(replicationState._runCount < 20, replicationState._runCount.toString());

c.database.destroy();
});
});
Expand Down

0 comments on commit 927d0d2

Please sign in to comment.