@@ -21,7 +21,7 @@ const MANAGED_TUNNEL_PENDING_WINDOW_MINUTES = 15;
2121export class ManagedTunnelLimitPersistenceError extends Schema . TaggedError < ManagedTunnelLimitPersistenceError > ( ) (
2222 "ManagedTunnelLimitPersistenceError" ,
2323 {
24- operation : Schema . Literals ( [ "lock-user" , "load-limit" , "count-tunnels" ] ) ,
24+ operation : Schema . Literals ( [ "transaction" , " lock-user", "load-limit" , "count-tunnels" ] ) ,
2525 userId : Schema . String ,
2626 cause : Schema . Defect ( ) ,
2727 } ,
@@ -48,7 +48,7 @@ export class ManagedTunnelLimitExceeded extends Schema.TaggedError<ManagedTunnel
4848export class ManagedTunnelLimits extends Context . Service <
4949 ManagedTunnelLimits ,
5050 {
51- /** Run `reserve` only while the user is under their limit, serialized per user so concurrent reservations cannot overshoot it . */
51+ /** Runs `reserve` only while the user is under their limit, serialized per user in one transaction . */
5252 readonly withCapacity : < A , E , R > (
5353 input : { readonly userId : string ; readonly environmentId : string } ,
5454 reserve : Effect . Effect < A , E , R > ,
@@ -64,34 +64,47 @@ const make = Effect.gen(function* () {
6464 input : { readonly userId : string ; readonly environmentId : string } ,
6565 reserve : Effect . Effect < A , E , R > ,
6666 ) : Effect . fn . Return < A , E | ManagedTunnelLimitExceeded | ManagedTunnelLimitPersistenceError , R > {
67- const persistenceError =
68- ( operation : ManagedTunnelLimitPersistenceError [ "operation" ] ) => ( cause : unknown ) =>
69- new ManagedTunnelLimitPersistenceError ( { operation, userId : input . userId , cause } ) ;
70-
7167 const checkAndReserve = Effect . gen ( function * ( ) {
72- // Held until commit, so the count below includes every reservation that won the lock earlier .
68+ // Held until commit, so the count below sees every reservation that took the lock first .
7369 yield * db
7470 . execute (
7571 sql `select pg_advisory_xact_lock(hashtextextended(${ `managed-tunnel-limit:${ input . userId } ` } , 0))` ,
7672 )
77- . pipe ( Effect . mapError ( persistenceError ( "lock-user" ) ) ) ;
73+ . pipe (
74+ Effect . mapError (
75+ ( cause ) =>
76+ new ManagedTunnelLimitPersistenceError ( {
77+ operation : "lock-user" ,
78+ userId : input . userId ,
79+ cause,
80+ } ) ,
81+ ) ,
82+ ) ;
7883
7984 const overrides = yield * db
8085 . select ( { maxTunnels : relayManagedTunnelLimits . maxTunnels } )
8186 . from ( relayManagedTunnelLimits )
8287 . where ( eq ( relayManagedTunnelLimits . userId , input . userId ) )
8388 . limit ( 1 )
84- . pipe ( Effect . mapError ( persistenceError ( "load-limit" ) ) ) ;
89+ . pipe (
90+ Effect . mapError (
91+ ( cause ) =>
92+ new ManagedTunnelLimitPersistenceError ( {
93+ operation : "load-limit" ,
94+ userId : input . userId ,
95+ cause,
96+ } ) ,
97+ ) ,
98+ ) ;
8599 const maxTunnels = overrides [ 0 ] ?. maxTunnels ?? DEFAULT_MANAGED_TUNNEL_LIMIT ;
86100
87101 const pendingCutoff = DateTime . formatIso (
88102 DateTime . subtract ( yield * DateTime . now , {
89103 minutes : MANAGED_TUNNEL_PENDING_WINDOW_MINUTES ,
90104 } ) ,
91105 ) ;
92- // Count reservations, not links: links are written only after provisioning creates Cloudflare resources.
93- // A row counts while recently reserved (provisioning may be in flight) or while it holds a live tunnel
94- // for a link that is neither revoked nor publish-only. Exclude this environment so relinking stays idempotent.
106+ // Counts allocations, not links: a link is written only after its tunnel exists.
107+ // Excludes this environment so relinking at the limit stays idempotent.
95108 const counted = yield * db
96109 . select ( { activeTunnels : count ( ) } )
97110 . from ( relayManagedEndpointAllocations )
@@ -126,7 +139,16 @@ const make = Effect.gen(function* () {
126139 ) ,
127140 ) ,
128141 )
129- . pipe ( Effect . mapError ( persistenceError ( "count-tunnels" ) ) ) ;
142+ . pipe (
143+ Effect . mapError (
144+ ( cause ) =>
145+ new ManagedTunnelLimitPersistenceError ( {
146+ operation : "count-tunnels" ,
147+ userId : input . userId ,
148+ cause,
149+ } ) ,
150+ ) ,
151+ ) ;
130152 const activeTunnels = counted [ 0 ] ?. activeTunnels ?? 0 ;
131153
132154 if ( activeTunnels >= maxTunnels ) {
@@ -140,13 +162,18 @@ const make = Effect.gen(function* () {
140162 return yield * reserve ;
141163 } ) ;
142164
143- return yield * db . $client
144- . withTransaction ( checkAndReserve )
145- . pipe (
146- Effect . mapError ( ( cause ) =>
147- SqlError . isSqlError ( cause ) ? persistenceError ( "lock-user" ) ( cause ) : cause ,
148- ) ,
149- ) ;
165+ // Statements inside map their own SqlErrors, so any left here come from opening or committing the transaction.
166+ return yield * db . $client . withTransaction ( checkAndReserve ) . pipe (
167+ Effect . mapError ( ( cause ) =>
168+ SqlError . isSqlError ( cause )
169+ ? new ManagedTunnelLimitPersistenceError ( {
170+ operation : "transaction" ,
171+ userId : input . userId ,
172+ cause,
173+ } )
174+ : cause ,
175+ ) ,
176+ ) ;
150177 } ) ,
151178 } ) ;
152179} ) ;
0 commit comments