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
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::stream_rpc::{
    connection::{BoxConnectionStream, ClientConnection, ConnectionContext, StreamSender},
    counters, logging,
    subscription_types::SubscriptionConfig,
};
use diem_infallible::RwLock;
use diem_logger::debug;
use futures::StreamExt;
use std::{
    collections::HashMap,
    sync::{
        atomic::{AtomicU64, Ordering},
        Arc,
    },
};

use storage_interface::MoveDbReader;

#[derive(Clone)]
pub struct ConnectionManager {
    pub clients: Arc<RwLock<HashMap<u64, ClientConnection>>>,
    pub diem_db: Arc<dyn MoveDbReader>,
    pub config: Arc<SubscriptionConfig>,
    /// Our unique user id counter.
    next_user_id: Arc<AtomicU64>,
}

impl ConnectionManager {
    pub fn new(diem_db: Arc<dyn MoveDbReader>, config: Arc<SubscriptionConfig>) -> Self {
        Self {
            clients: Arc::new(RwLock::new(HashMap::new())),
            diem_db,
            config,
            next_user_id: Arc::new(AtomicU64::new(0)),
        }
    }

    fn get_db(&self) -> Arc<dyn MoveDbReader> {
        self.diem_db.clone()
    }

    pub fn has_client(&self, client_id: u64) -> bool {
        self.clients.read().contains_key(&client_id)
    }

    #[allow(unused)]
    pub fn get_client(&self, client_id: u64) -> Option<ClientConnection> {
        self.clients.read().get(&client_id).cloned()
    }

    pub fn add_client(&self, client: ClientConnection) {
        self.clients.write().insert(client.id, client);
    }

    pub fn next_user_id(&self) -> u64 {
        // Ensure that if the node is up long enough, and we have enough connections to wrap around, we don't accidentally clobber an existing client
        loop {
            let next_id = self.next_user_id.fetch_add(1, Ordering::Relaxed);
            if !self.has_client(next_id) {
                return next_id;
            }
        }
    }

    /// Create a `ClientConnection`, and then wait for either the sending/receiving connection to
    /// close, before initiating cleanup
    pub async fn client_connection(
        self,
        client_sender: StreamSender,
        mut client_rcv: BoxConnectionStream,
        connection_context: ConnectionContext,
    ) {
        let client_id = self.next_user_id();
        let client = ClientConnection::new(
            client_id,
            client_sender.clone(),
            connection_context,
            self.config.clone(),
        );
        self.add_client(client.clone());

        counters::CLIENT_CONNECTED
            .with_label_values(&[
                client.connection_context.transport.as_str(),
                client.connection_context.sdk_info.language.as_str(),
                &client.connection_context.sdk_info.version.to_string(),
            ])
            .inc();

        debug!(
            logging::StreamRpcLog {
                transport: client.connection_context.transport.as_str(),
                remote_addr: client.connection_context.remote_addr.as_deref(),
                user_agent: None,
                action: logging::StreamRpcAction::ClientConnectionLog(
                    logging::ClientConnectionLog {
                        client_id: Some(client.id),
                        forwarded: None,
                        rpc_method: None,
                    }
                ),
            },
            "client connected"
        );

        // Reap client if we can no longer send to it
        let send_task = tokio::task::spawn(async move {
            loop {
                tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
                if client_sender.is_closed() {
                    break;
                }
            }
        });

        // TODO: reap idle connections without any subscriptions or which haven't accepted a message in a while?
        let task_client = client.clone();
        let task_db = self.get_db();
        let recv_task = tokio::task::spawn(async move {
            while let Some(result) = client_rcv.next().await {
                match result {
                    Ok(msg) => {
                        if let Some(message) = msg {
                            task_client.received_message(task_db.clone(), message).await;
                        }
                    }
                    Err(e) => {
                        debug!(
                            logging::StreamRpcLog {
                                transport: task_client.connection_context.transport.as_str(),
                                remote_addr: task_client.connection_context.remote_addr.as_deref(),
                                user_agent: None,
                                action: logging::StreamRpcAction::ClientConnectionLog(
                                    logging::ClientConnectionLog {
                                        client_id: Some(task_client.id),
                                        forwarded: None,
                                        rpc_method: None,
                                    }
                                ),
                            },
                            "client disconnect start {}", e
                        );
                        break;
                    }
                };
            }
        });

        // Wait for either the side of the stream to be closed
        tokio::select! {
            _ = send_task => (),
            _ = recv_task => (),
        }

        // One of the streams has been closed, so it's time to disconnect & cleanup
        self.clients.write().remove(&client_id);

        debug!(
            logging::StreamRpcLog {
                transport: client.connection_context.transport.as_str(),
                remote_addr: client.connection_context.remote_addr.as_deref(),
                user_agent: None,
                action: logging::StreamRpcAction::ClientConnectionLog(
                    logging::ClientConnectionLog {
                        client_id: Some(client.id),
                        forwarded: None,
                        rpc_method: None,
                    }
                ),
            },
            "client disconnected"
        );
    }
}