diff options
| author | Oskar Nyberg <oskar@mullvad.net> | 2021-07-06 19:16:53 +0200 |
|---|---|---|
| committer | Oskar Nyberg <oskar@mullvad.net> | 2021-07-08 13:23:30 +0200 |
| commit | 01b8b634786777369c22940f2790477bdece36b8 (patch) | |
| tree | 46b8c121f7194b4dfda05100fb73e7eb524efca1 | |
| parent | 80eb29423392d35d0add0392144aa40fa94fbf7e (diff) | |
| download | mullvadvpn-01b8b634786777369c22940f2790477bdece36b8.tar.xz mullvadvpn-01b8b634786777369c22940f2790477bdece36b8.zip | |
Lower account data cache validity time when account has expired
| -rw-r--r-- | gui/src/main/account-data-cache.ts | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/gui/src/main/account-data-cache.ts b/gui/src/main/account-data-cache.ts index 9a3df33d35..a79a934537 100644 --- a/gui/src/main/account-data-cache.ts +++ b/gui/src/main/account-data-cache.ts @@ -1,4 +1,4 @@ -import { closeToExpiry } from '../shared/account-expiry'; +import { closeToExpiry, hasExpired } from '../shared/account-expiry'; import { AccountToken, IAccountData, VoucherResponse } from '../shared/daemon-rpc-types'; import { DateComponent, dateByAddingComponent } from '../shared/date-helper'; import log from '../shared/logging'; @@ -11,6 +11,11 @@ interface IAccountFetchWatcher { onError: (error: Error) => void; } +// Account data is valid for 1 minute unless the account has expired. +const ACCOUNT_DATA_VALIDITY_SECONDS = 60_000; +// Account data is valid for 10 seconds if the account has expired. +const ACCOUNT_DATA_EXPIRED_VALIDITY_SECONDS = 10_000; + // An account data cache that helps to throttle RPC requests to get_account_data and retain the // cached value for 1 minute. export default class AccountDataCache { @@ -70,9 +75,9 @@ export default class AccountDataCache { } } - private setValue(value: IAccountData) { - this.validUntil = new Date(Date.now() + 60 * 1000); // 60s expiration - this.updateHandler(value); + private setValue(accountData: IAccountData) { + this.validUntil = this.getValidUntil(accountData); + this.updateHandler(accountData); this.notifyWatchers((watcher) => watcher.onFinish()); } @@ -80,6 +85,14 @@ export default class AccountDataCache { return this.validUntil && this.validUntil > new Date(); } + private getValidUntil(accountData: IAccountData): Date { + if (hasExpired(accountData.expiry)) { + return new Date(Date.now() + ACCOUNT_DATA_EXPIRED_VALIDITY_SECONDS); + } else { + return new Date(Date.now() + ACCOUNT_DATA_VALIDITY_SECONDS); + } + } + private async performFetch(accountToken: AccountToken) { this.performingFetch = true; try { |
