resp_async/
io.rs

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
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;

use bytes::BytesMut;
use log::{error, info};
use std::future::Future;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::{broadcast, mpsc};

use crate::error::*;
use crate::resp::*;

const DEFAULT_ADDRESS: &str = "127.0.0.1:6379";

#[derive(Debug)]
pub struct PeerContext<T>
where
    T: Default,
{
    peer: SocketAddr,
    local: SocketAddr,
    ctx: HashMap<String, Value>,
    pub user_data: T,
}

impl<T> PeerContext<T>
where
    T: Default,
{
    pub fn set<K>(&mut self, key: K, value: Value) -> Option<Value>
    where
        K: Into<String>,
    {
        self.ctx.insert(key.into(), value)
    }

    pub fn contains_key(&self, key: &str) -> bool {
        self.ctx.contains_key(key)
    }

    pub fn get(&mut self, key: &str) -> Option<&Value> {
        self.ctx.get(key)
    }

    pub fn get_mut(&mut self, key: &str) -> Option<&mut Value> {
        self.ctx.get_mut(key)
    }

    pub fn peer_addr(&self) -> &SocketAddr {
        &self.peer
    }

    pub fn local_addr(&self) -> &SocketAddr {
        &self.local
    }
}

pub trait EventHandler {
    type ClientUserData: Default + Send + Sync;

    fn on_request(
        &self,
        peer: &mut PeerContext<Self::ClientUserData>,
        request: Value,
    ) -> impl Future<Output = Result<Value>> + Send;
    fn on_connect(&self, _id: u64) -> impl Future<Output = Result<Self::ClientUserData>> + Send {
        async { Ok(Self::ClientUserData::default()) }
    }
    fn on_disconnect(&self, _id: u64) -> impl Future<Output = ()> + Send {
        async {}
    }
}

struct Shutdown {
    is_shutdown: bool,
    notify: broadcast::Receiver<()>,
}

impl Shutdown {
    pub(crate) fn new(notify: broadcast::Receiver<()>) -> Shutdown {
        Shutdown {
            is_shutdown: false,
            notify,
        }
    }

    pub(crate) async fn recv(&mut self) {
        if self.is_shutdown {
            return;
        }
        let _ = self.notify.recv().await;
        self.is_shutdown = true;
    }

    pub(crate) fn is_shutdown(&self) -> bool {
        self.is_shutdown
    }
}

pub struct Server<H>
where
    H: EventHandler + Send + Sync + 'static,
{
    handler: Arc<H>,
    address: String,
    client_id: Arc<AtomicU64>,
}

impl<H> Server<H>
where
    H: EventHandler + Send + Sync + 'static,
{
    pub fn new(handler: H) -> Self {
        Server {
            handler: Arc::new(handler),
            address: DEFAULT_ADDRESS.into(),
            client_id: Arc::new(AtomicU64::default()),
        }
    }

    pub fn listen(&mut self, addr: impl Into<String>) -> Result<&mut Self> {
        self.address = addr.into();
        Ok(self)
    }

    async fn run_client_loop(
        user_data: H::ClientUserData,
        handler: Arc<H>,
        mut socket: TcpStream,
    ) -> Result<()> {
        let mut client = PeerContext {
            peer: socket.peer_addr()?,
            local: socket.local_addr()?,
            ctx: HashMap::new(),
            user_data,
        };
        let mut rd = BytesMut::new();
        let mut wr = BytesMut::new();
        let mut decoder = ValueDecoder::default();
        loop {
            if let Some(value) = decoder.try_decode(&mut rd)? {
                handler
                    .on_request(&mut client, value)
                    .await?
                    .encode(&mut wr);
                socket.write_all(&wr).await?;
                wr.clear();
                socket.flush().await?;
            }

            if 0 == socket.read_buf(&mut rd).await? && rd.is_empty() {
                return Ok(());
            }
        }
    }

    async fn run_client_hooks(id: u64, handler: Arc<H>, socket: TcpStream) -> Result<()> {
        let user_data = handler.on_connect(id).await?;
        let result = Self::run_client_loop(user_data, Arc::clone(&handler), socket).await;
        handler.on_disconnect(id).await;
        result
    }

    async fn run_client(
        id: u64,
        handler: Arc<H>,
        socket: TcpStream,
        notify_shutdown: broadcast::Sender<()>,
        _shutdown_complete_tx: mpsc::Sender<()>,
    ) -> Result<()> {
        let mut shutdown = Shutdown::new(notify_shutdown.subscribe());
        tokio::select! {
            res = Self::run_client_hooks(id, handler, socket) => { res }
            _ = shutdown.recv() => { Ok(()) }
        }
    }

    async fn run_accept_loop(
        &mut self,
        listener: TcpListener,
        notify_shutdown: broadcast::Sender<()>,
        shutdown_complete_tx: mpsc::Sender<()>,
    ) -> Result<()> {
        let mut shutdown = Shutdown::new(notify_shutdown.subscribe());
        while !shutdown.is_shutdown() {
            tokio::select! {
                res = listener.accept() => {
                    if let Ok((socket, _)) = res {
                        let handler = Arc::clone(&self.handler);
                        let client_id = Arc::clone(&self.client_id);
                        let notify_shutdown = Clone::clone(&notify_shutdown);
                        let shutdown_complete_tx = Clone::clone(&shutdown_complete_tx);
                        tokio::spawn(async move {
                            Self::run_client(client_id.fetch_add(1, Ordering::AcqRel), handler, socket, notify_shutdown, shutdown_complete_tx).await
                        });
                    }
                },
                _ = shutdown.recv() => {}
            }
        }
        Ok(())
    }

    pub async fn serve(&mut self, shutdown: impl Future) -> Result<()> {
        let listener = TcpListener::bind(&self.address).await?;

        let (notify_shutdown, _) = broadcast::channel(1);
        let (shutdown_complete_tx, mut shutdown_complete_rx) = mpsc::channel(1);

        tokio::select! {
            res = self.run_accept_loop(listener, Clone::clone(&notify_shutdown), Clone::clone(&shutdown_complete_tx)) => {
                if let Err(err) = res {
                    error!("Failed to accept {:?}", err);
                }
            }
            _ = shutdown => {
                info!("shutting down server");
            }
        }

        drop(notify_shutdown);
        drop(shutdown_complete_tx);

        let _ = shutdown_complete_rx.recv().await;
        Ok(())
    }
}