echosrv 0.1.0

A high-performance async echo server library built with Tokio for testing and development
Documentation

Echo Server

A simple TCP echo server written in Rust using Tokio, designed for testing and development purposes.

Features

  • Async TCP Server: Built with Tokio for high-performance async I/O
  • Connection Management: Handles multiple concurrent connections with configurable limits
  • Graceful Shutdown: Responds to SIGINT/SIGTERM signals and supports programmatic shutdown
  • Configurable Timeouts: Read and write timeouts to prevent hanging connections
  • Configurable Buffer Size: Adjustable buffer size for different use cases
  • Comprehensive Testing: Extensive test suite covering various scenarios
  • Binary Data Support: Handles both text and binary data
  • Unicode Support: Full UTF-8 support for international characters
  • High Concurrency: Can handle thousands of concurrent connections

Architecture

The echo server follows a simple but robust architecture:

Core Components

  • EchoServer: Main server that manages the TCP listener and connection handling
  • Config: Configuration struct for server settings (bind address, connection limits, timeouts, buffer size)
  • EchoClient: Test client for connecting to and testing the server
  • Connection Handler: Async task that handles individual client connections with timeouts

Design Principles

  • SOLID Principles: Clean separation of concerns with single responsibility
  • Async-First: All I/O operations are non-blocking using Tokio
  • Error Handling: Comprehensive error handling with context information and timeouts
  • Resource Management: Automatic cleanup of resources using Rust's ownership system
  • Thread Safety: Atomic connection counting for concurrent access

Configuration

The server can be configured through the Config struct:

let config = Config {
    bind_addr: "127.0.0.1:8080".parse().unwrap(),
    max_connections: 1000,
    buffer_size: 1024,
    read_timeout: Some(Duration::from_secs(30)),
    write_timeout: Some(Duration::from_secs(30)),
};

Configuration Options

  • bind_addr: Socket address to bind the server to
  • max_connections: Maximum number of concurrent connections
  • buffer_size: Buffer size for reading/writing data
  • read_timeout: Read timeout for connections (None for no timeout)
  • write_timeout: Write timeout for connections (None for no timeout)

Usage

Running the Server

# Run on default port 8080
cargo run

# Run on specific port
cargo run 9000

# Run in release mode for better performance
cargo run --release 9000

Testing the Server

# Run all tests
cargo test

# Run specific test
cargo test test_basic_echo

# Run tests with output
cargo test -- --nocapture

# Run tests with logging
RUST_LOG=info cargo test -- --nocapture

Manual Testing

You can test the server manually using netcat or telnet:

# Start the server
cargo run 8080

# In another terminal, connect with netcat
echo "Hello, Server!" | nc localhost 8080

# Or use telnet
telnet localhost 8080

Testing

The echo server includes comprehensive tests covering:

Unit Tests

  • Basic Echo: Simple text echo functionality
  • Multiple Messages: Multiple messages over single connection
  • Binary Data: Binary data handling
  • Large Data: Data larger than internal buffers
  • Unicode: International character support

Integration Tests

  • Concurrent Clients: Multiple simultaneous connections
  • Connection Limits: Server behavior under connection limits
  • Graceful Shutdown: Proper shutdown handling
  • Timeout Configuration: Timeout behavior testing
  • Stress Testing: High-load scenarios

Test Coverage

# Run tests with coverage (requires cargo-tarpaulin)
cargo tarpaulin --out Html

# Run specific test categories
cargo test --test integration_tests

Performance

The echo server is optimized for:

  • Low Latency: Async I/O for minimal response times
  • High Throughput: Efficient handling of multiple connections
  • Memory Efficiency: Minimal memory footprint per connection
  • CPU Efficiency: Non-blocking operations prevent CPU waste
  • Scalability: Can handle thousands of concurrent connections

Use Cases

This echo server is useful for:

  • Network Testing: Testing network connectivity and protocols
  • Load Testing: Stress testing network infrastructure
  • Development: Quick testing of network clients
  • Learning: Understanding TCP server implementation
  • Benchmarking: Performance testing of network stacks

Development

Building

# Debug build
cargo build

# Release build
cargo build --release

# Check for issues
cargo check
cargo clippy

Adding New Tests

To add new tests, add them to src/tests.rs:

#[tokio::test]
async fn test_new_feature() -> Result<()> {
    // Test implementation
    Ok(())
}

License

This project is licensed under the MIT License - see the LICENSE file for details.