| 1 | import * as Config from "effect/Config"; |
| 2 | import * as Duration from "effect/Duration"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Schedule from "effect/Schedule"; |
| 5 | import * as Schema from "effect/Schema"; |
| 6 | |
| 7 | const timeout = Config.schema( |
| 8 | Schema.DurationFromString.pipe( |
| 9 | Schema.check( |
| 10 | Schema.makeFilter<Duration.Duration>((value) => |
| 11 | Duration.isFinite(value) && Duration.isPositive(value) |
| 12 | ? true |
| 13 | : "Expected a positive finite duration", |
| 14 | ), |
| 15 | ), |
| 16 | ), |
| 17 | "AWS_CLIENT_VPN_TIMEOUT", |
| 18 | ).pipe(Config.withDefault(Duration.minutes(30))); |
| 19 | |
| 20 | // Matches the Terraform AWS provider's Client VPN association and route timeouts. |
| 21 | export const retryClientVpn = <A, E, R>( |
| 22 | effect: Effect.Effect<A, E, R>, |
| 23 | whilePending: (error: E) => boolean, |
| 24 | ) => |
| 25 | Effect.gen(function* () { |
| 26 | const duration = yield* timeout; |
| 27 | return yield* effect.pipe( |
| 28 | Effect.retry({ |
| 29 | while: whilePending, |
| 30 | schedule: Schedule.spaced("5 seconds").pipe( |
| 31 | Schedule.upTo({ duration }), |
| 32 | ), |
| 33 | }), |
| 34 | ); |
| 35 | }); |