tinyredis 1.0.0

A Redis-compatible server written in Rust. Uses RESP2, persists writes to an append-only file, and accepts connections from any standard Redis client.
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
use std::fmt::Write as _;
use std::sync::Arc;
use std::sync::atomic::Ordering;
use std::time::{Duration, Instant};

use bytes::Bytes;
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::{Mutex, broadcast};

use crate::commands;
use crate::connection::Connection;
use crate::parser::{self, Frame};
use crate::persistence::AofSender;
use crate::stats::{ClientEntry, SharedStats};
use crate::store::Store;

type SharedStore = Arc<Mutex<Store>>;

/// Run the accept loop and expiry sweeper until `shutdown_tx` fires.
/// Pass `aof: None` to disable persistence (useful in tests).
pub async fn serve(
    listener: TcpListener,
    store: SharedStore,
    aof: Option<AofSender>,
    shutdown_tx: broadcast::Sender<()>,
    stats: SharedStats,
) {
    // Expiry sweeper
    {
        let store = Arc::clone(&store);
        let mut shutdown = shutdown_tx.subscribe();
        tokio::spawn(async move {
            let mut interval = tokio::time::interval(Duration::from_secs(1));
            loop {
                tokio::select! {
                    _ = interval.tick() => store.lock().await.purge_expired(),
                    _ = shutdown.recv() => break,
                }
            }
        });
    }

    // Accept loop
    let mut shutdown = shutdown_tx.subscribe();
    loop {
        tokio::select! {
            accept = listener.accept() => {
                let (socket, addr) = match accept {
                    Ok(a) => a,
                    Err(e) => { tracing::error!("accept error: {e}"); continue; }
                };
                tracing::debug!("accepted {addr}");
                let store = Arc::clone(&store);
                let aof = aof.clone();
                let stats = Arc::clone(&stats);
                tokio::spawn(handle_connection(socket, store, aof, stats));
            }
            _ = shutdown.recv() => break,
        }
    }
}

