mirror of
https://github.com/usebruno/bruno.git
synced 2026-06-24 13:15:40 +00:00
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:
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user