@@ -15,6 +15,7 @@ import * as Layer from "effect/Layer";
1515import * as Option from "effect/Option" ;
1616import * as Ref from "effect/Ref" ;
1717import * as Stream from "effect/Stream" ;
18+ import * as SynchronizedRef from "effect/SynchronizedRef" ;
1819import { HttpClient } from "effect/http" ;
1920import { ChildProcessSpawner } from "effect/process" ;
2021
@@ -132,7 +133,9 @@ export function classifyReachability(
132133 return fail (
133134 "no_connector" ,
134135 "Cloudflare has no live connection from this machine for the tunnel." ,
135- restart ,
136+ ( connector . readyConnections ?? 0 ) > 0
137+ ? "T3 Code retries the tunnel automatically. Wait a few minutes. If it stays offline, restart T3 Code."
138+ : restart ,
136139 ) ;
137140 }
138141 if ( status !== null ) {
@@ -212,7 +215,7 @@ const make = Effect.gen(function* () {
212215 Option . none < { readonly order : number ; readonly result : EnvironmentConnectReachability } > ( ) ,
213216 ) ;
214217 const checkCounter = yield * Ref . make ( 0 ) ;
215- const deadConnectorRef = yield * Ref . make < {
218+ const deadConnectorRef = yield * SynchronizedRef . make < {
216219 readonly streak : { readonly pid : number ; readonly since : number } | null ;
217220 readonly restartAfterMillis : number ;
218221 } > ( { streak : null , restartAfterMillis : Duration . toMillis ( DEAD_CONNECTOR_RESTART_AFTER ) } ) ;
@@ -284,30 +287,31 @@ const make = Effect.gen(function* () {
284287 ( connector . readyConnections ?? 0 ) > 0
285288 ? connector
286289 : null ;
287- const restartPid = yield * Ref . modify ( deadConnectorRef , ( state ) => {
288- if ( result . status === "reachable" ) {
289- return [
290- null ,
291- { streak : null , restartAfterMillis : Duration . toMillis ( DEAD_CONNECTOR_RESTART_AFTER ) } ,
292- ] ;
293- }
294- if ( dead === null ) return [ null , { ... state , streak : null } ] ;
295- if ( state . streak ?. pid !== dead . pid ) {
296- return [ null , { ... state , streak : { pid : dead . pid , since : now } } ] ;
297- }
298- if ( now - state . streak . since < state . restartAfterMillis ) return [ null , state ] ;
299- return [
300- dead . pid ,
301- {
290+ yield * SynchronizedRef . updateEffect ( deadConnectorRef , ( state ) =>
291+ Effect . gen ( function * ( ) {
292+ if ( result . status === "reachable" ) {
293+ return {
294+ streak : null ,
295+ restartAfterMillis : Duration . toMillis ( DEAD_CONNECTOR_RESTART_AFTER ) ,
296+ } ;
297+ }
298+ if ( dead === null ) return { ... state , streak : null } ;
299+ if ( state . streak ?. pid !== dead . pid ) {
300+ return { ... state , streak : { pid : dead . pid , since : now } } ;
301+ }
302+ if ( now - state . streak . since < state . restartAfterMillis ) return state ;
303+ const restarted = yield * runtime . restartConnector ( dead . pid ) ;
304+ return {
302305 streak : null ,
303- restartAfterMillis : Math . min (
304- state . restartAfterMillis * 2 ,
305- Duration . toMillis ( DEAD_CONNECTOR_RESTART_AFTER_MAX ) ,
306- ) ,
307- } ,
308- ] ;
309- } ) ;
310- if ( restartPid !== null ) yield * runtime . restartConnector ( restartPid ) ;
306+ restartAfterMillis : restarted
307+ ? Math . min (
308+ state . restartAfterMillis * 2 ,
309+ Duration . toMillis ( DEAD_CONNECTOR_RESTART_AFTER_MAX ) ,
310+ )
311+ : state . restartAfterMillis ,
312+ } ;
313+ } ) ,
314+ ) ;
311315 } ) ;
312316
313317 const check = Effect . gen ( function * ( ) {
0 commit comments