|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import globals from '../extensionGlobals' |
| 7 | +import { globalKey } from '../globalState' |
| 8 | +import { getLogger } from '../logger/logger' |
| 9 | +import { waitUntil } from '../utilities/timeoutUtils' |
| 10 | + |
| 11 | +/** |
| 12 | + * args: |
| 13 | + * @member result: the actual resource type callers want to use |
| 14 | + * @member locked: readWriteLock, while the lock is acquired by one process, the other can't access to it until it's released by the previous |
| 15 | + * @member timestamp: used for determining the resource is stale or not |
| 16 | + */ |
| 17 | +interface Resource<V> { |
| 18 | + result: V | undefined |
| 19 | + locked: boolean |
| 20 | + timestamp: number |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * GlobalStates schema, which is used for vscode global states deserialization, [globals.globalState#tryGet<T>] etc. |
| 25 | + * The purpose of it is to allow devs to overload the resource into existing global key and no need to create a specific key for only this purpose. |
| 26 | + */ |
| 27 | +export interface GlobalStateSchema<V> { |
| 28 | + resource: Resource<V> |
| 29 | +} |
| 30 | + |
| 31 | +const logger = getLogger('resourceCache') |
| 32 | + |
| 33 | +function now() { |
| 34 | + return globals.clock.Date.now() |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * CacheResource utilizes VSCode global states API to cache resources which are expensive to get so that the result can be shared across multiple VSCode instances. |
| 39 | + * The first VSCode instance invoking #getResource will hold a lock and make the actual network call/FS read to pull the real response. |
| 40 | + * When the pull is done, the lock will be released and it then caches the result in the global states. Then the rest of instances can now acquire the lock 1 by 1 and read the resource from the cache. |
| 41 | + * |
| 42 | + * constructor: |
| 43 | + * @param key: global state key, which is used for globals.globalState#update, #tryGet etc. |
| 44 | + * @param expirationInMilli: cache expiration time in milli seconds |
| 45 | + * @param defaultValue: default value for the cache if the cache doesn't pre-exist in users' FS |
| 46 | + * @param waitUntilOption: waitUntil option for acquire lock |
| 47 | + * |
| 48 | + * methods: |
| 49 | + * @method resourceProvider: implementation needs to implement this method to obtain the latest resource either via network calls or FS read |
| 50 | + * @method getResource: obtain the resource from cache or pull the latest from the service if the cache either expires or doesn't exist |
| 51 | + */ |
| 52 | +export abstract class CachedResource<V> { |
| 53 | + constructor( |
| 54 | + private readonly key: globalKey, |
| 55 | + private readonly expirationInMilli: number, |
| 56 | + private readonly defaultValue: GlobalStateSchema<V>, |
| 57 | + private readonly waitUntilOption: { timeout: number; interval: number; truthy: boolean } |
| 58 | + ) {} |
| 59 | + |
| 60 | + abstract resourceProvider(): Promise<V> |
| 61 | + |
| 62 | + async getResource(): Promise<V> { |
| 63 | + const cachedValue = await this.tryLoadResourceAndLock() |
| 64 | + const resource = cachedValue?.resource |
| 65 | + |
| 66 | + // If cache is still fresh, return cached result, otherwise pull latest from the service |
| 67 | + if (cachedValue && resource && resource.result) { |
| 68 | + const duration = now() - resource.timestamp |
| 69 | + if (duration < this.expirationInMilli) { |
| 70 | + logger.debug( |
| 71 | + `cache hit, duration(%sms) is less than expiration(%sms), returning cached value %s`, |
| 72 | + duration, |
| 73 | + this.expirationInMilli, |
| 74 | + this.key |
| 75 | + ) |
| 76 | + // release the lock |
| 77 | + await this.releaseLock(resource, cachedValue) |
| 78 | + return resource.result |
| 79 | + } else { |
| 80 | + logger.debug( |
| 81 | + `cache is stale, duration(%sms) is older than expiration(%sms), pulling latest resource %s`, |
| 82 | + duration, |
| 83 | + this.expirationInMilli, |
| 84 | + this.key |
| 85 | + ) |
| 86 | + } |
| 87 | + } else { |
| 88 | + logger.info(`cache miss, pulling latest resource %s`, this.key) |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Possible paths here |
| 93 | + * 1. cache doesn't exist. |
| 94 | + * 2. cache exists but expired. |
| 95 | + * 3. lock is held by other process and the waiting time is greater than the specified waiting time |
| 96 | + */ |
| 97 | + try { |
| 98 | + // Make the real network call / FS read to pull the resource |
| 99 | + const latest = await this.resourceProvider() |
| 100 | + |
| 101 | + // Update resource cache and release the lock |
| 102 | + const r: Resource<V> = { |
| 103 | + locked: false, |
| 104 | + timestamp: now(), |
| 105 | + result: latest, |
| 106 | + } |
| 107 | + logger.info(`doen loading latest resource, updating resource cache: %s`, this.key) |
| 108 | + await this.releaseLock(r) |
| 109 | + return latest |
| 110 | + } catch (e) { |
| 111 | + logger.error(`failed to load latest resource, releasing lock: %s`, this.key) |
| 112 | + await this.releaseLock() |
| 113 | + throw e |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // This method will lock the resource so other callers have to wait until the lock is released, otherwise will return undefined if it times out |
| 118 | + private async tryLoadResourceAndLock(): Promise<GlobalStateSchema<V> | undefined> { |
| 119 | + const _acquireLock = async () => { |
| 120 | + const cachedValue = this.readCacheOrDefault() |
| 121 | + |
| 122 | + if (!cachedValue.resource.locked) { |
| 123 | + await this.lockResource(cachedValue) |
| 124 | + return cachedValue |
| 125 | + } |
| 126 | + |
| 127 | + return undefined |
| 128 | + } |
| 129 | + |
| 130 | + const lock = await waitUntil(async () => { |
| 131 | + const lock = await _acquireLock() |
| 132 | + logger.debug(`try obtain resource cache read write lock for resource %s`, this.key) |
| 133 | + if (lock) { |
| 134 | + return lock |
| 135 | + } |
| 136 | + }, this.waitUntilOption) |
| 137 | + |
| 138 | + return lock |
| 139 | + } |
| 140 | + |
| 141 | + async lockResource(baseCache: GlobalStateSchema<V>): Promise<void> { |
| 142 | + await this.updateResourceCache({ locked: true }, baseCache) |
| 143 | + } |
| 144 | + |
| 145 | + async releaseLock(): Promise<void> |
| 146 | + async releaseLock(resource: Partial<Resource<V>>): Promise<void> |
| 147 | + async releaseLock(resource: Partial<Resource<V>>, baseCache: GlobalStateSchema<V>): Promise<void> |
| 148 | + async releaseLock(resource?: Partial<Resource<V>>, baseCache?: GlobalStateSchema<V>): Promise<void> { |
| 149 | + if (!resource) { |
| 150 | + await this.updateResourceCache({ locked: false }, undefined) |
| 151 | + } else if (baseCache) { |
| 152 | + await this.updateResourceCache(resource, baseCache) |
| 153 | + } else { |
| 154 | + await this.updateResourceCache(resource, undefined) |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + async clearCache() { |
| 159 | + const baseCache = this.readCacheOrDefault() |
| 160 | + await this.updateResourceCache({ result: undefined, timestamp: 0, locked: false }, baseCache) |
| 161 | + } |
| 162 | + |
| 163 | + private async updateResourceCache(resource: Partial<Resource<any>>, cache: GlobalStateSchema<any> | undefined) { |
| 164 | + const baseCache = cache ?? this.readCacheOrDefault() |
| 165 | + |
| 166 | + const toUpdate: GlobalStateSchema<V> = { |
| 167 | + ...baseCache, |
| 168 | + resource: { |
| 169 | + ...baseCache.resource, |
| 170 | + ...resource, |
| 171 | + }, |
| 172 | + } |
| 173 | + |
| 174 | + await globals.globalState.update(this.key, toUpdate) |
| 175 | + } |
| 176 | + |
| 177 | + private readCacheOrDefault(): GlobalStateSchema<V> { |
| 178 | + const cachedValue = globals.globalState.tryGet<GlobalStateSchema<V>>(this.key, Object, { |
| 179 | + ...this.defaultValue, |
| 180 | + resource: { |
| 181 | + ...this.defaultValue.resource, |
| 182 | + locked: false, |
| 183 | + result: undefined, |
| 184 | + timestamp: 0, |
| 185 | + }, |
| 186 | + }) |
| 187 | + |
| 188 | + return cachedValue |
| 189 | + } |
| 190 | +} |
0 commit comments