@@ -10,6 +10,7 @@ import {
1010 type DesktopServerExposureState ,
1111} from "@t3tools/contracts" ;
1212import { isTailscaleIpv4Address , readTailscaleStatus } from "@t3tools/tailscale" ;
13+ import * as Cache from "effect/Cache" ;
1314import * as Context from "effect/Context" ;
1415import * as Duration from "effect/Duration" ;
1516import * as Effect from "effect/Effect" ;
@@ -419,13 +420,19 @@ function resolveRuntimeState(input: {
419420const tailscaleIpv4AddressKey = (
420421 networkInterfaces : DesktopNetworkInterfaces . NetworkInterfaces ,
421422) : string =>
422- Object . values ( networkInterfaces )
423- . flatMap ( ( addresses ) => addresses ?? [ ] )
424- . filter (
425- ( address ) =>
426- ! address . internal && address . family === "IPv4" && isTailscaleIpv4Address ( address . address ) ,
427- )
428- . map ( ( address ) => address . address )
423+ [
424+ ...new Set (
425+ Object . values ( networkInterfaces )
426+ . flatMap ( ( addresses ) => addresses ?? [ ] )
427+ . filter (
428+ ( address ) =>
429+ ! address . internal &&
430+ address . family === "IPv4" &&
431+ isTailscaleIpv4Address ( address . address ) ,
432+ )
433+ . map ( ( address ) => address . address ) ,
434+ ) ,
435+ ]
429436 . toSorted ( )
430437 . join ( "," ) ;
431438
@@ -447,21 +454,17 @@ export const make = Effect.gen(function* () {
447454 // with values it read before that change persisted.
448455 const changePermit = yield * Semaphore . make ( 1 ) ;
449456
450- // Cache the `tailscale status` spawn for the TTL. On macOS, the Mac App
451- // Store Tailscale CLI lives inside Tailscale's sandbox container, so each
452- // spawn re-triggers the "Other apps" TCC prompt.
453- const [ cachedReadTailscaleStatus , invalidateTailscaleStatus ] =
454- yield * Effect . cachedInvalidateWithTTL (
457+ // Key status by interface addresses so network changes bypass stale DNS and
458+ // identity. Cache unchanged addresses to avoid repeated macOS TCC prompts.
459+ const tailscaleStatusCache = yield * Cache . make ( {
460+ capacity : 1 ,
461+ timeToLive : TAILSCALE_STATUS_CACHE_TTL ,
462+ lookup : ( _addresses : string ) =>
455463 readTailscaleStatus . pipe (
456464 Effect . orElseSucceed ( ( ) => null ) ,
457465 Effect . provideService ( ChildProcessSpawner . ChildProcessSpawner , childProcessSpawner ) ,
458466 ) ,
459- TAILSCALE_STATUS_CACHE_TTL ,
460- ) ;
461-
462- // Tailscale connecting or disconnecting changes its interface addresses, which
463- // makes a status read cached before the change stale.
464- const lastTailscaleAddressesRef = yield * Ref . make < string | null > ( null ) ;
467+ } ) ;
465468
466469 const readNetworkInterfaces = networkInterfaces . read ;
467470
@@ -577,21 +580,15 @@ export const make = Effect.gen(function* () {
577580 return coreEndpoints ;
578581 }
579582
580- const tailscaleAddresses = tailscaleIpv4AddressKey ( currentNetworkInterfaces ) ;
581- const previousTailscaleAddresses = yield * Ref . getAndSet (
582- lastTailscaleAddressesRef ,
583- tailscaleAddresses ,
584- ) ;
585- if ( previousTailscaleAddresses !== tailscaleAddresses ) {
586- yield * invalidateTailscaleStatus ;
587- }
588-
589583 const tailscaleEndpoints = yield * resolveTailscaleAdvertisedEndpoints ( {
590584 port : state . port ,
591585 serveEnabled : state . tailscaleServeEnabled ,
592586 servePort : state . tailscaleServePort ,
593587 networkInterfaces : currentNetworkInterfaces ,
594- readStatus : cachedReadTailscaleStatus ,
588+ readStatus : Cache . get (
589+ tailscaleStatusCache ,
590+ tailscaleIpv4AddressKey ( currentNetworkInterfaces ) ,
591+ ) ,
595592 } ) . pipe (
596593 Effect . provideService ( ChildProcessSpawner . ChildProcessSpawner , childProcessSpawner ) ,
597594 Effect . provideService ( HttpClient . HttpClient , httpClient ) ,
0 commit comments