MCPcopy Index your code
hub / github.com/nodejs/node / Agent

Function Agent

lib/_http_agent.js:153–269  ·  view source on GitHub ↗
(options)

Source from the content-addressed store, hash-verified

151}
152
153function Agent(options) {
154 if (!(this instanceof Agent))
155 return new Agent(options);
156
157 EventEmitter.call(this);
158
159 this.options = { __proto__: null, ...options };
160
161 this.defaultPort = this.options.defaultPort || 80;
162 this.protocol = this.options.protocol || 'http:';
163
164 if (this.options.noDelay === undefined)
165 this.options.noDelay = true;
166
167 // Don't confuse net and make it think that we're connecting to a pipe
168 this.options.path = null;
169 this.requests = { __proto__: null };
170 this.sockets = { __proto__: null };
171 this.freeSockets = { __proto__: null };
172 this.keepAliveMsecs = this.options.keepAliveMsecs || 1000;
173 this.keepAlive = this.options.keepAlive || false;
174 this.maxSockets = this.options.maxSockets || Agent.defaultMaxSockets;
175 this.maxFreeSockets = this.options.maxFreeSockets || 256;
176 this.scheduling = this.options.scheduling || 'lifo';
177 this.maxTotalSockets = this.options.maxTotalSockets;
178 this.totalSocketCount = 0;
179
180 this.agentKeepAliveTimeoutBuffer =
181 typeof this.options.agentKeepAliveTimeoutBuffer === 'number' &&
182 this.options.agentKeepAliveTimeoutBuffer >= 0 &&
183 NumberIsFinite(this.options.agentKeepAliveTimeoutBuffer) ?
184 this.options.agentKeepAliveTimeoutBuffer :
185 1000;
186
187 const proxyEnv = this.options.proxyEnv;
188 if (typeof proxyEnv === 'object' && proxyEnv !== null) {
189 this[kProxyConfig] = parseProxyConfigFromEnv(proxyEnv, this.protocol, this.keepAlive);
190 debug(`new ${this.protocol} agent with proxy config`, this[kProxyConfig]);
191 }
192
193 validateOneOf(this.scheduling, 'scheduling', ['fifo', 'lifo']);
194
195 if (this.maxTotalSockets !== undefined) {
196 validateNumber(this.maxTotalSockets, 'maxTotalSockets', 1);
197 } else {
198 this.maxTotalSockets = Infinity;
199 }
200
201 this.on('free', (socket, options) => {
202 const name = this.getName(options);
203 debug('agent.on(free)', name);
204
205 // TODO(ronag): socket.destroy(err) might have been called
206 // before coming here and have an 'error' scheduled. In the
207 // case of socket.destroy() below this 'error' has no handler
208 // and could cause unhandled exception.
209
210 if (!socket.writable) {

Callers

nothing calls this directly

Calls 14

parseProxyConfigFromEnvFunction · 0.85
asyncResetHandleFunction · 0.85
setRequestSocketFunction · 0.85
runInAsyncScopeMethod · 0.80
keepSocketAliveMethod · 0.80
debugFunction · 0.50
callMethod · 0.45
onMethod · 0.45
getNameMethod · 0.45
destroyMethod · 0.45
shiftMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…