-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
215 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { backendConfig } from "$backendConfig"; | ||
import { frontendConfig } from "$frontendConfig"; | ||
import { safeDnsPrefetch } from "$lib/utils/dnsUtils"; | ||
|
||
// Helper to safely extract hostname from URL string | ||
const getHostname = (url: string): string | null => { | ||
try { | ||
return new URL(url).hostname; | ||
} catch { | ||
return null; | ||
} | ||
}; | ||
|
||
// Define DNS prefetch targets by category | ||
export const dnsPrefetchTargets = { | ||
// API Endpoints | ||
api: [ | ||
getHostname(backendConfig.application.GRAPHQL_ENDPOINT), | ||
getHostname(backendConfig.capella.API_BASE_URL), | ||
'api.openai.com', | ||
'api.pinecone.io' | ||
], | ||
|
||
// Monitoring & APM | ||
monitoring: [ | ||
getHostname(frontendConfig.elasticApm.SERVER_URL), | ||
getHostname(frontendConfig.openreplay.INGEST_POINT), | ||
getHostname(backendConfig.openTelemetry.TRACES_ENDPOINT), | ||
getHostname(backendConfig.openTelemetry.METRICS_ENDPOINT), | ||
getHostname(backendConfig.openTelemetry.LOGS_ENDPOINT) | ||
], | ||
|
||
// Authentication | ||
auth: [ | ||
'login.microsoftonline.com' | ||
], | ||
|
||
// Content Delivery | ||
cdn: [ | ||
'd2bgp0ri487o97.cloudfront.net' | ||
] | ||
} as const; | ||
|
||
// Helper to get all unique, valid hostnames for a given category | ||
export function getDnsPrefetchTargets(categories: (keyof typeof dnsPrefetchTargets)[] = Object.keys(dnsPrefetchTargets) as any): string[] { | ||
const hostnames = categories | ||
.flatMap(category => dnsPrefetchTargets[category]) | ||
.filter((hostname): hostname is string => | ||
hostname !== null && | ||
hostname !== undefined && | ||
hostname !== '' | ||
); | ||
|
||
// Remove duplicates and localhost | ||
return [...new Set(hostnames)] | ||
.filter(hostname => | ||
!hostname.includes('localhost') && | ||
!hostname.includes('127.0.0.1') | ||
); | ||
} | ||
|
||
// Helper to prefetch DNS for specific categories | ||
export async function prefetchDnsForCategories(categories: (keyof typeof dnsPrefetchTargets)[]): Promise<void> { | ||
const targets = getDnsPrefetchTargets(categories); | ||
await safeDnsPrefetch(targets); | ||
} | ||
|
||
// Export the type for use in other files | ||
export type DnsPrefetchCategory = keyof typeof dnsPrefetchTargets; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { log, warn } from "$utils/unifiedLogger"; | ||
|
||
// Type definition for Bun's DNS cache stats | ||
interface DnsCacheStats { | ||
size: number; | ||
cacheHitsCompleted: number; | ||
cacheHitsInflight: number; | ||
cacheMisses: number; | ||
errors: number; | ||
totalCount: number; | ||
} | ||
|
||
let bunDns: { | ||
prefetch: (hostname: string) => void; | ||
getCacheStats: () => DnsCacheStats; | ||
} | null = null; | ||
|
||
// Initialize Bun DNS if available | ||
try { | ||
// Using dynamic import to avoid issues in non-Bun environments | ||
if (process.versions?.bun) { | ||
import('bun').then(bun => { | ||
bunDns = bun.dns; | ||
}).catch(err => { | ||
warn('Failed to initialize Bun DNS:', err); | ||
}); | ||
} | ||
} catch (error) { | ||
warn('Bun DNS initialization error:', error); | ||
} | ||
|
||
// Safe DNS prefetch function that works in both Bun and non-Bun environments | ||
export async function safeDnsPrefetch(hostnames: string[]): Promise<void> { | ||
if (!bunDns) { | ||
log('DNS prefetch skipped - Bun DNS not available'); | ||
return; | ||
} | ||
|
||
for (const hostname of hostnames) { | ||
try { | ||
bunDns.prefetch(hostname); | ||
log(`DNS prefetch successful for ${hostname}`); | ||
} catch (error) { | ||
warn(`DNS prefetch failed for ${hostname}:`, error); | ||
} | ||
} | ||
} | ||
|
||
// Get DNS cache stats safely | ||
export function getDnsCacheStats(): DnsCacheStats | null { | ||
if (!bunDns) { | ||
return null; | ||
} | ||
|
||
try { | ||
return bunDns.getCacheStats(); | ||
} catch (error) { | ||
warn('Failed to get DNS cache stats:', error); | ||
return null; | ||
} | ||
} | ||
|
||
// Log DNS cache effectiveness | ||
export function logDnsCacheEffectiveness(): void { | ||
const stats = getDnsCacheStats(); | ||
if (!stats) { | ||
log('DNS cache stats not available'); | ||
return; | ||
} | ||
|
||
const hitRate = stats.totalCount > 0 | ||
? (stats.cacheHitsCompleted / stats.totalCount) * 100 | ||
: 0; | ||
|
||
log('DNS Cache Effectiveness:', { | ||
hitRate: `${hitRate.toFixed(2)}%`, | ||
hits: stats.cacheHitsCompleted, | ||
misses: stats.cacheMisses, | ||
totalQueries: stats.totalCount, | ||
cacheSize: stats.size, | ||
errors: stats.errors | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters