-
Notifications
You must be signed in to change notification settings - Fork 92
perf: use conditional blob gets if same blob was fetched before for previous requests #3218
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
Changes from all commits
89a536f
4f2575b
061e18b
a1d62a0
9052763
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 |
|---|---|---|
|
|
@@ -25,16 +25,48 @@ export const getMemoizedKeyValueStoreBackedByRegionalBlobStore = ( | |
| const inMemoryCache = getRequestScopedInMemoryCache() | ||
|
|
||
| const memoizedValue = inMemoryCache.get(key) | ||
| if (typeof memoizedValue !== 'undefined') { | ||
| return memoizedValue as T | null | Promise<T | null> | ||
| if ( | ||
| memoizedValue?.conditional === false && | ||
| typeof memoizedValue?.currentRequestValue !== 'undefined' | ||
| ) { | ||
| return memoizedValue.currentRequestValue as T | null | Promise<T | null> | ||
|
Comment on lines
+28
to
+32
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.
|
||
| } | ||
|
|
||
| const blobKey = await encodeBlobKey(key) | ||
| const getPromise = withActiveSpan(tracer, otelSpanTitle, async (span) => { | ||
| span?.setAttributes({ key, blobKey }) | ||
| const blob = (await store.get(blobKey, { type: 'json' })) as T | null | ||
| inMemoryCache.set(key, blob) | ||
| span?.addEvent(blob ? 'Hit' : 'Miss') | ||
|
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. I converted from adding event to setting |
||
| const { etag: previousEtag, globalValue: previousBlob } = memoizedValue?.conditional | ||
| ? memoizedValue | ||
| : {} | ||
|
|
||
| span?.setAttributes({ key, blobKey, previousEtag }) | ||
|
|
||
| const result = await store.getWithMetadata(blobKey, { | ||
| type: 'json', | ||
| etag: previousEtag, | ||
| }) | ||
|
|
||
| const shouldReuseMemoizedBlob = result?.etag && previousEtag === result?.etag | ||
|
|
||
| const blob = (shouldReuseMemoizedBlob ? previousBlob : result?.data) as T | null | ||
|
|
||
| if (result?.etag && blob) { | ||
| inMemoryCache.set(key, { | ||
| data: blob, | ||
| etag: result?.etag, | ||
| }) | ||
| } else { | ||
| // if we don't get blob (null) or etag for some reason is missing, | ||
| // we still want to store resolved blob value so that it could be reused | ||
| // within the same request | ||
| inMemoryCache.set(key, blob) | ||
| } | ||
|
|
||
| span?.setAttributes({ | ||
| etag: result?.etag, | ||
| reusingPreviouslyFetchedBlob: shouldReuseMemoizedBlob, | ||
| status: blob ? (shouldReuseMemoizedBlob ? 'Hit, no change' : 'Hit') : 'Miss', | ||
|
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. this is pretty much modeling 304, 200, 404 response status codes from underlying blobs response hopefully this is temporary until we can nicely "merge" next-runtime produced span with blobs spans to avoid almost duplication (those spans below represent same thing) |
||
| }) | ||
|
|
||
| return blob | ||
| }) | ||
| inMemoryCache.set(key, getPromise) | ||
|
|
@@ -48,7 +80,14 @@ export const getMemoizedKeyValueStoreBackedByRegionalBlobStore = ( | |
| const blobKey = await encodeBlobKey(key) | ||
| return withActiveSpan(tracer, otelSpanTitle, async (span) => { | ||
| span?.setAttributes({ key, blobKey }) | ||
| return await store.setJSON(blobKey, value) | ||
| const writeResult = await store.setJSON(blobKey, value) | ||
| if (writeResult?.etag) { | ||
| inMemoryCache.set(key, { | ||
| data: value, | ||
| etag: writeResult.etag, | ||
| }) | ||
| } | ||
| return writeResult | ||
| }) | ||
| }, | ||
| } | ||
|
|
||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because of sindresorhus/eslint-plugin-unicorn#2604 where prettier is getting rid of extra parenthesis needed to satisfy this rule