kaspa-grpc-server 0.0.3

Kaspa gRPC server
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
use crate::{
    collector::{GrpcServiceCollector, GrpcServiceConverter},
    connection::{GrpcConnection, GrpcConnectionManager, GrpcSender},
    StatusResult,
};
use futures::Stream;
use kaspa_core::trace;
use kaspa_grpc_core::protowire::{kaspad_request::Payload, rpc_server::Rpc, NotifyNewBlockTemplateResponseMessage, *};
use kaspa_notify::{
    events::EVENT_TYPE_ARRAY,
    listener::ListenerId,
    notifier::Notifier,
    scope::{
        BlockAddedScope, FinalityConflictResolvedScope, FinalityConflictScope, NewBlockTemplateScope,
        PruningPointUtxoSetOverrideScope, Scope, SinkBlueScoreChangedScope, UtxosChangedScope, VirtualChainChangedScope,
        VirtualDaaScoreChangedScope,
    },
    subscriber::{Subscriber, SubscriptionManager},
};
use kaspa_rpc_core::{
    api::rpc::RpcApi,
    notify::{channel::NotificationChannel, connection::ChannelConnection},
    Notification, RpcResult,
};
use kaspa_rpc_service::service::RpcCoreService;
use std::{io::ErrorKind, net::SocketAddr, pin::Pin, sync::Arc};
use tokio::sync::{mpsc, RwLock};
use tokio_stream::wrappers::ReceiverStream;
use tonic::{Request, Response};

/// A protowire RPC service.
///
/// Relay requests to a central core service that queries the consensus.
///
/// Registers into a central core service in order to receive consensus notifications and
/// send those forward to the registered clients.
///
///
/// ### Implementation notes
///
/// The service is a listener of the provided core service. The registration happens in the constructor,
/// giving it the lifetime of the overall service.
///
/// As a corollary, the unregistration should occur just before the object is dropped by calling finalize.
///
/// #### Lifetime and usage
///
/// - new -> Self
///     - start
///         - register_connection
///         - unregister_connection
///     - stop
/// - finalize
///
/// _Object is ready for being dropped. Any further usage of it is undefined behavior._
///
/// #### Further development
///
/// TODO: implement a queue of requests and a pool of workers preparing and sending back the responses.
pub struct GrpcService {
    core_service: Arc<RpcCoreService>,
    core_channel: NotificationChannel,
    core_listener_id: ListenerId,
    connection_manager: Arc<RwLock<GrpcConnectionManager>>,
    notifier: Arc<Notifier<Notification, GrpcConnection>>,
}

const GRPC_SERVER: &str = "grpc-server";

impl GrpcService {
    pub fn new(core_service: Arc<RpcCoreService>) -> Self {
        // Prepare core objects
        let core_channel = NotificationChannel::default();
        let core_listener_id = core_service.notifier().register_new_listener(ChannelConnection::new(core_channel.sender()));

        // Prepare internals
        let core_events = EVENT_TYPE_ARRAY[..].into();
        let converter = Arc::new(GrpcServiceConverter::new());
        let collector = Arc::new(GrpcServiceCollector::new(core_channel.receiver(), converter));
        let subscriber = Arc::new(Subscriber::new(core_events, core_service.notifier(), core_listener_id));
        let notifier: Arc<Notifier<Notification, GrpcConnection>> =
            Arc::new(Notifier::new(core_events, vec![collector], vec![subscriber], 10, GRPC_SERVER));
        let connection_manager = Arc::new(RwLock::new(GrpcConnectionManager::new(notifier.clone())));

        Self { core_service, core_channel, core_listener_id, connection_manager, notifier }
    }

    #[inline(always)]
    pub fn notifier(&self) -> Arc<Notifier<Notification, GrpcConnection>> {
        self.notifier.clone()
    }

    pub fn start(&self) {
        // Start the internal notifier
        self.notifier().start();
    }

    pub async fn register_connection(&self, address: SocketAddr, sender: GrpcSender) -> ListenerId {
        self.connection_manager.write().await.register(address, sender)
    }

