-
Notifications
You must be signed in to change notification settings - Fork 0
Move assignment cache to IndexedDB, scoped by clientToken #188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,26 @@ | ||
| import type { AssignmentCache } from '@datadog/flagging-core' | ||
| import ChromeStorageAssignmentCache from './chrome-storage-assignment-cache' | ||
| import { hasWindowLocalStorage } from './helpers' | ||
| import { hasIndexedDB } from './helpers' | ||
| import HybridAssignmentCache from './hybrid-assignment-cache' | ||
| import { LocalStorageAssignmentCache } from './local-storage-assignment-cache' | ||
| import { IndexedDBAssignmentCache } from './indexeddb-assignment-cache' | ||
| import SimpleAssignmentCache from './simple-assignment-cache' | ||
|
|
||
| export function assignmentCacheFactory({ | ||
| forceMemoryOnly = false, | ||
| chromeStorage, | ||
| storageKeySuffix, | ||
| clientToken, | ||
| }: { | ||
| forceMemoryOnly?: boolean | ||
| storageKeySuffix: string | ||
| chromeStorage?: chrome.storage.StorageArea | ||
| clientToken: string | ||
| }): AssignmentCache { | ||
| const simpleCache = new SimpleAssignmentCache() | ||
|
|
||
| if (forceMemoryOnly) { | ||
| return simpleCache | ||
| } | ||
|
|
||
| if (chromeStorage) { | ||
| const chromeStorageCache = new ChromeStorageAssignmentCache(chromeStorage) | ||
| return new HybridAssignmentCache(simpleCache, chromeStorageCache) | ||
| } else { | ||
| if (hasWindowLocalStorage()) { | ||
| const localStorageCache = new LocalStorageAssignmentCache(storageKeySuffix) | ||
| return new HybridAssignmentCache(simpleCache, localStorageCache) | ||
| } else { | ||
| return simpleCache | ||
| } | ||
| if (hasIndexedDB()) { | ||
| const indexedDBCache = new IndexedDBAssignmentCache(clientToken) | ||
| return new HybridAssignmentCache(simpleCache, indexedDBCache) | ||
| } | ||
|
|
||
| return simpleCache | ||
| } | ||
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| import { | ||
| type AssignmentCacheEntry, | ||
| assignmentCacheKeyToString, | ||
| assignmentCacheValueToString, | ||
| buildStorageKeySuffix, | ||
| } from '@datadog/flagging-core' | ||
|
|
||
| import type { BulkReadAssignmentCache } from './hybrid-assignment-cache' | ||
| import { openDB, STORE_NAME } from './indexeddb-store' | ||
|
|
||
| export class IndexedDBAssignmentCache implements BulkReadAssignmentCache { | ||
| private readonly storageKey: string | ||
| private readonly mirror: Map<string, string> = new Map() | ||
| private persistScheduled = false | ||
|
|
||
| constructor(clientToken: string) { | ||
| this.storageKey = `assignments-${buildStorageKeySuffix(clientToken)}` | ||
| } | ||
|
|
||
| /** No-op — IndexedDB entries are loaded lazily via getEntries(). */ | ||
| init(): Promise<void> { | ||
| return Promise.resolve() | ||
| } | ||
|
|
||
| /** Fire-and-forget persist to IndexedDB. Never blocks the caller, never throws. */ | ||
| set(entry: AssignmentCacheEntry): void { | ||
| const key = assignmentCacheKeyToString(entry) | ||
| const value = assignmentCacheValueToString(entry) | ||
| this.mirror.set(key, value) | ||
| this.persist() | ||
| } | ||
leoromanovsky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| has(_entry: AssignmentCacheEntry): boolean { | ||
| throw new Error('This should never be called for IndexedDBAssignmentCache, use getEntries() instead.') | ||
| } | ||
|
|
||
| /** Read all persisted entries. Returns [] on any error — never throws. */ | ||
| async getEntries(): Promise<[string, string][]> { | ||
| try { | ||
| const db = await openDB() | ||
| try { | ||
| const entries = await new Promise<[string, string][] | undefined>((resolve, reject) => { | ||
| const tx = db.transaction(STORE_NAME, 'readonly') | ||
| const store = tx.objectStore(STORE_NAME) | ||
| const request = store.get(this.storageKey) | ||
| request.onsuccess = () => resolve(request.result as [string, string][] | undefined) | ||
| request.onerror = () => reject(request.error) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor: you could also add |
||
| }) | ||
| if (Array.isArray(entries)) { | ||
| this.mirror.clear() | ||
| for (const [k, v] of entries) { | ||
| this.mirror.set(k, v) | ||
| } | ||
| return entries | ||
| } | ||
|
Comment on lines
+50
to
+56
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential race condition between
which would cause data loss. The I haven't looked much further, so it's possible that these methods are used in a way that this race condition isn't a concern, but perhaps worth taking a second look.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for the careful review; will take another look at this. |
||
| } finally { | ||
| db.close() | ||
| } | ||
| } catch { | ||
| // Silently fail — persistence should never break the SDK | ||
| } | ||
| return [] | ||
| } | ||
|
|
||
| /** Remove persisted entries. Never throws. */ | ||
| async clear(): Promise<void> { | ||
| this.mirror.clear() | ||
| try { | ||
| const db = await openDB() | ||
| try { | ||
| await new Promise<void>((resolve, reject) => { | ||
| const tx = db.transaction(STORE_NAME, 'readwrite') | ||
| const store = tx.objectStore(STORE_NAME) | ||
| store.delete(this.storageKey) | ||
| tx.oncomplete = () => resolve() | ||
| tx.onerror = () => reject(tx.error) | ||
| tx.onabort = () => reject(tx.error) | ||
| }) | ||
| } finally { | ||
| db.close() | ||
| } | ||
| } catch { | ||
| // Silently fail | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Schedule a fire-and-forget write for the next microtask. Coalesces rapid set() calls | ||
| * into a single IDB transaction, avoiding O(n^2) write volume and out-of-order races. | ||
| */ | ||
| private persist(): void { | ||
| if (this.persistScheduled) { | ||
| return | ||
| } | ||
| this.persistScheduled = true | ||
| queueMicrotask(() => { | ||
| this.persistScheduled = false | ||
| const entries = Array.from(this.mirror.entries()) | ||
| openDB() | ||
| .then((db) => { | ||
| const tx = db.transaction(STORE_NAME, 'readwrite') | ||
| const store = tx.objectStore(STORE_NAME) | ||
| store.put(entries, this.storageKey) | ||
| tx.oncomplete = () => db.close() | ||
| tx.onerror = () => db.close() | ||
| tx.onabort = () => db.close() | ||
| }) | ||
| .catch(() => { | ||
| // Silently fail — persistence should never break the SDK | ||
| }) | ||
| }) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| export const DB_NAME = 'dd-flagging' | ||
| export const DB_VERSION = 1 | ||
| export const STORE_NAME = 'configurations' | ||
|
|
||
| export function openDB(): Promise<IDBDatabase> { | ||
| return new Promise((resolve, reject) => { | ||
| const request = indexedDB.open(DB_NAME, DB_VERSION) | ||
| request.onupgradeneeded = () => { | ||
| const db = request.result | ||
| if (!db.objectStoreNames.contains(STORE_NAME)) { | ||
| db.createObjectStore(STORE_NAME) | ||
| } | ||
| } | ||
| request.onsuccess = () => resolve(request.result) | ||
| request.onerror = () => reject(request.error) | ||
| }) | ||
| } |
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.