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
// Copyright (c) Facebook, Inc. and its affiliates.
// Copyright (c) Zefchain Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use std::{collections::HashMap, io, mem, net::SocketAddr, pin::pin, sync::Arc};

use async_trait::async_trait;
use futures::{
    future,
    stream::{self, FuturesUnordered, SplitSink, SplitStream},
    Sink, SinkExt, Stream, StreamExt, TryStreamExt,
};
use linera_core::{JoinSetExt as _, TaskHandle};
use serde::{Deserialize, Serialize};
use tokio::{
    io::AsyncWriteExt,
    net::{lookup_host, TcpListener, TcpStream, ToSocketAddrs, UdpSocket},
    sync::Mutex,
    task::JoinSet,
};
use tokio_util::{codec::Framed, sync::CancellationToken, udp::UdpFramed};
use tracing::{error, warn};

use crate::{
    simple::{codec, codec::Codec},
    RpcMessage,
};

/// Suggested buffer size
pub const DEFAULT_MAX_DATAGRAM_SIZE: &str = "65507";

/// Number of tasks to spawn before attempting to reap some finished tasks to prevent memory leaks.
const REAP_TASKS_THRESHOLD: usize = 100;

// Supported transport protocols.
#[derive(clap::ValueEnum, Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub enum TransportProtocol {
    Udp,
    Tcp,
}

impl std::str::FromStr for TransportProtocol {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        clap::ValueEnum::from_str(s, true)
    }
}

impl std::fmt::Display for TransportProtocol {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl TransportProtocol {
    pub fn scheme(&self) -> &'static str {
        match self {
            TransportProtocol::Udp => "udp",
            TransportProtocol::Tcp => "tcp",
        }
    }
}

/// A pool of (outgoing) data streams.
pub trait ConnectionPool: Send {
    fn send_message_to<'a>(
        &'a mut self,
        message: RpcMessage,
        address: &'a str,
    ) -> future::BoxFuture<'a, Result<(), codec::Error>>;
}

/// The handler required to create a service.
///
/// The implementation needs to implement [`Clone`] because a seed instance is used to generate
/// cloned instances, where each cloned instance handles a single request. Multiple cloned instances
/// may exist at the same time and handle separate requests concurrently.
#[async_trait]
pub trait MessageHandler: Clone {
    async fn handle_message(&mut self, message: RpcMessage) -> Option<RpcMessage>;
}

/// The result of spawning a server is oneshot channel to track completion, and the set of
/// executing tasks.
pub struct ServerHandle {
    pub handle: TaskHandle<Result<(), std::io::Error>>,
}

impl ServerHandle {
    pub async fn join(self) -> Result<(), std::io::Error> {
        self.handle.await.map_err(|_| {
            std::io::Error::new(
                std::io::ErrorKind::Interrupted,
                "Server task did not finish successfully",
            )
        })?
    }
}

/// A trait alias for a protocol transport.
///
/// A transport is an active connection that can be used to send and receive
/// [`RpcMessage`]s.
pub trait Transport:
    Stream<Item = Result<RpcMessage, codec::Error>> + Sink<RpcMessage, Error = codec::Error>
{
}

impl<T> Transport for T where
    T: Stream<Item = Result<RpcMessage, codec::Error>> + Sink<RpcMessage, Error = codec::Error>
{
}

impl TransportProtocol {
    /// Creates a transport for this protocol.
    pub async fn connect(
        self,
        address: impl ToSocketAddrs,
    ) -> Result<impl Transport, std::io::Error> {
        let mut addresses = lookup_host(address)
            .await
            .expect("Invalid address to connect to");
        let address = addresses
            .next()
            .expect("Couldn't resolve address to connect to");

        let stream: futures::future::Either<_, _> = match self {
            TransportProtocol::Udp => {
                let socket = UdpSocket::bind(&"0.0.0.0:0").await?;

                UdpFramed::new(socket, Codec)
                    .with(move |message| future::ready(Ok((message, address))))
                    .map_ok(|(message, _address)| message)
                    .left_stream()
            }
            TransportProtocol::Tcp => {
                let stream = TcpStream::connect(address).await?;

                Framed::new(stream, Codec).right_stream()
            }
        };

        Ok(stream)
    }

    /// Creates a [`ConnectionPool`] for this protocol.
    pub async fn make_outgoing_connection_pool(
        self,
    ) -> Result<Box<dyn ConnectionPool>, std::io::Error> {
        let pool: Box<dyn ConnectionPool> = match self {
            Self::Udp => Box::new(UdpConnectionPool::new().await?),
            Self::Tcp => Box::new(TcpConnectionPool::new().await?),
        };
        Ok(pool)
    }

    /// Runs a server for this protocol and the given message handler.
    pub fn spawn_server<S>(
        self,
        address: impl ToSocketAddrs + Send + 'static,
        state: S,
        shutdown_signal: CancellationToken,
        join_set: &mut JoinSet<()>,
    ) -> ServerHandle
    where
        S: MessageHandler + Send + 'static,
    {
        let handle = match self {
            Self::Udp => join_set.spawn_task(UdpServer::run(address, state, shutdown_signal)),
            Self::Tcp => join_set.spawn_task(TcpServer::run(address, state, shutdown_signal)),
        };
        ServerHandle { handle }
    }
}

