Skip to content
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

feat: add combined size pool #1848

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions common/reviews/api/core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -3113,6 +3113,8 @@ enum RequestType {
// (undocumented)
Interaction = "interaction",
// (undocumented)
Metadata = "metadata",
// (undocumented)
Prefetch = "prefetch",
// (undocumented)
Thumbnail = "thumbnail"
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/enums/RequestType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
* Request types for requesting images from the imageLoadPoolManager
*/
enum RequestType {
/** Highest priority for loading*/
/** Highest priority for loading as this is needed before rendering images */
Metadata = 'metadata',
/** Highest priority for loading once metadata has been fetched */
Interaction = 'interaction',
/** Second highest priority for loading*/
/** Second highest priority for loading of images */
Thumbnail = 'thumbnail',
/** Third highest priority for loading, usually used for image loading in the background*/
Prefetch = 'prefetch',
Expand Down
69 changes: 64 additions & 5 deletions packages/core/src/requestPool/requestPoolManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ type RequestPool = {
* You don't need to directly use the imageRetrievalPoolManager to load images
* since the imageLoadPoolManager will automatically use it for retrieval. However,
* maximum number of concurrent requests can be set by calling `setMaxConcurrentRequests`.
*
* ### Requests between types
* There are different types of requests, and each type starves the next request
* type, under the assumption that they are needed in order for fetching.
* The total number of requests is set by the setMaxCOncurrentRequests, defaulting to 10
* concurrent requests.
*/
class RequestPoolManager {
private id: string;
Expand All @@ -77,8 +83,11 @@ class RequestPoolManager {
[RequestType.Prefetch]: 0,
[RequestType.Compute]: 0,
} as Record<RequestType, number>;
private maxConcurrentRequests = 10;

/* maximum number of requests of each type. */
public maxNumRequests: {
[RequestType.Metadata]: number;
[RequestType.Interaction]: number;
[RequestType.Thumbnail]: number;
[RequestType.Prefetch]: number;
Expand All @@ -97,6 +106,7 @@ class RequestPoolManager {
this.id = id ? id : uuidv4();

this.requestPool = {
[RequestType.Metadata]: { 0: [] },
[RequestType.Interaction]: { 0: [] },
[RequestType.Thumbnail]: { 0: [] },
[RequestType.Prefetch]: { 0: [] },
Expand All @@ -107,13 +117,15 @@ class RequestPoolManager {
this.awake = false;

this.numRequests = {
[RequestType.Metadata]: 0,
[RequestType.Interaction]: 0,
[RequestType.Thumbnail]: 0,
[RequestType.Prefetch]: 0,
[RequestType.Compute]: 0,
} as Record<RequestType, number>;

this.maxNumRequests = {
[RequestType.Metadata]: 6,
[RequestType.Interaction]: 6,
[RequestType.Thumbnail]: 6,
[RequestType.Prefetch]: 5,
Expand All @@ -127,6 +139,14 @@ class RequestPoolManager {
};
}

/**
* Sets the overall maximum number of requests combined between the various types.
* Only applies to metadata, interaction, thumbnail and prefetch
*/
public setMaxConcurrentRequests(maxNumRequests: number) {
this.maxConcurrentRequests = maxNumRequests;
}

/**
* This function sets the maximum number of requests for a given request type.
* @param type - The type of request you want to set the max number
Expand Down Expand Up @@ -231,8 +251,18 @@ class RequestPoolManager {
this.requestPool[type] = { 0: [] };
}

private sendRequests(type) {
const requestsToSend = this.maxNumRequests[type] - this.numRequests[type];
protected getRequestsAvailable(type) {
return this.maxNumRequests[type] - this.numRequests[type];
}

private sendRequests(type, maxRemaining = 1000) {
if (maxRemaining <= 0) {
return this.getRequestsAvailable(type) > 0;
}
const requestsToSend = Math.min(
this.getRequestsAvailable(type),
maxRemaining
);
let syncImageCount = 0;

for (let i = 0; i < requestsToSend; i++) {
Expand Down Expand Up @@ -280,19 +310,48 @@ class RequestPoolManager {
return null;
}

public get outstandingRequests() {
return (
this.numRequests[RequestType.Interaction] +
this.numRequests[RequestType.Thumbnail] +
this.numRequests[RequestType.Prefetch]
);
}

protected startGrabbing(): void {
const hasRemainingMetadataRequests = this.sendRequests(
RequestType.Metadata,
this.maxConcurrentRequests - this.outstandingRequests
);
let available = this.maxConcurrentRequests - this.outstandingRequests;
// Allow 1 interaction request always
const hasRemainingInteractionRequests = this.sendRequests(
RequestType.Interaction
RequestType.Interaction,
(available > 0 && available) ||
(this.numRequests[RequestType.Interaction] === 0 && 1) ||
0
);
available = this.maxConcurrentRequests - this.outstandingRequests;
// Allow an extra request for thumbnail if there are no outstanding
// interaction or thumbnail requests.
const hasRemainingThumbnailRequests = this.sendRequests(
RequestType.Thumbnail
RequestType.Thumbnail,
(available > 0 && available) ||
(this.numRequests[RequestType.Interaction] === 0 &&
this.numRequests[RequestType.Thumbnail] === 0 &&
1) ||
0
);
const hasRemainingPrefetchRequests = this.sendRequests(
RequestType.Prefetch
RequestType.Prefetch,
this.maxConcurrentRequests - this.outstandingRequests
);

// Compute requests are on a different queue as they don't use http requests
const hasRemainingComputeRequests = this.sendRequests(RequestType.Compute);

if (
!hasRemainingMetadataRequests &&
!hasRemainingInteractionRequests &&
!hasRemainingThumbnailRequests &&
!hasRemainingPrefetchRequests &&
Expand Down