8000 GitHub - pgalkin/NxLite: Lightning-fast HTTP server built in C
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

pgalkin/NxLite

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

9 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

NxLite

A high-performance HTTP server written in C, designed for maximum efficiency, edge caching, and production-grade performance.

πŸš€ Performance Highlights

  • 437,344 RPS peak throughput (500 concurrent connections)
  • 241.9% performance improvement with edge caching
  • Sub-millisecond latency (281ΞΌs average with caching)
  • 81% bandwidth reduction through intelligent caching
  • Production-ready scalability handling 26+ million requests/minute

✨ Features

Core Performance

  • Zero-Copy I/O: Uses sendfile() for efficient file transfers
  • Non-Blocking Architecture: Event-driven with epoll for maximum concurrency
  • Master-Worker Model: Pre-fork architecture similar to Nginx
  • Memory Pooling: Custom allocation system reduces fragmentation
  • Keep-Alive Support: Persistent connections for reduced latency

Edge Caching System

  • Intelligent Response Caching: Hash-based in-memory cache for static assets
  • ETag Support: RFC-compliant conditional requests with If-None-Match
  • 304 Not Modified: Optimized responses for unchanged content
  • Vary Header Handling: Cache awareness of Accept-Encoding and User-Agent
  • Cache Invalidation: Time-based expiration with configurable timeouts

Compression Engine

  • Gzip/Deflate Support: Automatic content compression for compatible clients
  • MIME-Type Awareness: Intelligent compression based on content type
  • Adaptive Compression Levels: Optimized settings per content type
  • Bandwidth Optimization: Up to 81% reduction in data transfer

HTTP Features

  • HTTP/1.1 Compliant: Full support for modern HTTP features
  • Multiple Methods: GET, HEAD, POST support
  • Range Requests: Partial content delivery
  • MIME Type Detection: Automatic content-type headers
  • Cache-Control Headers: Proper caching directives
  • Last-Modified Support: File-based modification tracking

πŸ“Š Benchmark Results

Edge Caching Performance

Scenario RPS Improvement Transfer Rate Latency
Baseline 91,216 - 0.90 GB/s 1.09ms
33% Cache 311,891 +241.9% 2.07 GB/s 281ΞΌs
50% Cache 284,923 +212.3% 1.42 GB/s 261ΞΌs
80% Cache 272,238 +198.5% 571 MB/s 346ΞΌs

Stress Testing

Test Concurrency RPS Transfer Rate
Medium Load 200 311,891 2.07 GB/s
High Load 500 437,344 2.90 GB/s
Sustained (60s) 300 272,238 571 MB/s

Compression Performance

Mode RPS Transfer Rate Compression Ratio
No Compression 91,216 0.90 GB/s -
Gzip 34,065 100 MB/s ~70%
Mixed Compression 98,149 244 MB/s ~60%

πŸ—οΈ Architecture

Master-Worker Model

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Master Process                       β”‚
β”‚  β€’ Configuration Management  β€’ Worker Monitoring        β”‚
β”‚  β€’ Socket Creation          β€’ Graceful Shutdown         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                  β”‚
      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
      β”‚           β”‚           β”‚           β”‚
β”Œβ”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β” β”Œβ”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”
β”‚ Worker 1 β”‚ β”‚ Worker 2β”‚ β”‚ Worker 3β”‚ β”‚ Worker N β”‚
β”‚ β€’ epoll  β”‚ β”‚ β€’ epoll β”‚ β”‚ β€’ epoll β”‚ β”‚ β€’ epoll  β”‚
β”‚ β€’ cache  β”‚ β”‚ β€’ cache β”‚ β”‚ β€’ cache β”‚ β”‚ β€’ cache  β”‚
β”‚ β€’ gzip   β”‚ β”‚ β€’ gzip  β”‚ β”‚ β€’ gzip  β”‚ β”‚ β€’ gzip   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Request Processing Pipeline

Client Request
      ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    Cache Hit?    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Parse      β”‚ ──────────────→  β”‚  Cached     β”‚
β”‚  Headers    β”‚                  β”‚  Response   β”‚
β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜                  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
      β”‚ Cache Miss                      ↓
      ↓                           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                   β”‚  Send       β”‚
β”‚  File       β”‚ ←──── ETag ────── β”‚  304/200    β”‚
β”‚  System     β”‚      Check       β”‚  Response   β”‚
β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜                   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
      β”‚
      ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    Compress?     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Content    β”‚ ──────────────→  β”‚  Gzip/      β”‚
β”‚  Type       β”‚                  β”‚  Deflate    β”‚
β”‚  Check      β”‚                  β”‚  Content    β”‚
β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜                  β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
      β”‚                                β”‚
      ↓                                ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Cache      β”‚ ←────────────────│  Send       β”‚
