|
| 1 | +# Async API Design Document |
| 2 | + |
| 3 | +## Remarks |
| 4 | + |
| 5 | +For this document currently we describe `Events` and `PubSub` however this |
| 6 | +analogy shall continue for other messaging patterns later on. |
| 7 | + |
| 8 | +## Terminology |
| 9 | + |
| 10 | +## Overview |
| 11 | + |
| 12 | +A high-level pitch of the feature: |
| 13 | + |
| 14 | +* The new `async` API will provide non_blocking, but still linear, behavior in |
| 15 | + the code thanks to `async/await` |
| 16 | +* The new `async` API will solve a usage of `iceoryx2` API in `async` code which |
| 17 | + currently need to be handmade manually either via: |
| 18 | + * polling and repetition logic within functions, causing additional not needed |
| 19 | + code on user side |
| 20 | + * custom WaitSet usage in separate thread and a bridge to `async` world. |
| 21 | + * or some other custom work |
| 22 | +* The `async` usage in Rust is already well established technique that is |
| 23 | + adopted by many crates, including those with highest usage |
| 24 | + |
| 25 | +## Requirements |
| 26 | + |
| 27 | +* **R1: Async API look and feel** \* The new `async` API shall provide the same |
| 28 | + look and feel as standard one |
| 29 | + |
| 30 | +## Use Cases |
| 31 | + |
| 32 | +### Use-Case 1: Waiting for an event |
| 33 | + |
| 34 | +* **As a** developer |
| 35 | +* **I want** to wait on `event` API |
| 36 | +* **So that** it does not block current thread and continues only once event is |
| 37 | + delivered |
| 38 | + |
| 39 | +### Use-Case 2: Waiting for a new sample |
| 40 | + |
| 41 | +* **As a** developer |
| 42 | +* **I want** to wait on `new sample` in pub-sub |
| 43 | +* **So that** it does not block current thread and continues only once sample is |
| 44 | + delivered |
| 45 | + |
| 46 | +## Usage |
| 47 | + |
| 48 | +### Example: Await on events |
| 49 | + |
| 50 | +```rust |
| 51 | +let node = NodeBuilder::new().create::<ipc_threadsafe::Service>().unwrap(); |
| 52 | + |
| 53 | +let event = node |
| 54 | + .service_builder(&"MyEventName".try_into().unwrap()) |
| 55 | + .event() |
| 56 | + .open_or_create() |
| 57 | + .unwrap(); |
| 58 | + |
| 59 | +let listener = event.listener_builder().create_async().unwrap(); |
| 60 | + |
| 61 | +println!("Awaiting for Iceoryx event in batches while doing something else ..."); |
| 62 | + |
| 63 | +listener |
| 64 | + .wait_all(&mut |event_id| { |
| 65 | + print!("Received Iceoryx event: {:?}\n", event_id); |
| 66 | + }) |
| 67 | + .await |
| 68 | + .unwrap(); |
| 69 | +``` |
| 70 | + |
| 71 | +## Implementation |
| 72 | + |
| 73 | +### Achieving async API |
| 74 | + |
| 75 | +Since all iceoryx2 messaging patterns (except `Event`) are poll based, we need |
| 76 | +to pair them them with the `Event` to achieve possibility to react in `async` |
| 77 | +API only on change of data. This means: |
| 78 | + |
| 79 | +* `PubSub` should be paired with `Event` |
| 80 | +* `RequestResponse` should be paired with `Event` |
| 81 | +* and so on |
| 82 | + |
| 83 | +Due to this, further document assumes direct usage of high level `Event` |
| 84 | +messaging pattern to facilitate the feature. |
| 85 | + |
| 86 | +> DISCLAIMER: IT may be that iceoryx2 authors will have better idea than that. |
| 87 | +> The only issue I do see now is probably impact on zero-trust deployment during |
| 88 | +> configuration where ie. async PubSub Producer shall also have rights to be |
| 89 | +> Event Notifier on some connected topic. |
| 90 | +
|
| 91 | +### Split of code |
| 92 | + |
| 93 | +The main idea is to split source code into two parts: |
| 94 | + |
| 95 | +1. The Event implementation that is both `OS` dependent and `runtime` dependent |
| 96 | + since it incurs some IO call |
| 97 | +2. All the other API that do need `Event` implementation, but the rest is |
| 98 | + purely `runtime` independent and can be pure `async` |
| 99 | + |
| 100 | +To facilitate above, below class diagram is showing one of solution |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | +### Building objects |
| 105 | + |
| 106 | +The next step is to provide a way to build objects that provide `async` API. |
| 107 | + |
| 108 | +There are two approaches that can be chosen for implementation: |
| 109 | + |
| 110 | +#### 1. Use custom event for each pair (messaging patern, data type) based on |
| 111 | +`ServiceName` |
| 112 | + |
| 113 | +##### Pros |
| 114 | + |
| 115 | +* all messaging pattern in service will work as usual |
| 116 | +* no limitations |
| 117 | + |
| 118 | +##### Cons |
| 119 | + |
| 120 | +* dynamic Service creation to obtain event for each messaging pattern, like for |
| 121 | + `ServiceNameABC` (PubSub, int) we need to create also internally service |
| 122 | + `ServiceNameABC/__internal_pubsub_event` to obtain event for notifications |
| 123 | + |
| 124 | +#### 2. Use event from the service `ServiceName` |
| 125 | + |
| 126 | +##### Pros |
| 127 | + |
| 128 | +* no need to create dynamic event name |
| 129 | + |
| 130 | +##### Cons |
| 131 | + |
| 132 | +* limit a service to only single messaging pattern as using event will cause no |
| 133 | + way to use it again |
| 134 | + |
| 135 | +Considering above, continuation is done based on option 1. Below shows only |
| 136 | +small snippet where extension for creating object can be placed. |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | +### Implementation |
| 141 | + |
| 142 | +#### AsyncListener - `messaging-patter == event` |
| 143 | + |
| 144 | +This is purely `runtime` specific implementation but is currently doable with |
| 145 | +`non-blocking` api of `Listener`. Working example: |
| 146 | +[Code sample](assets/event_example.rs) During implementation it may come |
| 147 | +beneficial to either expose some properties (of current listener) or `add` new |
| 148 | +sync api with different signature. |
| 149 | + |
| 150 | +#### AsyncSubscriber - `messaging-patter == pubsub` |
| 151 | + |
| 152 | +Pure `async` implementation not using any specifics of `runtime` shall be |
| 153 | +doable. In case some unexpected dependency will be needed it has to be exposed |
| 154 | +over defined abstraction same as `AsyncListenerTrait` |
| 155 | + |
| 156 | +#### Builder |
| 157 | + |
| 158 | +Implement the `Builders` extensions to they can provide `async` versions of objects |
| 159 | + |
| 160 | +## Certification & Safety-Critical Usage |
| 161 | + |
| 162 | +Answer: |
| 163 | + |
| 164 | +* Applicable standards (e.g., ASIL-D, ISO 26262) |
| 165 | +* Support for **zero-trust deployments**: can rogue processes break it? |
| 166 | +* Evidence for claims (e.g., subscribers cannot corrupt publisher-owned |
| 167 | + read-only memory) |
| 168 | +* Real-time suitability: any blocking calls, background threads, or |
| 169 | + indeterminism? |
| 170 | +* If unsuitable for zero-trust or real-time use, how do we prevent accidental |
| 171 | + misuse? |
| 172 | + |
| 173 | +PR: To me idea, we will build async API on top of existing non-blocking, non |
| 174 | +async implementation and the API will be just a thin wrapper. The connection to |
| 175 | +specific `runtime` is outside of project and in gesture of specific `runtime` to |
| 176 | +guarantee any of above. |
| 177 | + |
| 178 | +## Milestones |
| 179 | + |
| 180 | +### Milestone 1 – Provide Traits and object skeletons |
| 181 | + |
| 182 | +* TBD later |
| 183 | + |
| 184 | +**Results:** |
| 185 | + |
| 186 | +* User will see that there is ongoing work on `async` API |
| 187 | + |
| 188 | +### Milestone 2 – Implement Event in Async Runtime |
| 189 | + |
| 190 | +* TBD later |
| 191 | + |
| 192 | +**Results:** |
| 193 | + |
| 194 | +* User will get first support for async API for specific runtime. This will also |
| 195 | + open a way for other to implement a bridge to other runtime like `tokio` as |
| 196 | + basic idea will be shown |
| 197 | + |
| 198 | +### Milestone 2 – Implement PubSub |
| 199 | + |
| 200 | +* TBD later |
| 201 | + |
| 202 | +**Results:** |
| 203 | + |
| 204 | +* PubSub API will have `async` API available |
0 commit comments