async fn handle_connection(
    socket: TcpStream,
    store: SharedStore,
    aof: Option<AofSender>,
    stats: SharedStats,
) {
    stats.connected_clients.fetch_add(1, Ordering::Relaxed);
    stats
        .total_connections_received
        .fetch_add(1, Ordering::Relaxed);

    // Capture addresses before the socket is moved into Connection.
    let peer_addr = socket
        .peer_addr()
        .map(|a| a.to_string())
        .unwrap_or_default();
    let local_addr = socket
        .local_addr()
        .map(|a| a.to_string())
        .unwrap_or_default();

    // Assign a unique monotonic client ID and register in the global registry.
    let client_id = stats.next_client_id.fetch_add(1, Ordering::Relaxed) + 1;
    {
        let mut clients = stats.clients.lock().unwrap();
        clients.insert(
            client_id,
            ClientEntry {
                id: client_id,
                addr: peer_addr,
                laddr: local_addr,
                name: String::new(),
                connected_at: Instant::now(),
                last_cmd: String::new(),
                multi: -1,
            },
        );
    }

    // Connections start authenticated only if no password is required.
    let mut authenticated = stats.requirepass.is_none();

    // Transaction state.
    let mut in_multi = false;
    let mut tx_queue: Vec<parser::Command> = Vec::new();

    // Local copy of this connection's name (avoids locking the registry for every command).
    let mut client_name = String::new();

    let mut conn = Connection::new(socket);
    while let Ok(Some(frame)) = conn.read_frame().await {
        let cmd = match parser::Command::from_frame(frame) {
            Ok(c) => c,
            Err(e) => {
                conn.write_frame(&Frame::Error(format!("ERR {e}")));
                if conn.flush().await.is_err() {
                    break;
                }
                continue;
            }
        };
        stats
            .total_commands_processed
            .fetch_add(1, Ordering::Relaxed);

        // QUIT — always allowed; discards any open transaction.
        if cmd.name == "QUIT" {
            set_last_cmd(&stats, client_id, "quit");
            conn.write_frame(&Frame::Simple("OK".into()));
            let _ = conn.flush().await;
            break;
        }

        // AUTH — always executed immediately, even inside MULTI.
        if cmd.name == "AUTH" {
            set_last_cmd(&stats, client_id, "auth");
            let resp = handle_auth(&cmd, &stats, &mut authenticated);
            conn.write_frame(&resp);
            if conn.flush().await.is_err() {
                break;
            }
            continue;
        }

        // Gate all other commands on authentication.
        if !authenticated {
            conn.write_frame(&Frame::Error("NOAUTH Authentication required.".into()));
            if conn.flush().await.is_err() {
                break;
            }
            continue;
        }

        // MULTI — start a transaction.
        if cmd.name == "MULTI" {
            set_last_cmd(&stats, client_id, "multi");
            let resp = if in_multi {
                Frame::Error("ERR MULTI calls can not be nested".into())
            } else {
                in_multi = true;
                set_multi(&stats, client_id, 0);
                Frame::Simple("OK".into())
            };
            conn.write_frame(&resp);
            if conn.flush().await.is_err() {
                break;
            }
            continue;
        }

        // DISCARD — abort the transaction.
        if cmd.name == "DISCARD" {
            set_last_cmd(&stats, client_id, "discard");
            let resp = if in_multi {
                in_multi = false;
                tx_queue.clear();
                set_multi(&stats, client_id, -1);
                Frame::Simple("OK".into())
            } else {
                Frame::Error("ERR DISCARD without MULTI".into())
            };
            conn.write_frame(&resp);
            if conn.flush().await.is_err() {
                break;
            }
            continue;
        }

        // EXEC — execute all queued commands.
        if cmd.name == "EXEC" {
            set_last_cmd(&stats, client_id, "exec");
            if !in_multi {
                conn.write_frame(&Frame::Error("ERR EXEC without MULTI".into()));
                if conn.flush().await.is_err() {
                    break;
                }
                continue;
            }
            in_multi = false;
            set_multi(&stats, client_id, -1);
            let queue = std::mem::take(&mut tx_queue);
            let mut results = Vec::with_capacity(queue.len());
            for queued_cmd in queue {
                let resp = commands::dispatch(
                    queued_cmd,
                    Arc::clone(&store),
                    aof.clone(),
                    Arc::clone(&stats),
                )
                .await;
                results.push(resp);
            }
            conn.write_frame(&Frame::Array(results));
            if conn.flush().await.is_err() {
                break;
            }
            continue;
        }

        // CLIENT — per-connection metadata commands.
        if cmd.name == "CLIENT" {
            let resp = handle_client_cmd(
                &cmd,
                &stats,
                client_id,
                &mut client_name,
                in_multi,
                tx_queue.len(),
            );
            // Record last_cmd with subcommand (e.g. "client|list").
            let subcmd_lower = cmd
                .args
                .first()
                .and_then(|b| std::str::from_utf8(b).ok())
                .map(|s| s.to_ascii_lowercase())
                .unwrap_or_default();
            set_last_cmd(&stats, client_id, &format!("client|{subcmd_lower}"));
            conn.write_frame(&resp);
            if conn.flush().await.is_err() {
                break;
            }
            continue;
        }

        // Inside MULTI: queue the command and reply QUEUED.
        if in_multi {
            tx_queue.push(cmd);
            set_multi(&stats, client_id, tx_queue.len() as i64);
            conn.write_frame(&Frame::Simple("QUEUED".into()));
            if conn.flush().await.is_err() {
                break;
            }
            continue;
        }

        // Normal (non-transaction) execution.
        set_last_cmd(&stats, client_id, &cmd.name.to_lowercase());
        let resp =
            commands::dispatch(cmd, Arc::clone(&store), aof.clone(), Arc::clone(&stats)).await;
        conn.write_frame(&resp);
        if conn.flush().await.is_err() {
            break;
        }
    }

    // Deregister this connection from the global registry.
    stats.clients.lock().unwrap().remove(&client_id);
    stats.connected_clients.fetch_sub(1, Ordering::Relaxed);
}

// ── per-connection registry helpers ──────────────────────────────────────────

fn set_last_cmd(stats: &crate::stats::ServerStats, id: u64, cmd: &str) {
    if let Ok(mut clients) = stats.clients.lock()
        && let Some(e) = clients.get_mut(&id)
    {
        e.last_cmd = cmd.to_string();
    }
}

fn set_multi(stats: &crate::stats::ServerStats, id: u64, multi: i64) {
    if let Ok(mut clients) = stats.clients.lock()
        && let Some(e) = clients.get_mut(&id)
    {
        e.multi = multi;
    }
}

