Title: Avoid multiple refresh by zhiyuanliang-ms · Pull Request #136 · Azure/AppConfiguration-JavaScriptProvider · GitHub
Open Graph Title: Avoid multiple refresh by zhiyuanliang-ms · Pull Request #136 · Azure/AppConfiguration-JavaScriptProvider
X Title: Avoid multiple refresh by zhiyuanliang-ms · Pull Request #136 · Azure/AppConfiguration-JavaScriptProvider
Description: Why this PR? class Timer { #backoffEnd; // timestamp #interval; constructor(interval) { this.#interval = interval; this.#backoffEnd = Date.now(); } canRefresh() { return Date.now() >= this.#backoffEnd; } reset() { this.#backoffEnd = Date.now() + this.#interval; } } async function refresh() { await f(); } async function f() { if (!timer.canRefresh()) { console.log('timer rejects') return Promise.resolve(false); } console.log('f starts') await new Promise(resolve => setTimeout(resolve, 1000)) console.log('f ends') timer.reset() } for (let i = 0; i < 3; i++) { refresh(); } The above code snippet simulate the current implementation of refresh and it will produce the following output f starts f starts f starts f ends f ends f ends The timer fails to prevent refresh operation from being executed multiple times during refresh interval. Solution Except for the web worker scenario, javascript is in general single thread. For an async function, it will be synchronously executed until an await occurs, then it will give the control to the event loop. So this should be atomic. So the solution is to maintain a boolean variable refreshInProgress to indicate whether there is any ongoing refresh call. The check/setting of the flag will be done synchronously so it should be concurrent/"thread" safe.
Open Graph Description: Why this PR? class Timer { #backoffEnd; // timestamp #interval; constructor(interval) { this.#interval = interval; this.#backoffEnd = Date.now(); } canRefresh(...
X Description: Why this PR? class Timer { #backoffEnd; // timestamp #interval; constructor(interval) { this.#interval = interval; this.#backoffEnd = Date.now(); } canRefresh(...
Opengraph URL: https://github.com/Azure/AppConfiguration-JavaScriptProvider/pull/136
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:50235d0a-aa38-8061-bf05-ffadf2b55d1e |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 9148:1B57B6:A892F4:E6D3C6:698F002A |
| html-safe-nonce | 95e2453e6c053c265dea3674938e78048a4acdeff83cc7a39b7acfd6df13715d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MTQ4OjFCNTdCNjpBODkyRjQ6RTZEM0M2OjY5OEYwMDJBIiwidmlzaXRvcl9pZCI6IjIxMDg2ODAzOTQwNzMxMTI2MTgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | d115db006a42ba79df030ff8fed88dbe1805a110de614f91663b4708d2b4e380 |
| hovercard-subject-tag | pull_request:2205054137 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/Azure/AppConfiguration-JavaScriptProvider/pull/136/files |
| twitter:image | https://avatars.githubusercontent.com/u/141655842?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/141655842?s=400&v=4 |
| og:image:alt | Why this PR? class Timer { #backoffEnd; // timestamp #interval; constructor(interval) { this.#interval = interval; this.#backoffEnd = Date.now(); } canRefresh(... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 2da1a0d1318592c9965539b12269c4641177dfabfc86c3807992efb13e1d96ff |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/Azure/AppConfiguration-JavaScriptProvider git https://github.com/Azure/AppConfiguration-JavaScriptProvider.git |
| octolytics-dimension-user_id | 6844498 |
| octolytics-dimension-user_login | Azure |
| octolytics-dimension-repository_id | 688057518 |
| octolytics-dimension-repository_nwo | Azure/AppConfiguration-JavaScriptProvider |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 688057518 |
| octolytics-dimension-repository_network_root_nwo | Azure/AppConfiguration-JavaScriptProvider |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 66d26b1a7f81bd3ffe8d0f16abab43f6e64fd21a |
| ui-target | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width