β”‚  Response   β”‚                  β”‚  Response   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ› οΈ Building and Running

Prerequisites

# Ubuntu/Debian
sudo apt update
sudo apt install build-essential cmake libz-dev

# CentOS/RHEL
sudo yum install gcc cmake make zlib-devel

# macOS
brew install cmake

Build Instructions

# Clone the repository
git clone https://github.com/dexter-xd/NxLite.git
cd NxLite

# Create build directory
mkdir build && cd build

# Configure and build
cmake ..
make -j$(nproc)

# The binary will be at: build/nxlite

Running the Server

# Run with default configuration
./nxlite

# Run with custom config
./nxlite ../server.conf

# Background execution
nohup ./nxlite > server.log 2>&1 &

βš™οΈ Configuration

Example Configuration (server.conf)

# Basic Settings
port=7877
worker_processes=auto
root=../static

# Performance Tuning
max_connections=100000
keep_alive_timeout=120
cache_timeout=3600
cache_size=10000

# Logging
log_level=INFO
access_log=./logs/access.log
error_log=./logs/error.log

Configuration Options

Parameter Default Description
port 7877 Server listening port
worker_processes 4 Number of worker processes
root ../static Document root directory
max_connections 10000 Maximum concurrent connections
keep_alive_timeout 60 Keep-alive timeout (seconds)
cache_timeout 3600 Response cache TTL (seconds)
cache_size 10000 Maximum cached responses

πŸ“ˆ Benchmarking

Quick Performance Test

cd benchmark

# Fast validation (10 seconds)
./quick_test.sh

# Comprehensive testing (30 seconds per test)
./comprehensive_test.sh

# Production simulation (60+ seconds)
./production_test.sh

Manual Testing with wrk

# Baseline performance
wrk -t4 -c100 -d30s http://localhost:7877/

# Edge caching test (33% conditional)
wrk -t4 -c100 -d30s -s mixed_cache.lua http://localhost:7877/

# Stress test (500 connections)
wrk -t8 -c500 -d30s -s mixed_cache.lua http://localhost:7877/

# Compression test
wrk -t4 -c100 -d30s -H 'Accept-Encoding: gzip' http://localhost:7877/

Real-World Simulation

# Mixed content types with caching
wrk -t4 -c100 -d60s -s real_world.lua http://localhost:7877/

# Heavy caching scenario (80% repeat visitors)
wrk -t4 -c100 -d30s -s heavy_cache.lua http://localhost:7877/

πŸ”§ Performance Tuning

System Limits

# /etc/sysctl.conf
fs.file-max = 100000
net.core.somaxconn = 65536
net.ipv4.tcp_max_syn_backlog = 65536
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300

# Apply changes
sudo sysctl -p

Process Limits

# /etc/security/limits.conf
* soft nofile 100000
* hard nofile 100000

# Verify
ulimit -n

Optimal Settings

# For 8-core server
worker_processes=8
max_connections=50000
keep_alive_timeout=120

# For high-traffic sites
cache_size=50000
cache_timeout=7200

πŸ“‹ Monitoring and Logs

Log Files

# Real-time access log
tail -f build/logs/access.log

# Error monitoring
tail -f build/logs/error.log

# Performance metrics
grep "Cache hit" build/logs/nxlite.log

Health Checks

# Basic connectivity
curl -I http://localhost:7877/

# Cache validation
curl -H "If-None-Match: \"some-etag\"" http://localhost:7877/

# Compression check
curl -H "Accept-Encoding: gzip" -v http://localhost:7877/

πŸ† Production Deployment

Recommended Setup

  1. Load Balancer: Place behind nginx/HAProxy for SSL termination
  2. Monitoring: Use Prometheus + Grafana for metrics
  3. Logging: Centralized logging with ELK stack
  4. Caching: Configure appropriate cache sizes for your workload

Docker Deployment

FROM ubuntu:22.04
RUN apt update && apt install -y build-essential cmake libz-dev
COPY . /app
WORKDIR /app
RUN mkdir build && cd build && cmake .. && make
EXPOSE 7877
CMD ["./build/nxlite"]

🀝 Contributing

We welcome contributions! Please see our Contributing Guide for details.

Development Setup

# Install development dependencies
sudo apt install valgrind gdb clang-format

# Run tests
cd benchmark && ./comprehensive_test.sh

# Memory leak check
valgrind --leak-check=full ./build/nxlite

πŸ“ License

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

πŸ™ Acknowledgments

  • Inspired by nginx architecture
  • Built with modern C performance practices
  • Benchmarked against industry standards

NxLite - Where performance meets simplicity. Ready for production. πŸš€

About

Lightning-fast HTTP server built in C

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C 77.5%
  • Shell 9.8%
  • HTML 7.0%
  • Lua 4.8%
  • CMake 0.9%
  • CSS 0.0%
0