Skip to content

Commit f187085

Browse files
authored
Improve accept logic (#21)
Handle accept errors using an exponential backoff strategy. Also uses a semaphore to limit the max number connections. Adds inline documentation and comments.
1 parent 2115ebf commit f187085

File tree

2 files changed

+317
-15
lines changed

2 files changed

+317
-15
lines changed

src/lib.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,29 @@
1-
pub const DEFAULT_PORT: &str = "6379";
1+
//! A minimal (i.e. very incomplete) implementation of a Redis server and
2+
//! client.
3+
//!
4+
//! The purpose of this project is to provide a larger example of an
5+
//! asynchronous Rust project built with Tokio. Do not attempt to run this in
6+
//! production... seriously.
7+
//!
8+
//! # Layout
9+
//!
10+
//! The library is structured such that it can be used with guides. There are
11+
//! modules that are public that probably would not be public in a "real" redis
12+
//! client library.
13+
//!
14+
//! The major components are:
15+
//!
16+
//! * `server`: Redis server implementation. Includes a single `run` function
17+
//! that takes a `TcpListener` and starts accepting redis client connections.
18+
//!
19+
//! * `client`: an asynchronous Redis client implementation. Demonstrates how to
20+
//! build clients with Tokio.
21+
//!
22+
//! * `cmd`: implementations of the supported Redis commands.
23+
//!
24+
//! * `frame`: represents a single Redis protocol frame. A frame is used as an
25+
//! intermediate representation between a "command" and the byte
26+
//! representation.
227
328
pub mod client;
429

@@ -22,8 +47,25 @@ pub mod server;
2247
mod shutdown;
2348
use shutdown::Shutdown;
2449

50+
/// Default port that a redis server listens on.
51+
///
52+
/// Used if no port is specified.
53+
pub const DEFAULT_PORT: &str = "6379";
54+
2555
/// Error returned by most functions.
56+
///
57+
/// When writing a real application, one might want to consider a specialized
58+
/// errror handling crate or defining an error type as an `enum` of causes.
59+
/// However, for our example, using a boxed `std::error::Error` is sufficient.
60+
///
61+
/// For performance reasons, boxing is avoided in any hot path. For example, in
62+
/// `parse`, a custom error `enum` is defined. This is because the error is hit
63+
/// and handled during normal execution when a partial frame is received on a
64+
/// socket. `std::error::Error` is implemented for `parse::Error` which allows
65+
/// it to be converted to `Box<dyn std::error::Error>`.
2666
pub type Error = Box<dyn std::error::Error + Send + Sync>;
2767

2868
/// A specialized `Result` type for mini-redis operations.
69+
///
70+
/// This is defined as a convenience.
2971
pub type Result<T> = std::result::Result<T, Error>;

0 commit comments

Comments
 (0)