Skip to content

Commit

Permalink
feat: adapt client concurrency to network conditions
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Cao <[email protected]>
  • Loading branch information
ryanccn committed Nov 10, 2023
1 parent 41cbd9d commit c61c2de
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/lib/changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const exists = async (f: string) => {

export const listChangelogPosts = async () => {
const fileList = await readdir("./changelog").then((list) => list.filter((k) => k.endsWith(".mdx")));
const lim = pLimit(10);
const lim = pLimit(12);

const unsorted = await Promise.all(
fileList.map((fileName) =>
Expand Down
4 changes: 2 additions & 2 deletions src/lib/export/formats/packwiz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import {
getLatestQuilt,
} from "../upstream/loaderVersions";

import pLimit from "p-limit";
import { getCFDownload } from "../upstream/curseforge";
import { getModrinthDownload } from "../upstream/modrinth";

import { clientPLimit } from "~/lib/utils/concurrency";
import type { ModPwToml } from "~/types/packwiz";
import type { CurseForgeDownload, ModrinthDownload, ProviderSpecificOptions } from "../upstream/types";

Expand Down Expand Up @@ -42,7 +42,7 @@ export const getIndexTOML = async (id: string) => {

if (!list) return null;

const lim = pLimit(10);
const lim = clientPLimit();

return stringify({
"hash-format": "sha512",
Expand Down
4 changes: 2 additions & 2 deletions src/lib/export/formats/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type JSZip from "jszip";
import type { SetStateFn } from "~/types/react";
import type { ExportReturnData } from "../upstream/types";

import pLimit from "p-limit";
import { clientPLimit } from "~/lib/utils/concurrency";

export const enum ExportStatus {
Idle,
Expand Down Expand Up @@ -31,7 +31,7 @@ export const exportZip = async ({
throw new Error("f");
}

const lim = pLimit(8);
const lim = clientPLimit();

await Promise.all(
urls.map((downloadData) =>
Expand Down
4 changes: 2 additions & 2 deletions src/lib/export/upstream/download.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { RichModList } from "~/types/moddermore";
import type { ExportReturnData } from "./types";

import pLimit from "p-limit";
import { clientPLimit } from "~/lib/utils/concurrency";
import { getCFDownload } from "./curseforge";
import { getModrinthDownload } from "./modrinth";

Expand All @@ -13,7 +13,7 @@ export const getDownloadURLs = async (
) => {
let ret: ExportReturnData = [];

const lim = pLimit(8);
const lim = clientPLimit();

await Promise.all(
list.mods.map((mod) =>
Expand Down
6 changes: 3 additions & 3 deletions src/lib/import/parseModFolder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import type { Mod } from "~/types/moddermore";
import type { ModrinthVersion } from "~/types/modrinth";
import type { SetStateFn } from "~/types/react";

import pLimit from "p-limit";
import { remoteFetch } from "../remoteFetch";
import { clientPLimit } from "../utils/concurrency";
import { remoteFetch } from "../utils/remoteFetch";
import { curseforgeHash, modrinthHash } from "./hash";

interface CurseForgeSpecialtyResponse {
Expand Down Expand Up @@ -80,7 +80,7 @@ export const parseModFolder = async ({ f, setProgress }: InputData) => {
const ret: (Mod | null)[] = [];
setProgress({ value: 0, max: mods.length });

const resolveLimit = pLimit(8);
const resolveLimit = clientPLimit();

await Promise.all(
mods.map((mod) =>
Expand Down
4 changes: 2 additions & 2 deletions src/lib/import/prism.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { parse } from "@iarna/toml";
import type JSZip from "jszip";
import pLimit from "p-limit";
import { clientPLimit } from "../utils/concurrency";
import toast from "react-hot-toast";
import { parseModFolder } from "./parseModFolder";

Expand Down Expand Up @@ -69,7 +69,7 @@ export const parsePrismInstance = async ({ f, useMetadata, setProgress }: InputD
const ret: (Mod | null)[] = [];
setProgress({ value: 0, max: mods.length });

const resolveLimit = pLimit(8);
const resolveLimit = clientPLimit();

await Promise.all(
mods.map((mod) =>
Expand Down
23 changes: 23 additions & 0 deletions src/lib/utils/concurrency.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pLimit from "p-limit";

export const autoClientConcurrency = () => {
switch (window.navigator?.connection?.effectiveType) {
case "4g": {
return 10;
}
case "3g": {
return 6;
}
case "2g": {
return 3;
}
case "slow-2g": {
return 1;
}
default: {
return 8;
}
}
};

export const clientPLimit = () => pLimit(autoClientConcurrency());
28 changes: 28 additions & 0 deletions src/lib/utils/networkInformation.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
declare interface Navigator extends NavigatorNetworkInformation {}
declare interface WorkerNavigator extends NavigatorNetworkInformation {}

declare interface NavigatorNetworkInformation {
readonly connection?: NetworkInformation;
}

type ConnectionType =
| "bluetooth"
| "cellular"
| "ethernet"
| "mixed"
| "none"
| "wifi"
| "wimax"
| "other"
| "unknown";

type EffectiveConnectionType = "2g" | "3g" | "4g" | "slow-2g";

interface NetworkInformation extends EventTarget {
readonly type?: ConnectionType;
readonly effectiveType?: EffectiveConnectionType;
readonly downlink?: number;
readonly downlinkMax?: number;
readonly rtt?: number;
readonly saveData?: boolean;
}
2 changes: 1 addition & 1 deletion src/pages/api/likes/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const h: NextApiHandler = async (req, res) => {

const likes = sess.extraProfile.likes ?? [];

const lim = pLimit(10);
const lim = pLimit(12);

const likedLists = await Promise.all(
likes.map((likedListId) => lim(() => getSpecificListByID(likedListId))),
Expand Down
2 changes: 1 addition & 1 deletion src/pages/api/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { getSpecificList } from "~/lib/db";
import { getListsCollection } from "~/lib/db/client";
import { type ModList } from "~/types/moddermore";

const lim = pLimit(16);
const lim = pLimit(12);

const search = async (query: string, isAdmin?: boolean) => {
const collection = await getListsCollection();
Expand Down

0 comments on commit c61c2de

Please sign in to comment.