Skip to content
Merged
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
31 changes: 19 additions & 12 deletions src/app/api/settings/proxy/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,18 +194,25 @@ export async function GET(request: Request) {
const providerAssignments = await getProxyAssignments({ scope: "provider" });
if (providerAssignments.length > 0) {
config.providers = { ...(config.providers || {}) };
for (const assignment of providerAssignments) {
if (!assignment.scopeId || !assignment.proxyId) {
continue;
}
const proxyData = await getProxyById(assignment.proxyId, { includeSecrets: true });
if (!proxyData) continue;
config.providers[assignment.scopeId] = {
type: proxyData.type,
host: proxyData.host,
port: proxyData.port,
username: proxyData.username,
password: proxyData.password,
const providerProxyResults = await Promise.all(
providerAssignments.map(async (assignment) => {
if (!assignment.scopeId || !assignment.proxyId) {
return null;
}
const proxyData = await getProxyById(assignment.proxyId, { includeSecrets: true });
if (!proxyData) return null;
return { scopeId: assignment.scopeId, proxyData };
})
);

for (const result of providerProxyResults) {
if (!result) continue;
config.providers[result.scopeId] = {
type: result.proxyData.type,
host: result.proxyData.host,
port: result.proxyData.port,
username: result.proxyData.username,
password: result.proxyData.password,
};
}
Comment on lines +197 to 217

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Optimization: Deduplicate Proxy Lookups

If multiple provider assignments share the same proxy (which is a very common configuration), the current implementation will perform redundant database queries for the same proxyId.

We can optimize this by:

  1. Filtering the assignments to find valid ones.
  2. Extracting the unique proxyIds.
  3. Fetching only the unique proxies in parallel.
  4. Mapping them back to the providers.

This reduces the number of database queries and event loop microtasks.

      const validAssignments = providerAssignments.filter(
        (assignment) => assignment.scopeId && assignment.proxyId
      );

      const uniqueProxyIds = Array.from(
        new Set(validAssignments.map((assignment) => assignment.proxyId as string))
      );

      const proxyDataResults = await Promise.all(
        uniqueProxyIds.map(async (proxyId) => {
          const proxyData = await getProxyById(proxyId, { includeSecrets: true });
          return { proxyId, proxyData };
        })
      );

      const proxyMap = new Map<string, NonNullable<typeof proxyDataResults[number]["proxyData"]>>();
      for (const { proxyId, proxyData } of proxyDataResults) {
        if (proxyData) {
          proxyMap.set(proxyId, proxyData);
        }
      }

      for (const assignment of validAssignments) {
        const proxyData = proxyMap.get(assignment.proxyId!);
        if (!proxyData) continue;
        config.providers[assignment.scopeId!] = {
          type: proxyData.type,
          host: proxyData.host,
          port: proxyData.port,
          username: proxyData.username,
          password: proxyData.password,
        };
      }

}
Expand Down