feat(bruno-requests): log when reusing cached agent

- HTTPS agents: "Reusing cached agent (SSL session reuse enabled)"
- HTTP agents: "Reusing cached agent (connection reuse enabled)"

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
lohit-bruno
2026-01-30 16:04:28 +05:30
parent 15c86f8e6b
commit 37d1b3c5f9
2 changed files with 44 additions and 4 deletions

View File

@@ -1,7 +1,7 @@
import https from 'node:https';
import http from 'node:http';
import { EventEmitter } from 'node:events';
import { getOrCreateAgent, clearAgentCache, getAgentCacheSize } from './agent-cache';
import { getOrCreateAgent, getOrCreateHttpAgent, clearAgentCache, getAgentCacheSize } from './agent-cache';
describe('Agent Cache', () => {
beforeEach(() => {
@@ -89,6 +89,34 @@ describe('Agent Cache', () => {
expect(agent2.timeline).toBe(timeline2);
});
it('logs when reusing a cached HTTPS agent', () => {
const timeline1: any[] = [];
const timeline2: any[] = [];
// First call creates new agent - no reuse message
getOrCreateAgent(https.Agent, {}, null, timeline1);
expect(timeline1.some((e) => e.message.includes('Reusing cached agent'))).toBe(false);
// Second call reuses cached agent - should log reuse message with SSL session reuse
getOrCreateAgent(https.Agent, {}, null, timeline2);
expect(timeline2.some((e) => e.message.includes('Reusing cached agent'))).toBe(true);
expect(timeline2.some((e) => e.message.includes('SSL session reuse'))).toBe(true);
});
it('logs when reusing a cached HTTP agent', () => {
const timeline1: any[] = [];
const timeline2: any[] = [];
// First call creates new agent - no reuse message
getOrCreateHttpAgent(http.Agent, { keepAlive: true }, null, timeline1);
expect(timeline1.some((e) => e.message.includes('Reusing cached agent'))).toBe(false);
// Second call reuses cached agent - should log reuse message with connection reuse
getOrCreateHttpAgent(http.Agent, { keepAlive: true }, null, timeline2);
expect(timeline2.some((e) => e.message.includes('Reusing cached agent'))).toBe(true);
expect(timeline2.some((e) => e.message.includes('connection reuse'))).toBe(true);
});
it('logs SSL validation status on agent creation', () => {
const timeline: any[] = [];
getOrCreateAgent(https.Agent, { rejectUnauthorized: true }, null, timeline);

View File

@@ -154,7 +154,8 @@ function getOrCreateAgentInternal<TOptions extends HttpAgentOptions>(
proxyUri: string | null,
timeline: TimelineEntry[] | null,
getCacheKey: CacheKeyFn<TOptions>,
getTimelineClass: TimelineClassFn
getTimelineClass: TimelineClassFn,
cacheHitMessage: string
): HttpAgent | HttpsAgent {
const agentClassId = getAgentClassId(BaseAgentClass);
const cacheKey = getCacheKey(agentClassId, options, proxyUri);
@@ -172,6 +173,15 @@ function getOrCreateAgentInternal<TOptions extends HttpAgentOptions>(
(agent as any).timeline = timeline;
}
// Log that we're reusing a cached agent
if (timeline) {
timeline.push({
timestamp: new Date(),
type: 'info',
message: cacheHitMessage
});
}
return agent;
}
@@ -222,7 +232,8 @@ function getOrCreateAgent(
proxyUri,
timeline,
getAgentCacheKey,
getTimelineAgentClass
getTimelineAgentClass,
'Reusing cached agent (SSL session reuse enabled)'
);
}
@@ -249,7 +260,8 @@ function getOrCreateHttpAgent(
proxyUri,
timeline,
getHttpAgentCacheKey,
getTimelineHttpAgentClass
getTimelineHttpAgentClass,
'Reusing cached agent (connection reuse enabled)'
) as HttpAgent;
}