/// An implementation of [`ConnectionPool`] based on UDP.
struct UdpConnectionPool {
    transport: UdpFramed<Codec>,
}

impl UdpConnectionPool {
    async fn new() -> Result<Self, std::io::Error> {
        let socket = UdpSocket::bind(&"0.0.0.0:0").await?;
        let transport = UdpFramed::new(socket, Codec);
        Ok(Self { transport })
    }
}

impl ConnectionPool for UdpConnectionPool {
    fn send_message_to<'a>(
        &'a mut self,
        message: RpcMessage,
        address: &'a str,
    ) -> future::BoxFuture<'a, Result<(), codec::Error>> {
        Box::pin(async move {
            let address = address
                .parse()
                .map_err(|error| std::io::Error::new(std::io::ErrorKind::Other, error))?;
            self.transport.send((message, address)).await
        })
    }
}

/// Server implementation for UDP.
pub struct UdpServer<State> {
    handler: State,
    udp_sink: SharedUdpSink,
    udp_stream: SplitStream<UdpFramed<Codec>>,
    active_handlers: HashMap<SocketAddr, TaskHandle<()>>,
    join_set: JoinSet<()>,
}

/// Type alias for the outgoing endpoint of UDP messages.
type SharedUdpSink = Arc<Mutex<SplitSink<UdpFramed<Codec>, (RpcMessage, SocketAddr)>>>;

impl<State> UdpServer<State>
where
    State: MessageHandler + Send + 'static,
{
    /// Runs the UDP server implementation.
    pub async fn run(
        address: impl ToSocketAddrs,
        state: State,
        shutdown_signal: CancellationToken,
    ) -> Result<(), std::io::Error> {
        let mut server = Self::bind(address, state).await?;

        loop {
            tokio::select! { biased;
                _ = shutdown_signal.cancelled() => {
                    server.shutdown().await;
                    return Ok(());
                }
                result = server.udp_stream.next() => match result {
                    Some(Ok((message, peer))) => server.handle_message(message, peer),
                    Some(Err(error)) => server.handle_error(error).await?,
                    None => unreachable!("`UdpFramed` should never return `None`"),
                },
            }
        }
    }

    /// Creates a [`UdpServer`] bound to the provided `address`, handling messages using the
    /// provided `handler`.
    async fn bind(address: impl ToSocketAddrs, handler: State) -> Result<Self, std::io::Error> {
        let socket = UdpSocket::bind(address).await?;
        let (udp_sink, udp_stream) = UdpFramed::new(socket, Codec).split();

        Ok(UdpServer {
            handler,
            udp_sink: Arc::new(Mutex::new(udp_sink)),
            udp_stream,
            active_handlers: HashMap::new(),
            join_set: JoinSet::new(),
        })
    }

    /// Spawns a task to handle a single incoming message.
    fn handle_message(&mut self, message: RpcMessage, peer: SocketAddr) {
        let previous_task = self.active_handlers.remove(&peer);
        let mut state = self.handler.clone();
        let udp_sink = self.udp_sink.clone();

        let new_task = self.join_set.spawn_task(async move {
            if let Some(reply) = state.handle_message(message).await {
                if let Some(task) = previous_task {
                    if let Err(error) = task.await {
                        warn!("Message handler task panicked: {}", error);
                    }
                }
                let status = udp_sink.lock().await.send((reply, peer)).await;
                if let Err(error) = status {
                    error!("Failed to send query response: {}", error);
                }
            }
        });

        self.active_handlers.insert(peer, new_task);

        if self.active_handlers.len() >= REAP_TASKS_THRESHOLD {
            // Collect finished tasks to avoid leaking memory.
            self.active_handlers.retain(|_, task| task.is_running());
            self.join_set.reap_finished_tasks();
        }
    }

    /// Handles an error while receiving a message.
    async fn handle_error(&mut self, error: codec::Error) -> Result<(), std::io::Error> {
        match error {
            codec::Error::IoError(io_error) => {
                error!("I/O error in UDP server: {io_error}");
                self.shutdown().await;
                Err(io_error)
            }
            other_error => {
                warn!("Received an invalid message: {other_error}");
                Ok(())
            }
        }
    }

    /// Gracefully shuts down the server, waiting for existing tasks to finish.
    async fn shutdown(&mut self) {
        let handlers = mem::take(&mut self.active_handlers);
        let mut handler_results = FuturesUnordered::from_iter(handlers.into_values());

        while let Some(result) = handler_results.next().await {
            if let Err(error) = result {
                warn!("Message handler panicked: {}", error);
            }
        }

        self.join_set.await_all_tasks().await;
    }
}

/// An implementation of [`ConnectionPool`] based on TCP.
struct TcpConnectionPool {
    streams: HashMap<String, Framed<TcpStream, Codec>>,
}

