Skip to content

Commit 41f9a9b

Browse files
committed
Prevent multiple polling cycles to be scheduled at the same time
1 parent 69f09a4 commit 41f9a9b

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

lib/strategy/ordered.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export class OrderedStrategy {
5050
private polling: boolean;
5151
private toPoll: Heap<{ chain: RelationChain; expected: string[] }>;
5252
private pollInterval?: number;
53+
private pollingIsScheduled: boolean;
5354

5455
private cancled = false;
5556

@@ -70,6 +71,7 @@ export class OrderedStrategy {
7071
this.notifier = notifier;
7172
this.polling = polling;
7273
this.pollInterval = pollInterval;
74+
this.pollingIsScheduled = false;
7375

7476
this.toPoll = new Heap((a, b) => a.chain.ordering(b.chain));
7577
this.launchedRelations = new Heap((a, b) => a.ordering(b));
@@ -86,6 +88,7 @@ export class OrderedStrategy {
8688
this.notifier.error(error, {});
8789
},
8890
scheduleFetch: ({ target, expected }, { chain }) => {
91+
this.logger.debug(`Scheduling fetch for mutable page: ${target}`);
8992
chain.target = target;
9093
this.toPoll.push({ chain, expected });
9194
this.notifier.mutable({}, {});
@@ -257,11 +260,13 @@ export class OrderedStrategy {
257260
member = this.members.pop();
258261
}
259262

260-
if (this.polling) {
261-
this.logger.debug("Polling is enabled, settings timeout");
263+
// Make sure polling task is only scheduled once
264+
if (this.polling && !this.pollingIsScheduled) {
265+
this.logger.debug(`Polling is enabled, setting timeout of ${this.pollInterval || 1000} ms to poll`);
262266
setTimeout(() => {
263267
if (this.cancled) return;
264268

269+
this.pollingIsScheduled = false;
265270
this.notifier.pollCycle({}, {});
266271
const toPollArray = this.toPoll.toArray();
267272
this.logger.debug(
@@ -278,6 +283,7 @@ export class OrderedStrategy {
278283
this.modulator.push(rel);
279284
}
280285
}, this.pollInterval || 1000);
286+
this.pollingIsScheduled = true;
281287
} else {
282288
this.logger.debug("Closing the notifier, polling is not set");
283289
this.cancled = true;

lib/strategy/unordered.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export class UnorderedStrategy {
2121
private cacheList: Node[] = [];
2222
private polling: boolean;
2323
private pollInterval?: number;
24+
private pollingIsScheduled: boolean;
2425

2526
private canceled = false;
2627

@@ -39,6 +40,7 @@ export class UnorderedStrategy {
3940
this.manager = memberManager;
4041
this.fetcher = fetcher;
4142
this.polling = polling;
43+
this.pollingIsScheduled = false;
4244

4345
// Callbacks for the fetcher
4446
// - seen: the strategy wanted to fetch an uri, but it was already seen
@@ -52,6 +54,7 @@ export class UnorderedStrategy {
5254
this.notifier.error(error, {});
5355
},
5456
scheduleFetch: (node: Node) => {
57+
this.logger.debug(`Scheduling fetch for mutable page: ${node.target}`);
5558
this.cacheList.push(node);
5659
this.notifier.mutable({}, {});
5760
},
@@ -121,10 +124,13 @@ export class UnorderedStrategy {
121124
private checkEnd() {
122125
if (this.canceled) return;
123126
if (this.inFlight <= 0) {
124-
if (this.polling) {
127+
// Make sure we don't schedule multiple polling cycles
128+
if (this.polling && !this.pollingIsScheduled) {
129+
this.logger.debug(`Polling is enabled, setting timeout of ${this.pollInterval || 1000} ms to poll`);
125130
setTimeout(() => {
126131
if (this.canceled) return;
127132

133+
this.pollingIsScheduled = false;
128134
this.notifier.pollCycle({}, {});
129135
const cl = this.cacheList.slice();
130136
this.cacheList = [];
@@ -133,6 +139,7 @@ export class UnorderedStrategy {
133139
this.modulator.push(cache);
134140
}
135141
}, this.pollInterval || 1000);
142+
this.pollingIsScheduled = true;
136143
} else {
137144
this.logger.debug("Closing the notifier, polling is not set");
138145
this.canceled = true;

0 commit comments

Comments
 (0)