@@ -24,11 +24,13 @@ import {
2424 isWildcardHost ,
2525 VIRTUAL_INTERFACE ,
2626} from "../startupAccess.ts" ;
27+ import { makeBackgroundRefreshed } from "./backgroundRefresh.ts" ;
2728
2829type NetworkInterfacesMap = ReturnType < typeof NodeOS . networkInterfaces > ;
2930
3031// Stay below the config discovery timeout so slow Tailscale drops only the tailnet name.
3132const TAILSCALE_ENDPOINT_TIMEOUT = Duration . seconds ( 3 ) ;
33+ const SERVED_URL_MAX_AGE = Duration . seconds ( 30 ) ;
3234
3335export class DirectEndpoints extends Context . Service <
3436 DirectEndpoints ,
@@ -119,6 +121,30 @@ export const make = Effect.gen(function* () {
119121 : Effect . succeed ( false ) ;
120122 yield * Effect . forkDetach ( onCloudVm ) ;
121123
124+ // Serve setup can fail without stopping startup; advertise only after it answers as this server.
125+ const servedUrl = yield * makeBackgroundRefreshed (
126+ readTailscaleStatus . pipe (
127+ Effect . map ( ( status ) => status . magicDnsName ) ,
128+ Effect . flatMap ( ( magicDnsName ) => {
129+ if ( magicDnsName === null ) return Effect . succeed ( null ) ;
130+ const httpBaseUrl = buildTailscaleHttpsBaseUrl ( {
131+ magicDnsName,
132+ servePort : config . tailscaleServePort ,
133+ } ) ;
134+ return probeTailscaleHttpsEndpoint ( { baseUrl : httpBaseUrl } ) . pipe (
135+ Effect . map ( ( reachable ) => ( reachable ? httpBaseUrl : null ) ) ,
136+ ) ;
137+ } ) ,
138+ Effect . timeoutOption ( TAILSCALE_ENDPOINT_TIMEOUT ) ,
139+ Effect . map ( Option . flatMap ( Option . fromNullishOr ) ) ,
140+ Effect . orElseSucceed ( ( ) => Option . none < string > ( ) ) ,
141+ Effect . provideService ( ChildProcessSpawner . ChildProcessSpawner , spawner ) ,
142+ Effect . provideService ( HttpClient . HttpClient , httpClient ) ,
143+ ) ,
144+ SERVED_URL_MAX_AGE ,
145+ { warm : config . tailscaleServeEnabled } ,
146+ ) ;
147+
122148 const resolve = Effect . gen ( function * ( ) {
123149 const endpoints = [
124150 ...resolveBoundEndpoints ( {
@@ -128,28 +154,10 @@ export const make = Effect.gen(function* () {
128154 onCloudVm : yield * onCloudVm ,
129155 } ) ,
130156 ] ;
131- // Serve setup can fail without stopping startup; advertise only after it answers as this server.
132157 if ( config . tailscaleServeEnabled ) {
133- const servedUrl = yield * readTailscaleStatus . pipe (
134- Effect . map ( ( status ) => status . magicDnsName ) ,
135- Effect . flatMap ( ( magicDnsName ) => {
136- if ( magicDnsName === null ) return Effect . succeed ( null ) ;
137- const httpBaseUrl = buildTailscaleHttpsBaseUrl ( {
138- magicDnsName,
139- servePort : config . tailscaleServePort ,
140- } ) ;
141- return probeTailscaleHttpsEndpoint ( { baseUrl : httpBaseUrl } ) . pipe (
142- Effect . map ( ( reachable ) => ( reachable ? httpBaseUrl : null ) ) ,
143- ) ;
144- } ) ,
145- Effect . timeoutOption ( TAILSCALE_ENDPOINT_TIMEOUT ) ,
146- Effect . map ( Option . flatMap ( Option . fromNullishOr ) ) ,
147- Effect . orElseSucceed ( ( ) => Option . none < string > ( ) ) ,
148- Effect . provideService ( ChildProcessSpawner . ChildProcessSpawner , spawner ) ,
149- Effect . provideService ( HttpClient . HttpClient , httpClient ) ,
150- ) ;
151- if ( Option . isSome ( servedUrl ) ) {
152- endpoints . push ( { kind : "tailnet" , httpBaseUrl : servedUrl . value } ) ;
158+ const served = yield * servedUrl ;
159+ if ( Option . isSome ( served ) ) {
160+ endpoints . push ( { kind : "tailnet" , httpBaseUrl : served . value } ) ;
153161 }
154162 }
155163 return endpoints ;
0 commit comments