@@ -82,6 +82,11 @@ export class RemoteEnvironmentAuthorization extends Context.Service<
8282 readonly expectedEnvironmentId : EnvironmentId ;
8383 readonly rejectedAccessToken ?: string ;
8484 } ) => Effect . Effect < AuthorizedRemoteHttpEnvironment , ConnectionAttemptError > ;
85+ /** Lets the next direct authorization of this address skip its own descriptor read. */
86+ readonly rememberCheckedDescriptor : (
87+ httpBaseUrl : string ,
88+ descriptor : ExecutionEnvironmentDescriptor ,
89+ ) => Effect . Effect < void > ;
8590 /**
8691 * Renews a live socket's token before it expires, so the server moves the
8792 * socket to the new session instead of closing it. Runs until interrupted.
@@ -95,6 +100,7 @@ export class RemoteEnvironmentAuthorization extends Context.Service<
95100
96101const CACHED_ENDPOINT_SOCKET_TIMEOUT_MS = 3_000 ;
97102const DPOP_AUTHORIZATION_TIMEOUT_MS = 30_000 ;
103+ const CHECKED_DESCRIPTOR_TTL_MS = 5_000 ;
98104
99105function mapDpopSocketError ( error : RemoteEnvironmentAuthError | ConnectionAttemptError ) {
100106 return error . _tag === "ConnectionTransientError" || error . _tag === "ConnectionBlockedError"
@@ -135,6 +141,13 @@ export const make = Effect.gen(function* () {
135141 }
136142 > ( ) ;
137143
144+ // A host check that just answered hands its descriptor to the reconnect it
145+ // triggers, once, so the reconnect skips a round trip.
146+ const checkedDescriptors = new Map <
147+ string ,
148+ { readonly descriptor : ExecutionEnvironmentDescriptor ; readonly at : number }
149+ > ( ) ;
150+
138151 /**
139152 * A saved LAN address can belong to another machine on a different network.
140153 */
@@ -144,9 +157,15 @@ export const make = Effect.gen(function* () {
144157 readonly httpBaseUrl : string ;
145158 readonly connectionMethod : ClientConnectionMethod ;
146159 } ) {
147- const descriptor = yield * fetchDescriptor ( input . httpBaseUrl , input . connectionMethod ) . pipe (
148- Effect . provideService ( HttpClient . HttpClient , httpClient ) ,
149- ) ;
160+ const checked = checkedDescriptors . get ( input . httpBaseUrl ) ;
161+ checkedDescriptors . delete ( input . httpBaseUrl ) ;
162+ const descriptor =
163+ checked !== undefined &&
164+ ( yield * Clock . currentTimeMillis ) - checked . at <= CHECKED_DESCRIPTOR_TTL_MS
165+ ? checked . descriptor
166+ : yield * fetchDescriptor ( input . httpBaseUrl , input . connectionMethod ) . pipe (
167+ Effect . provideService ( HttpClient . HttpClient , httpClient ) ,
168+ ) ;
150169 if ( descriptor . environmentId !== input . expectedEnvironmentId ) {
151170 return yield * environmentMismatchError ( {
152171 expected : input . expectedEnvironmentId ,
@@ -527,6 +546,10 @@ export const make = Effect.gen(function* () {
527546
528547 return RemoteEnvironmentAuthorization . of ( {
529548 authorizeBearer,
549+ // Untraced: it runs on every live session's host check.
550+ rememberCheckedDescriptor : Effect . fnUntraced ( function * ( httpBaseUrl , descriptor ) {
551+ checkedDescriptors . set ( httpBaseUrl , { descriptor, at : yield * Clock . currentTimeMillis } ) ;
552+ } ) ,
530553 authorizeDpop : Effect . fn ( "environment.authorization" ) ( function ( input ) {
531554 return authorizeDpop ( input ) ;
532555 } ) ,
0 commit comments