| 245 | // stop counting before user callbacks so they cannot suppress background spans. |
| 246 | const borrowers = { traced: 0, untraced: 0 } |
| 247 | const borrow = <A, E, R>( |
| 248 | counted: "traced" | "untraced" | "any", |
| 249 | f: (leave: () => void) => Effect.Effect<A, E, R> |
| 250 | ): Effect.Effect<A, E, R> => |
| 251 | Effect.withFiber((fiber) => { |
| 252 | const kind = fiber.cache.tracerEnabled ? "traced" : "untraced" |
| 253 | if (counted !== "any" && counted !== kind) return f(constVoid) |
| 254 | return Effect.uninterruptibleMask((restore) => { |
| 255 | let counting = true |
| 256 | const leave = () => { |
| 257 | if (counting) { |
| 258 | counting = false |
| 259 | borrowers[kind]-- |
| 260 | } |
| 261 | } |
| 262 | borrowers[kind]++ |
| 263 | return Effect.ensuring(restore(f(leave)), Effect.sync(leave)) |
| 264 | }) |
| 265 | }) |
| 266 | const acquire = Effect.uninterruptibleMask((restore) => { |
| 267 | // Closed once: on connect failure/interruption or when the transport closes. |
| 268 | const permit = Scope.makeUnsafe() |