|
| 1 | +-- | This module provides an experimental and opinionated interface |
| 2 | +-- | for managing multiple Hydra applications. |
1 | 3 | module HydraSdk.Internal.Extra.AppManager |
2 | 4 | ( ActiveApp |
3 | 5 | , AppManager |
@@ -36,27 +38,61 @@ import Effect.Aff.Class (class MonadAff, liftAff) |
36 | 38 | import Effect.Class (liftEffect) |
37 | 39 | import Effect.Exception (Error) |
38 | 40 |
|
| 41 | +-- | The index of the slot within the `AppManager` that can be |
| 42 | +-- | reserved / occupied to host a Hydra application with a properly |
| 43 | +-- | configured Hydra Head. |
39 | 44 | type AppManagerSlot = Int |
| 45 | + |
| 46 | +-- | A secret used to authenticate application hosting requests |
| 47 | +-- | for a reserved slot. |
40 | 48 | type ReservationCode = UUID |
41 | 49 |
|
| 50 | +-- | Represents an active application within the `AppManager`, i.e., |
| 51 | +-- | a hosted application with an established Hydra Head. |
| 52 | +-- | |
| 53 | +-- | `state`: The state of the running application. Typically a record |
| 54 | +-- | with mutable, thread-safe variables used to track the current |
| 55 | +-- | Hydra Head status, UTxO snapshot, and other relevant |
| 56 | +-- | information. |
| 57 | +-- | |
| 58 | +-- | `config`: The configuration of the running application. |
| 59 | +-- | |
| 60 | +-- | `occupiedSlot`: The number of the slot this application occupies. |
42 | 61 | type ActiveApp appState appConfig = |
43 | 62 | { state :: appState |
44 | 63 | , config :: appConfig |
45 | 64 | , occupiedSlot :: AppManagerSlot |
46 | 65 | } |
47 | 66 |
|
| 67 | +-- | Represents a reserved slot within the `AppManager`. |
| 68 | +-- | |
| 69 | +-- | `config`: App configuration corresponding to the reserved slot. |
| 70 | +-- | This configuration is used to spin up an application instance |
| 71 | +-- | given the correct reservation code is provided. |
| 72 | +-- | |
| 73 | +-- | `reservationCode`: A secret used to authenticate hosting requests |
| 74 | +-- | for this reservation. |
| 75 | +-- | |
| 76 | +-- | `reservationMonitor`: Fiber of the assigned reservation monitor, |
| 77 | +-- | which will remove the reservation and free the slot once |
| 78 | +-- | the configured slot reservation period has expired. |
48 | 79 | type ReservedSlot appConfig = |
49 | 80 | { config :: appConfig |
50 | 81 | , reservationCode :: ReservationCode |
51 | 82 | , reservationMonitor :: Fiber Unit |
52 | 83 | } |
53 | 84 |
|
| 85 | +-- | Hydra application manager. Tracks active application instances |
| 86 | +-- | and slots for future hosting requests. |
54 | 87 | type AppManager appId appState appConfigAvailable appConfigActive = |
55 | 88 | { activeApps :: Map appId (ActiveApp appState appConfigActive) |
56 | 89 | , reservedSlots :: Map AppManagerSlot (ReservedSlot appConfigAvailable) |
57 | 90 | , availableSlots :: Map AppManagerSlot appConfigAvailable |
58 | 91 | } |
59 | 92 |
|
| 93 | +-- | Applies an effectful function to the `AppManager` stored in |
| 94 | +-- | an asynchronous variable (AVar) in a safe manner. |
| 95 | +-- TODO: Consider using `HydraSdk.Internal.Lib.AVar.modify` instead? |
60 | 96 | withAppManager |
61 | 97 | :: forall m appId appState appConfigAvailable appConfigActive a |
62 | 98 | . MonadAff m |
|
0 commit comments