    pub async fn unregister_connection(&self, address: SocketAddr) {
        self.connection_manager.write().await.unregister(address);
    }

    pub async fn stop(&self) -> RpcResult<()> {
        // Stop the internal notifier
        self.notifier().stop().await?;
        Ok(())
    }

    pub fn finalize(&self) -> RpcResult<()> {
        self.core_service.notifier().unregister_listener(self.core_listener_id)?;
        self.core_channel.receiver().close();
        Ok(())
    }
}

#[tonic::async_trait]
impl Rpc for GrpcService {
    type MessageStreamStream = Pin<Box<dyn Stream<Item = Result<KaspadResponse, tonic::Status>> + Send + Sync + 'static>>;

    async fn message_stream(
        &self,
        request: Request<tonic::Streaming<KaspadRequest>>,
    ) -> Result<Response<Self::MessageStreamStream>, tonic::Status> {
        let remote_addr = request.remote_addr().ok_or_else(|| {
            // TODO: perhaps use Uuid for connection id and allow optional address
            tonic::Status::new(tonic::Code::InvalidArgument, "Incoming connection opening request has no remote address".to_string())
        })?;

        // TODO: return err if number of inbound connections exceeded

        trace!("MessageStream from {:?}", remote_addr);

        // External sender and receiver
        let (send_channel, recv_channel) = mpsc::channel::<StatusResult<KaspadResponse>>(128);
        let listener_id = self.register_connection(remote_addr, send_channel.clone()).await;

        // Request handler
        let core_service = self.core_service.clone();
        let connection_manager = self.connection_manager.clone();
        let notifier = self.notifier();
        let mut request_stream: tonic::Streaming<KaspadRequest> = request.into_inner();
        tokio::spawn(async move {
            loop {
                // TODO: add a select! and handle a shutdown signal
                match request_stream.message().await {
                    Ok(Some(request)) => {
                        //trace!("Incoming {:?}", request);
                        // TODO: extract response gen to a method
                        let mut response: KaspadResponse = if let Some(payload) = request.payload {
                            match payload {
                                Payload::GetProcessMetricsRequest(ref request) => match request.try_into() {
                                    Ok(request) => core_service.get_process_metrics_call(request).await.into(),
                                    Err(err) => GetProcessMetricsResponseMessage::from(err).into(),
                                },
                                Payload::PingRequest(ref request) => match request.try_into() {
                                    Ok(request) => core_service.ping_call(request).await.into(),
                                    Err(err) => PingResponseMessage::from(err).into(),
                                },
                                Payload::GetCoinSupplyRequest(ref request) => match request.try_into() {
                                    Ok(request) => core_service.get_coin_supply_call(request).await.into(),
                                    Err(err) => GetCoinSupplyResponseMessage::from(err).into(),
                                },
                                Payload::GetMempoolEntriesByAddressesRequest(ref request) => match request.try_into() {
                                    Ok(request) => core_service.get_mempool_entries_by_addresses_call(request).await.into(),
                                    Err(err) => GetMempoolEntriesByAddressesResponseMessage::from(err).into(),
                                },
                                Payload::GetBalancesByAddressesRequest(ref request) => match request.try_into() {
                                    Ok(request) => core_service.get_balances_by_addresses_call(request).await.into(),
                                    Err(err) => GetBalancesByAddressesResponseMessage::from(err).into(),
                                },
                                Payload::GetBalanceByAddressRequest(ref request) => match request.try_into() {
                                    Ok(request) => core_service.get_balance_by_address_call(request).await.into(),
                                    Err(err) => GetBalanceByAddressResponseMessage::from(err).into(),
                                },
                                Payload::EstimateNetworkHashesPerSecondRequest(ref request) => match request.try_into() {
                                    Ok(request) => core_service.estimate_network_hashes_per_second_call(request).await.into(),
                                    Err(err) => EstimateNetworkHashesPerSecondResponseMessage::from(err).into(),
                                },
                                Payload::UnbanRequest(ref request) => match request.try_into() {
                                    Ok(request) => core_service.unban_call(request).await.into(),
                                    Err(err) => UnbanResponseMessage::from(err).into(),
                                },
                                Payload::BanRequest(ref request) => match request.try_into() {
                                    Ok(request) => core_service.ban_call(request).await.into(),
                                    Err(err) => BanResponseMessage::from(err).into(),
                                },
                                Payload::GetSinkBlueScoreRequest(ref request) => match request.try_into() {
                                    Ok(request) => core_service.get_sink_blue_score_call(request).await.into(),
                                    Err(err) => GetSinkBlueScoreResponseMessage::from(err).into(),
                                },
                                Payload::GetUtxosByAddressesRequest(ref request) => match request.try_into() {
                                    Ok(request) => core_service.get_utxos_by_addresses_call(request).await.into(),
                                    Err(err) => GetUtxosByAddressesResponseMessage::from(err).into(),
                                },
                                Payload::GetHeadersRequest(ref request) => match request.try_into() {
                                    Ok(request) => core_service.get_headers_call(request).await.into(),
                                    Err(err) => ShutdownResponseMessage::from(err).into(),
                                },
                                Payload::ShutdownRequest(ref request) => match request.try_into() {
                                    Ok(request) => core_service.shutdown_call(request).await.into(),
                                    Err(err) => ShutdownResponseMessage::from(err).into(),
                                },
                                Payload::GetMempoolEntriesRequest(ref request) => match request.try_into() {
                                    Ok(request) => core_service.get_mempool_entries_call(request).await.into(),
                                    Err(err) => GetMempoolEntriesResponseMessage::from(err).into(),
                                },
                                Payload::ResolveFinalityConflictRequest(ref request) => match request.try_into() {
                                    Ok(request) => core_service.resolve_finality_conflict_call(request).await.into(),
                                    Err(err) => ResolveFinalityConflictResponseMessage::from(err).into(),
                                },
                                Payload::GetBlockDagInfoRequest(ref request) => match request.try_into() {
                                    Ok(request) => core_service.get_block_dag_info_call(request).await.into(),
                                    Err(err) => GetBlockDagInfoResponseMessage::from(err).into(),
                                },
                                Payload::GetBlockCountRequest(ref request) => match request.try_into() {
                                    Ok(request) => core_service.get_block_count_call(request).await.into(),
                                    Err(err) => GetBlockCountResponseMessage::from(err).into(),
                                },
                                Payload::GetBlocksRequest(ref request) => match request.try_into() {
                                    Ok(request) => core_service.get_blocks_call(request).await.into(),
                                    Err(err) => GetBlocksResponseMessage::from(err).into(),
                                },
                                Payload::GetVirtualChainFromBlockRequest(ref request) => match request.try_into() {
                                    Ok(request) => core_service.get_virtual_chain_from_block_call(request).await.into(),
                                    Err(err) => GetVirtualChainFromBlockResponseMessage::from(err).into(),
                                },
                                Payload::GetSubnetworkRequest(ref request) => match request.try_into() {
                                    Ok(request) => core_service.get_subnetwork_call(request).await.into(),
                                    Err(err) => GetSubnetworkResponseMessage::from(err).into(),
                                },
                                Payload::SubmitTransactionRequest(ref request) => match request.try_into() {
                                    Ok(request) => core_service.submit_transaction_call(request).await.into(),
                                    Err(err) => SubmitTransactionResponseMessage::from(err).into(),
                                },
                                Payload::AddPeerRequest(ref request) => match request.try_into() {
                                    Ok(request) => core_service.add_peer_call(request).await.into(),
                                    Err(err) => AddPeerResponseMessage::from(err).into(),
                                },
                                Payload::GetConnectedPeerInfoRequest(ref request) => match request.try_into() {
                                    Ok(request) => core_service.get_connected_peer_info_call(request).await.into(),
                                    Err(err) => GetConnectedPeerInfoResponseMessage::from(err).into(),
                                },
                                Payload::GetMempoolEntryRequest(ref request) => match request.try_into() {
                                    Ok(request) => core_service.get_mempool_entry_call(request).await.into(),
                                    Err(err) => GetMempoolEntryResponseMessage::from(err).into(),
                                },
                                Payload::GetSelectedTipHashRequest(ref request) => match request.try_into() {
                                    Ok(request) => core_service.get_selected_tip_hash_call(request).await.into(),
                                    Err(err) => GetSelectedTipHashResponseMessage::from(err).into(),
                                },
                                Payload::GetPeerAddressesRequest(ref request) => match request.try_into() {
                                    Ok(request) => core_service.get_peer_addresses_call(request).await.into(),
                                    Err(err) => GetPeerAddressesResponseMessage::from(err).into(),
                                },
                                Payload::GetCurrentNetworkRequest(ref request) => match request.try_into() {
                                    Ok(request) => core_service.get_current_network_call(request).await.into(),
                                    Err(err) => GetCurrentNetworkResponseMessage::from(err).into(),
                                },
                                Payload::SubmitBlockRequest(ref request) => match request.try_into() {
                                    Ok(request) => core_service.submit_block_call(request).await.into(),
                                    Err(err) => SubmitBlockResponseMessage::from(err).into(),
                                },
                                Payload::GetBlockTemplateRequest(ref request) => match request.try_into() {
                                    Ok(request) => core_service.get_block_template_call(request).await.into(),
                                    Err(err) => GetBlockTemplateResponseMessage::from(err).into(),
                                },

                                Payload::GetBlockRequest(ref request) => match request.try_into() {
                                    Ok(request) => core_service.get_block_call(request).await.into(),
                                    Err(err) => GetBlockResponseMessage::from(err).into(),
                                },

                                Payload::GetInfoRequest(ref request) => match request.try_into() {
                                    Ok(request) => core_service.get_info_call(request).await.into(),
                                    Err(err) => GetInfoResponseMessage::from(err).into(),
                                },

                                Payload::NotifyBlockAddedRequest(ref request) => {
                                    match kaspa_rpc_core::NotifyBlockAddedRequest::try_from(request) {
                                        Ok(request) => {
                                            let result = notifier
                                                .clone()
                                                .execute_subscribe_command(
                                                    listener_id,
                                                    Scope::BlockAdded(BlockAddedScope::default()),
                                                    request.command,
                                                )
                                                .await;
                                            NotifyBlockAddedResponseMessage::from(result).into()
                                        }
                                        Err(err) => NotifyBlockAddedResponseMessage::from(err).into(),
                                    }
                                }

                                Payload::NotifyVirtualChainChangedRequest(ref request) => {
                                    match kaspa_rpc_core::NotifyVirtualChainChangedRequest::try_from(request) {
                                        Ok(request) => {
                                            let result = notifier
                                                .clone()
                                                .execute_subscribe_command(
                                                    listener_id,
                                                    Scope::VirtualChainChanged(VirtualChainChangedScope::new(
                                                        request.include_accepted_transaction_ids,
                                                    )),
                                                    request.command,
                                                )
                                                .await;
                                            NotifyVirtualChainChangedResponseMessage::from(result).into()
                                        }
                                        Err(err) => NotifyVirtualChainChangedResponseMessage::from(err).into(),
                                    }
                                }

                                Payload::NotifyFinalityConflictRequest(ref request) => {
                                    match kaspa_rpc_core::NotifyFinalityConflictRequest::try_from(request) {
                                        Ok(request) => {
                                            let result = notifier
                                                .clone()
                                                .execute_subscribe_command(
                                                    listener_id,
                                                    Scope::FinalityConflict(FinalityConflictScope::default()),
                                                    request.command,
                                                )
                                                .await
                                                .and(
                                                    notifier
                                                        .clone()
                                                        .execute_subscribe_command(
                                                            listener_id,
                                                            Scope::FinalityConflictResolved(FinalityConflictResolvedScope::default()),
                                                            request.command,
                                                        )
                                                        .await,
                                                );
                                            NotifyFinalityConflictResponseMessage::from(result).into()
                                        }
                                        Err(err) => NotifyFinalityConflictResponseMessage::from(err).into(),
                                    }
                                }

                                Payload::NotifyUtxosChangedRequest(ref request) => {
                                    match kaspa_rpc_core::NotifyUtxosChangedRequest::try_from(request) {
                                        Ok(request) => {
                                            let result = notifier
                                                .clone()
                                                .execute_subscribe_command(
                                                    listener_id,
                                                    Scope::UtxosChanged(UtxosChangedScope::new(request.addresses)),
                                                    request.command,
                                                )
                                                .await;
                                            NotifyUtxosChangedResponseMessage::from(result).into()
                                        }
                                        Err(err) => NotifyUtxosChangedResponseMessage::from(err).into(),
                                    }
                                }

                                Payload::NotifySinkBlueScoreChangedRequest(ref request) => {
                                    match kaspa_rpc_core::NotifySinkBlueScoreChangedRequest::try_from(request) {
                                        Ok(request) => {
                                            let result = notifier
                                                .clone()
                                                .execute_subscribe_command(
                                                    listener_id,
                                                    Scope::SinkBlueScoreChanged(SinkBlueScoreChangedScope::default()),
                                                    request.command,
                                                )
                                                .await;
                                            NotifySinkBlueScoreChangedResponseMessage::from(result).into()
                                        }
                                        Err(err) => NotifySinkBlueScoreChangedResponseMessage::from(err).into(),
                                    }
                                }

                                Payload::NotifyVirtualDaaScoreChangedRequest(ref request) => {
                                    match kaspa_rpc_core::NotifyVirtualDaaScoreChangedRequest::try_from(request) {
                                        Ok(request) => {
                                            let result = notifier
                                                .clone()
                                                .execute_subscribe_command(
                                                    listener_id,
                                                    Scope::VirtualDaaScoreChanged(VirtualDaaScoreChangedScope::default()),
                                                    request.command,
                                                )
                                                .await;
                                            NotifyVirtualDaaScoreChangedResponseMessage::from(result).into()
                                        }
                                        Err(err) => NotifyVirtualDaaScoreChangedResponseMessage::from(err).into(),
                                    }
                                }

                                Payload::NotifyPruningPointUtxoSetOverrideRequest(ref request) => {
                                    match kaspa_rpc_core::NotifyPruningPointUtxoSetOverrideRequest::try_from(request) {
                                        Ok(request) => {
                                            let result = notifier
                                                .clone()
                                                .execute_subscribe_command(
                                                    listener_id,
                                                    Scope::PruningPointUtxoSetOverride(PruningPointUtxoSetOverrideScope::default()),
                                                    request.command,
                                                )
                                                .await;
                                            NotifyPruningPointUtxoSetOverrideResponseMessage::from(result).into()
                                        }
                                        Err(err) => NotifyPruningPointUtxoSetOverrideResponseMessage::from(err).into(),
                                    }
                                }

                                Payload::NotifyNewBlockTemplateRequest(ref request) => {
                                    match kaspa_rpc_core::NotifyNewBlockTemplateRequest::try_from(request) {
                                        Ok(request) => {
                                            let result = notifier
                                                .clone()
                                                .execute_subscribe_command(
                                                    listener_id,
                                                    Scope::NewBlockTemplate(NewBlockTemplateScope::default()),
                                                    request.command,
                                                )
                                                .await;
                                            NotifyNewBlockTemplateResponseMessage::from(result).into()
                                        }
                                        Err(err) => NotifyNewBlockTemplateResponseMessage::from(err).into(),
                                    }
                                }

                                Payload::StopNotifyingUtxosChangedRequest(ref request) => {
                                    let notify_request = NotifyUtxosChangedRequestMessage::from(request);
                                    let response: StopNotifyingUtxosChangedResponseMessage =
                                        match kaspa_rpc_core::NotifyUtxosChangedRequest::try_from(&notify_request) {
                                            Ok(request) => {
                                                let result = notifier
                                                    .clone()
                                                    .execute_subscribe_command(
                                                        listener_id,
                                                        Scope::UtxosChanged(UtxosChangedScope::new(request.addresses)),
                                                        request.command,
                                                    )
                                                    .await;
                                                NotifyUtxosChangedResponseMessage::from(result).into()
                                            }
                                            Err(err) => NotifyUtxosChangedResponseMessage::from(err).into(),
                                        };
                                    KaspadResponse {
                                        id: 0,
                                        payload: Some(kaspad_response::Payload::StopNotifyingUtxosChangedResponse(response)),
                                    }
                                }

                                Payload::StopNotifyingPruningPointUtxoSetOverrideRequest(ref request) => {
                                    let notify_request = NotifyPruningPointUtxoSetOverrideRequestMessage::from(request);
                                    let response: StopNotifyingPruningPointUtxoSetOverrideResponseMessage =
                                        match kaspa_rpc_core::NotifyPruningPointUtxoSetOverrideRequest::try_from(&notify_request) {
                                            Ok(request) => {
                                                let result =
                                                    notifier
                                                        .clone()
                                                        .execute_subscribe_command(
                                                            listener_id,
                                                            Scope::PruningPointUtxoSetOverride(
                                                                PruningPointUtxoSetOverrideScope::default(),
                                                            ),
                                                            request.command,
                                                        )
                                                        .await;
                                                NotifyPruningPointUtxoSetOverrideResponseMessage::from(result).into()
                                            }
                                            Err(err) => NotifyPruningPointUtxoSetOverrideResponseMessage::from(err).into(),
                                        };
                                    KaspadResponse {
                                        id: 0,
                                        payload: Some(kaspad_response::Payload::StopNotifyingPruningPointUtxoSetOverrideResponse(
                                            response,
                                        )),
                                    }
                                }
                            }
                        } else {
                            // TODO: maybe add a dedicated proto message for this case
                            GetBlockResponseMessage::from(kaspa_rpc_core::RpcError::General("Missing request payload".to_string()))
                                .into()
                        };
                        response.id = request.id;
                        //trace!("Outgoing {:?}", response);

                        match send_channel.send(Ok(response)).await {
                            Ok(_) => {}
                            Err(_) => {
                                // If sending failed, then remove the connection from connection manager
                                trace!("[Remote] stream sending error. Remote {:?}", &remote_addr);
                                break;
                            }
                        }
                    }
                    Ok(None) => {
                        trace!("Request handler stream {0} got Ok(None). Connection terminated by the server", remote_addr);
                        break;
                    }

                    Err(err) => {
                        if let Some(io_err) = match_for_io_error(&err) {
                            if io_err.kind() == ErrorKind::BrokenPipe {
                                // here you can handle special case when client
                                // disconnected in unexpected way
                                trace!("\tRequest handler stream {0} error: client disconnected, broken pipe", remote_addr);
                            }
                        }
                        break;
                    }
                }
            }
            trace!("Request handler {0} terminated", remote_addr);
            // TODO: unregister connection from notifier
            connection_manager.write().await.unregister(remote_addr);
        });

        // Return connection stream
        let response_stream = ReceiverStream::new(recv_channel);
        Ok(Response::new(Box::pin(response_stream)))
    }
}

fn match_for_io_error(err_status: &tonic::Status) -> Option<&std::io::Error> {
    let mut err: &(dyn std::error::Error + 'static) = err_status;

    loop {
        if let Some(io_err) = err.downcast_ref::<std::io::Error>() {
            return Some(io_err);
        }

        // h2::Error do not expose std::io::Error with `source()`
        // https://github.com/hyperium/h2/pull/462
        if let Some(h2_err) = err.downcast_ref::<h2::Error>() {
            if let Some(io_err) = h2_err.get_io() {
                return Some(io_err);
            }
        }

        err = match err.source() {
            Some(err) => err,
            None => return None,
        };
    }
}