impl TcpConnectionPool {
    async fn new() -> Result<Self, std::io::Error> {
        let streams = HashMap::new();
        Ok(Self { streams })
    }

    async fn get_stream(
        &mut self,
        address: &str,
    ) -> Result<&mut Framed<TcpStream, Codec>, io::Error> {
        if !self.streams.contains_key(address) {
            match TcpStream::connect(address).await {
                Ok(s) => {
                    self.streams
                        .insert(address.to_string(), Framed::new(s, Codec));
                }
                Err(error) => {
                    error!("Failed to open connection to {}: {}", address, error);
                    return Err(error);
                }
            };
        };
        Ok(self.streams.get_mut(address).unwrap())
    }
}

impl ConnectionPool for TcpConnectionPool {
    fn send_message_to<'a>(
        &'a mut self,
        message: RpcMessage,
        address: &'a str,
    ) -> future::BoxFuture<'a, Result<(), codec::Error>> {
        Box::pin(async move {
            let stream = self.get_stream(address).await?;
            let result = stream.send(message).await;
            if result.is_err() {
                self.streams.remove(address);
            }
            result
        })
    }
}

/// Server implementation for TCP.
pub struct TcpServer<State> {
    connection: Framed<TcpStream, Codec>,
    handler: State,
    shutdown_signal: CancellationToken,
}

impl<State> TcpServer<State>
where
    State: MessageHandler + Send + 'static,
{
    /// Runs the TCP server implementation.
    ///
    /// Listens for connections and spawns a task with a new [`TcpServer`] instance to serve that
    /// client.
    pub async fn run(
        address: impl ToSocketAddrs,
        handler: State,
        shutdown_signal: CancellationToken,
    ) -> Result<(), std::io::Error> {
        let listener = TcpListener::bind(address).await?;

        let mut accept_stream = stream::try_unfold(listener, |listener| async move {
            let (socket, _) = listener.accept().await?;
            Ok::<_, io::Error>(Some((socket, listener)))
        });
        let mut accept_stream = pin!(accept_stream);

        let connection_shutdown_signal = shutdown_signal.child_token();
        let mut join_set = JoinSet::new();
        let mut reap_countdown = REAP_TASKS_THRESHOLD;

        loop {
            tokio::select! { biased;
                _ = shutdown_signal.cancelled() => {
                    join_set.await_all_tasks().await;
                    return Ok(());
                }
                maybe_socket = accept_stream.next() => match maybe_socket {
                    Some(Ok(socket)) => {
                        let server = TcpServer::new_connection(
                            socket,
                            handler.clone(),
                            connection_shutdown_signal.clone(),
                        );
                        join_set.spawn_task(server.serve());
                        reap_countdown -= 1;
                    }
                    Some(Err(error)) => {
                        join_set.await_all_tasks().await;
                        return Err(error);
                    }
                    None => unreachable!(
                        "The `accept_stream` should never finish unless there's an error",
                    ),
                },
            }

            if reap_countdown == 0 {
                join_set.reap_finished_tasks();
                reap_countdown = REAP_TASKS_THRESHOLD;
            }
        }
    }

    /// Creates a new [`TcpServer`] to serve a single connection established on the provided
    /// [`TcpStream`].
    fn new_connection(
        tcp_stream: TcpStream,
        handler: State,
        shutdown_signal: CancellationToken,
    ) -> Self {
        TcpServer {
            connection: Framed::new(tcp_stream, Codec),
            handler,
            shutdown_signal,
        }
    }

    /// Serves a client through a single connection.
    async fn serve(mut self) {
        loop {
            tokio::select! { biased;
                _ = self.shutdown_signal.cancelled() => {
                    let mut tcp_stream = self.connection.into_inner();
                    if let Err(error) = tcp_stream.shutdown().await {
                        let peer = tcp_stream
                            .peer_addr()
                            .map(|address| address.to_string())
                            .unwrap_or_else(|_| "an unknown peer".to_owned());
                        warn!("Failed to close connection to {peer}: {error:?}");
                    }
                    return;
                }
                result = self.connection.next() => match result {
                    Some(Ok(message)) => self.handle_message(message).await,
                    Some(Err(error)) => {
                        self.handle_error(error);
                        return;
                    }
                    None => break,
                },
            }
        }
    }

    /// Handles a single request message from a client.
    async fn handle_message(&mut self, message: RpcMessage) {
        if let Some(reply) = self.handler.handle_message(message).await {
            if let Err(error) = self.connection.send(reply).await {
                error!("Failed to send query response: {error}");
            }
        }
    }

    /// Handles an error received while attempting to receive from the connection.
    ///
    /// Ignores a successful connection termination, while logging an unexpected connection
    /// termination or any other error.
    fn handle_error(&self, error: codec::Error) {
        if !matches!(
            &error,
            codec::Error::IoError(error)
                if error.kind() == io::ErrorKind::UnexpectedEof
                || error.kind() == io::ErrorKind::ConnectionReset
        ) {
            error!("Error while reading TCP stream: {error}");
        }
    }
}