-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLDR-16900 Limit frequency of announce and completion requests (#3583)
- Loading branch information
Showing
3 changed files
with
99 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* cldrSchedule: for Survey Tool scheduling of http requests. | ||
*/ | ||
|
||
export class FetchSchedule { | ||
/** | ||
* Construct a new FetchSchedule object | ||
* | ||
* @param {String} description description or name of caller | ||
* @param {Number} refreshSeconds postpone requests until this many seconds have elapsed since last request/response | ||
* @param {Boolean} debug whether to log debugging info to console | ||
* @returns the new FetchSchedule object | ||
*/ | ||
constructor(description, refreshSeconds, debug) { | ||
this.description = description; | ||
this.refreshMillis = refreshSeconds * 1000; | ||
this.debug = debug; | ||
this.lastRequestTime = this.lastResponseTime = 0; | ||
} | ||
|
||
setRequestTime() { | ||
this.lastRequestTime = Date.now(); | ||
if (this.debug) { | ||
console.log( | ||
this.description + " set lastRequestTime = " + this.lastRequestTime | ||
); | ||
} | ||
} | ||
|
||
setResponseTime() { | ||
this.lastResponseTime = Date.now(); | ||
if (this.debug) { | ||
console.log( | ||
this.description + " set lastResponseTime = " + this.lastResponseTime | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Is it too soon to make a request? | ||
* | ||
* @returns true if it is too soon, else false | ||
*/ | ||
tooSoon() { | ||
const now = Date.now(); | ||
if ( | ||
now < this.lastResponseTime + this.refreshMillis || | ||
now < this.lastRequestTime + this.refreshMillis | ||
) { | ||
if (this.debug) { | ||
console.log( | ||
this.description + | ||
" postponing request: less than " + | ||
this.refreshMillis / 1000 + | ||
" seconds elapsed; now = " + | ||
now | ||
); | ||
} | ||
return true; | ||
} else { | ||
if (this.debug) { | ||
console.log(this.description + " will make a request; now = " + now); | ||
} | ||
return false; | ||
} | ||
} | ||
} |