Skip to content

Commit 036f880

Browse files
committed
Fixed small issues
1 parent aab8d52 commit 036f880

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

protocol.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ The `type` field indicates that this is an API request, the `serviceId` field in
1919
will bear the `requestId` of 652. Finally, the `payload` field contains the JSON formatted message that is to be
2020
parsed by the particular service, in this case `getCustomerIds`.
2121

22+
If the `requestId` was already used by a previous request, then that request is implicitly cancelled.
23+
2224
On a successful call, the backend will respond with a stream of messages:
2325

2426
```json
@@ -62,7 +64,7 @@ In the case the client sends a request to a non-existing service, for example:
6264
{"type":"request","serviceId":"getCustomerIdsWrong","requestId":652,"payload":{ ... }}
6365
```
6466

65-
The server responds with an error of `unknonwnEndpoint`:
67+
The server responds with an error of `unknownEndpoint`:
6668

6769
```json
6870
{"type":"error","requestId":652,"kind":{"type":"unknownEndpoint","endpoint":"getCustomerIdsWrong"}}

src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ pub fn serve(services: Vec<BoxedService>) -> impl Filter<Extract = (impl warp::R
9797
})
9898
}
9999

100-
// TODO: Cancel previous requests
101100
fn client_connected(
102101
ws: WebSocket,
103102
services: Arc<BTreeMap<String, BoxedService>>,
@@ -137,7 +136,11 @@ fn client_connected(
137136
if let Some(srv) = services.get(body.service_id) {
138137
// Set up cancellation signal
139138
let canceled = Arc::new(AtomicBool::new(false));
140-
active_responses.insert(body.request_id, canceled.clone());
139+
140+
if let Some(previous) = active_responses.insert(body.request_id, canceled.clone()) {
141+
previous.store(true, Ordering::SeqCst);
142+
};
143+
141144
executor
142145
.spawn(serve_request(
143146
canceled,
@@ -367,7 +370,7 @@ mod tests {
367370
completion = Some(env.clone());
368371
false
369372
}
370-
}) // if let Outgoing::Next { .. } = env { true } else { false })
373+
})
371374
.filter_map(|env| {
372375
if let Outgoing::Next { payload, .. } = env {
373376
Some(serde_json::from_value::<Resp>(payload).expect("Could not deserialize response"))

0 commit comments

Comments
 (0)