shoal_core/server/
messages.rs

1//! The different messages that can be sent in shoal
2
3use bytes::BytesMut;
4use std::net::SocketAddr;
5use uuid::Uuid;
6
7use super::shard::ShardInfo;
8use crate::shared::traits::ShoalDatabase;
9
10/// The messages that can be sent in shoal
11#[derive(Debug)]
12pub enum Msg<S: ShoalDatabase> {
13    /// A message from our node local mesh
14    Mesh { shard: usize, msg: MeshMsg<S> },
15    /// A message from a client
16    Client {
17        /// The address this request came from
18        addr: SocketAddr,
19        /// The number of bytes to read
20        read: usize,
21        /// The raw data for our request
22        data: BytesMut,
23    },
24    /// Tell Shoal to shutdown
25    Shutdown,
26}
27
28/// The metadata about a query from a client
29#[derive(Debug)]
30pub struct QueryMetadata {
31    /// The address of the client this query came from
32    pub addr: SocketAddr,
33    /// The id for this query
34    pub id: Uuid,
35    /// This queries index in the queries vec
36    pub index: usize,
37    /// Whether this is the last query in a query bundle
38    pub end: bool,
39}
40
41impl QueryMetadata {
42    /// Create a new query metadata object
43    ///
44    /// # Arguments
45    ///
46    /// * `addr` - The address to respond to this query at
47    /// * `id` - The id of this query
48    /// * `index` - The index for this query in a bundle of queries
49    /// * `end` - Whether this is the last query in a bundle or not
50    pub fn new(addr: SocketAddr, id: Uuid, index: usize, end: bool) -> Self {
51        QueryMetadata {
52            addr,
53            id,
54            index,
55            end,
56        }
57    }
58}
59
60/// The messages that can be sent over of node local mesh
61#[derive(Debug)]
62pub enum MeshMsg<S: ShoalDatabase> {
63    /// Join this nodes token ring
64    Join(ShardInfo),
65    /// A query to execute
66    Query {
67        /// The metadata about a query
68        meta: QueryMetadata,
69        /// The query to execute
70        query: S::QueryKinds,
71    },
72    /// Tell this shard to shutdown
73    Shutdown,
74}
75
76/// The messages that can be sent between workers in a shard
77pub enum ShardMsg<D: ShoalDatabase> {
78    /// A query to execute
79    Query {
80        /// The metadata about a query
81        meta: QueryMetadata,
82        /// The query to execute
83        query: D::QueryKinds,
84    },
85    /// Tell this shard to shutdown
86    Shutdown,
87}