Repository navigation
fix: deduplicate concurrent Solid document loads #349
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
Open
SharonStrats
wants to merge
1
commit into
staging
Choose a base branch
from
fix/429
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import type { Fetcher, NamedNode } from 'rdflib' | ||
|
|
||
| type LoadTarget = Parameters<Fetcher['load']>[0] | ||
| type IdentityTransition = () => void | ||
|
|
||
| function plainLoadKey (target: LoadTarget, options: unknown): string | undefined { | ||
| if (options !== undefined && (typeof options !== 'object' || options === null || Object.keys(options).length > 0)) { | ||
| return undefined | ||
| } | ||
| const uri = typeof target === 'string' | ||
| ? target | ||
| : (target as NamedNode | undefined)?.termType === 'NamedNode' ? (target as NamedNode).value : undefined | ||
| return uri?.split('#')[0] | ||
| } | ||
|
|
||
| /** | ||
| * Concurrent plain `load(doc)` calls share one request. | ||
| * | ||
| * rdflib already shares in-flight loads, except when it upgrades a load to | ||
| * `force` itself: after login every earlier answer is flagged out-of-date, so | ||
| * each startup caller of e.g. the profile document sent its own GET. Calls | ||
| * with options (force, headers, ...) are not shared, and an entry only lives | ||
| * while its request is pending, so later loads behave as before. | ||
| */ | ||
| export function shareConcurrentLoads (fetcher: Fetcher): IdentityTransition { | ||
| const originalLoad = fetcher.load.bind(fetcher) | ||
| const pending = new Map<string, Promise<unknown>>() | ||
|
|
||
| fetcher.load = function (target: LoadTarget, options?: unknown) { | ||
| if (Array.isArray(target)) { | ||
| return Promise.all(target.map(member => (originalLoad as any)(member, options))) as any | ||
| } | ||
| const key = plainLoadKey(target, options) | ||
| if (!key) return (originalLoad as any)(target, options) | ||
|
|
||
| const existing = pending.get(key) | ||
| if (existing) return existing as any | ||
|
SharonStrats marked this conversation as resolved.
Resolved
|
||
|
|
||
| const request = Promise.resolve((originalLoad as any)(target, options)) | ||
| .finally(() => { | ||
| if (pending.get(key) === request) pending.delete(key) | ||
| }) | ||
| pending.set(key, request) | ||
| return request as any | ||
| } as Fetcher['load'] | ||
|
|
||
| return () => { | ||
| pending.clear() | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import * as rdf from 'rdflib' | ||
| import { shareConcurrentLoads } from '../src/util/shareConcurrentLoads' | ||
|
|
||
| function deferred () { | ||
| let resolve!: (value: unknown) => void | ||
| let reject!: (error: unknown) => void | ||
| const promise = new Promise((res, rej) => { resolve = res; reject = rej }) | ||
| return { promise, resolve, reject } | ||
| } | ||
|
|
||
| function fetcherWith (load: (...args: unknown[]) => Promise<unknown>) { | ||
| const fetcher = { load } as unknown as rdf.Fetcher | ||
| shareConcurrentLoads(fetcher) | ||
| return fetcher | ||
| } | ||
|
|
||
| describe('shareConcurrentLoads', () => { | ||
| const doc = rdf.sym('https://alice.example/profile/card') | ||
|
|
||
| it('shares one request between concurrent plain loads of the same document', async () => { | ||
| const response = deferred() | ||
| const load = vi.fn(() => response.promise) | ||
| const fetcher = fetcherWith(load) | ||
|
|
||
| const first = fetcher.load(doc) | ||
| const second = fetcher.load(rdf.sym('https://alice.example/profile/card#me').doc()) | ||
| const third = fetcher.load('https://alice.example/profile/card#me', {}) | ||
| response.resolve('ok') | ||
|
|
||
| await expect(Promise.all([first, second, third])).resolves.toEqual(['ok', 'ok', 'ok']) | ||
| expect(load).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| it('loads again once the shared request has settled', async () => { | ||
| const load = vi.fn(async () => 'ok') | ||
| const fetcher = fetcherWith(load) | ||
|
|
||
| await fetcher.load(doc) | ||
| await fetcher.load(doc) | ||
|
|
||
| expect(load).toHaveBeenCalledTimes(2) | ||
| }) | ||
|
|
||
| it('lets a failed load be retried', async () => { | ||
| const load = vi.fn() | ||
| .mockRejectedValueOnce(new Error('offline')) | ||
| .mockResolvedValueOnce('ok') | ||
| const fetcher = fetcherWith(load) | ||
|
|
||
| await expect(fetcher.load(doc)).rejects.toThrow('offline') | ||
| await expect(fetcher.load(doc)).resolves.toBe('ok') | ||
| }) | ||
|
|
||
| it('does not share forced loads, loads with options, or batches', async () => { | ||
| const response = deferred() | ||
| const load = vi.fn(() => response.promise) | ||
| const fetcher = fetcherWith(load) | ||
|
|
||
| const plain = fetcher.load(doc) | ||
| const forced = fetcher.load(doc, { force: true }) | ||
| const withHeaders = fetcher.load(doc, { headers: { accept: 'text/turtle' } } as any) | ||
| const batch = fetcher.load([doc]) | ||
| response.resolve('ok') | ||
| await Promise.all([plain, forced, withHeaders, batch]) | ||
|
|
||
| expect(load).toHaveBeenCalledTimes(4) | ||
| }) | ||
|
|
||
| it('does not let a batch re-enter the sharing wrapper', async () => { | ||
| const load = vi.fn(async (target: unknown) => { | ||
| if (Array.isArray(target)) { | ||
| return Promise.all(target.map(member => (fetcher.load as any)(member))) | ||
| } | ||
| return target | ||
| }) | ||
| const fetcher = fetcherWith(load) | ||
|
|
||
| await expect(fetcher.load([doc, rdf.sym('https://bob.example/profile/card')])).resolves.toHaveLength(2) | ||
|
|
||
| expect(load).toHaveBeenCalledTimes(2) | ||
| expect(load.mock.calls.every(([target]) => !Array.isArray(target))).toBe(true) | ||
| }) | ||
|
|
||
| it('does not share a pending request across an identity transition', async () => { | ||
| const response = deferred() | ||
| const load = vi.fn(() => response.promise) | ||
| const fetcher = { load } as unknown as rdf.Fetcher | ||
| const invalidate = shareConcurrentLoads(fetcher) | ||
|
|
||
| const beforeTransition = fetcher.load(doc) | ||
| invalidate() | ||
| const afterTransition = fetcher.load(doc) | ||
| response.resolve('old identity') | ||
|
|
||
| expect(await beforeTransition).toBe('old identity') | ||
| expect(load).toHaveBeenCalledTimes(2) | ||
| expect(await afterTransition).toBe('old identity') | ||
| }) | ||
|
|
||
| it('does not share loads of different documents', async () => { | ||
| const load = vi.fn(async () => 'ok') | ||
| const fetcher = fetcherWith(load) | ||
|
|
||
| await Promise.all([fetcher.load(doc), fetcher.load(rdf.sym('https://bob.example/profile/card'))]) | ||
|
|
||
| expect(load).toHaveBeenCalledTimes(2) | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.