// ── CLIENT subcommand handler ─────────────────────────────────────────────────

fn handle_client_cmd(
    cmd: &parser::Command,
    stats: &crate::stats::ServerStats,
    client_id: u64,
    client_name: &mut String,
    in_multi: bool,
    tx_queue_len: usize,
) -> Frame {
    let subcmd = match cmd.args.first() {
        Some(b) => String::from_utf8_lossy(b).to_ascii_uppercase(),
        None => {
            return Frame::Error("ERR wrong number of arguments for 'client' command".into());
        }
    };

    match subcmd.as_str() {
        "SETNAME" => {
            if cmd.args.len() != 2 {
                return Frame::Error(
                    "ERR wrong number of arguments for 'client|setname' command".into(),
                );
            }
            let name = match std::str::from_utf8(&cmd.args[1]) {
                Ok(s) => s,
                Err(_) => {
                    return Frame::Error(
                        "ERR Client names cannot contain spaces, newlines or special characters."
                            .into(),
                    );
                }
            };
            // Name must not contain spaces or control characters.
            if name.bytes().any(|b| b <= b' ') {
                return Frame::Error(
                    "ERR Client names cannot contain spaces, newlines or special characters."
                        .into(),
                );
            }
            *client_name = name.to_string();
            if let Ok(mut clients) = stats.clients.lock()
                && let Some(e) = clients.get_mut(&client_id)
            {
                e.name = name.to_string();
            }
            Frame::Simple("OK".into())
        }

        "GETNAME" => {
            if client_name.is_empty() {
                Frame::Null
            } else {
                Frame::Bulk(Bytes::from(client_name.clone()))
            }
        }

        "ID" => Frame::Integer(client_id as i64),

        "LIST" => {
            let clients = match stats.clients.lock() {
                Ok(c) => c,
                Err(_) => return Frame::Error("ERR internal error".into()),
            };
            let now = Instant::now();
            // Collect and sort by ID for deterministic output.
            let mut entries: Vec<&ClientEntry> = clients.values().collect();
            entries.sort_by_key(|e| e.id);

            let mut output = String::new();
            for entry in entries {
                let age = now.duration_since(entry.connected_at).as_secs();
                // Reflect the current transaction depth for ourselves.
                let multi = if entry.id == client_id && in_multi {
                    tx_queue_len as i64
                } else {
                    entry.multi
                };
                let _ = writeln!(
                    output,
                    "id={id} addr={addr} laddr={laddr} fd=-1 name={name} age={age} \
                     idle=0 flags=N db=0 sub=0 psub=0 multi={multi} watch=0 qbuf=0 \
                     qbuf-free=0 argv-mem=0 multi-mem=0 tot-mem=0 rbs=0 rbp=0 oll=0 \
                     omem=0 events=r cmd={cmd} user=default resp=2",
                    id = entry.id,
                    addr = entry.addr,
                    laddr = entry.laddr,
                    name = entry.name,
                    cmd = entry.last_cmd,
                );
            }
            Frame::Bulk(Bytes::from(output))
        }

        _ => Frame::Error(format!(
            "ERR unknown subcommand '{}' for 'client' command",
            subcmd.to_ascii_lowercase()
        )),
    }
}

/// Handle the AUTH command, updating `authenticated` on success.
fn handle_auth(
    cmd: &parser::Command,
    stats: &crate::stats::ServerStats,
    authenticated: &mut bool,
) -> Frame {
    match &stats.requirepass {
        None => Frame::Error(
            "ERR Client sent AUTH, but no password is set. \
             Did you mean ACL SETUSER with >password?"
                .into(),
        ),
        Some(required) => {
            // AUTH password  OR  AUTH username password (username ignored)
            let given = match cmd.args.len() {
                1 => std::str::from_utf8(&cmd.args[0]).unwrap_or(""),
                2 => std::str::from_utf8(&cmd.args[1]).unwrap_or(""),
                _ => {
                    return Frame::Error("ERR wrong number of arguments for 'auth' command".into());
                }
            };
            if given == required.as_str() {
                *authenticated = true;
                Frame::Simple("OK".into())
            } else {
                *authenticated = false;
                Frame::Error("WRONGPASS invalid username-password pair or user is disabled.".into())
            }
        }
    }
}