Skip to content
Merged
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
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions scopes/scope/network/http/http.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { expect } from 'chai';
import { DEFAULT_AGENT_MAX_SOCKETS, withAgentSocketDefault } from './http';

describe('withAgentSocketDefault()', () => {
it('applies the default when network.max_sockets is not configured', () => {
expect(withAgentSocketDefault({}).maxSockets).to.equal(DEFAULT_AGENT_MAX_SOCKETS);
});

it('keeps a configured value', () => {
expect(withAgentSocketDefault({ maxSockets: 40 }).maxSockets).to.equal(40);
});
});
18 changes: 16 additions & 2 deletions scopes/scope/network/http/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,11 @@ export class Http implements Network {
fetchRetryMaxtimeout: getAsNumber(CFG_FETCH_RETRY_MAXTIMEOUT) ?? 60000,
fetchTimeout: getAsNumber(CFG_FETCH_TIMEOUT) ?? 60000,
localAddress: obj[CFG_LOCAL_ADDRESS],
maxSockets: getAsNumber(CFG_MAX_SOCKETS) ?? 15,
// No fallback on purpose, as for networkConcurrency below: the pnpm
// engine applies maxSockets as a hard per-registry cap, so a default
// of 15 throttled every install's downloads to 15 connections. Bit's
// own agent applies its default in `withAgentSocketDefault`.
maxSockets: getAsNumber(CFG_MAX_SOCKETS),
// No fallback on purpose: when the user hasn't configured it, leave it
// undefined so the package manager applies its own adaptive default
// (pnpm: min(96, max(cpuCores * 3, 64))). A hardcoded 16 here overrode
Expand All @@ -202,7 +206,7 @@ export class Http implements Network {
}

static async getAgent(uri: string, agentOpts: AgentOptions): Promise<Agent> {
const agent = await getAgent(uri, agentOpts);
const agent = await getAgent(uri, withAgentSocketDefault(agentOpts));
return agent;
}

Expand Down Expand Up @@ -956,6 +960,16 @@ export class Http implements Network {
}
}

/**
* The socket limit of Bit's own HTTP agent when `network.max_sockets` is not
* configured.
*/
export const DEFAULT_AGENT_MAX_SOCKETS = 15;

export function withAgentSocketDefault<T extends { maxSockets?: number }>(agentOpts: T): T & { maxSockets: number } {
return { ...agentOpts, maxSockets: agentOpts.maxSockets ?? DEFAULT_AGENT_MAX_SOCKETS };
}

export function getAuthHeader(token: string) {
return {
Authorization: `${DEFAULT_AUTH_TYPE} ${token}`,
Expand Down
Loading