How dexter-xd/nxlite works
Project Type and Purpose
• NxLite is a standalone, high-performance HTTP server written in C.
• It follows a master-worker (pre-fork) model to handle thousands of concurrent connections efficiently, using epoll for non-blocking I/O, zero-copy sendfile, in-memory caching, and a custom memory pool.
File Structure Analysis
• Top-level: CMake build files, configuration (server.conf), static content (static/index.html), headers (include/*.h), and source code (src/*.c).
• src/main.c initializes the server (reads config, forks master), src/master.c manages worker processes, src/worker.c contains per-connection logic, src/http.c and src/server.c parse/process requests, src/mempool.c implements custom allocator, src/log.c handles logging, src/shutdown.c handles graceful shutdown.
Key Components
a. External Clients – browsers or benchmarking tools (ab, wrk, hey)
b. Master Process
• Reads server.conf
• Creates and binds listening socket
• Forks N worker processes and supervises them
• Handles graceful shutdown/restart
c. Shared Listening Socket
• A single socket descriptor passed via fork to all workers
d. Worker Processes (multiple instances)
• Each runs an event loop (epoll)
• Accepts new connections from the shared socket
• Parses HTTP requests (http.c)
• Uses memory pool (mempool.c) for buffers
• Checks in-memory cache for static assets (simple hash or lookup)
• Serves static files using zero-copy sendfile()
• Writes access/error logs (log.c)
• Manages keep-alive and connection teardown
e. Operating System Kernel
• epoll subsystem
• sendfile zero-copy path
• TCP/IP stack
f. File System
• Static root directory (static/)
• Log files (e.g., build/logs/access.log)
Relationships and Data Flow
• Clients → TCP connection → Master Process’s listening socket
• Shared Listening Socket → Worker’s epoll wait → Worker accepts
• Worker: parse request → check cache →
• cache hit → generate response in memory → send over socket
• cache miss → open file → sendfile() → store in cache → send over socket
• Worker → writes logs → Log file on disk
• Master ↔ Worker (monitoring via signals or IPC for health checks)
Architectural Patterns & Design Principles
• Master-Worker (pre-fork) pattern for process isolation and multi-core utilization
• Event-driven, non-blocking I/O (epoll) for high concurrency
• Zero-copy I/O (sendfile) for minimal CPU overhead on file transfers
• Custom memory pooling to reduce fragmentation and allocator overhead
• Simple in-memory caching for static assets
Detail and Clarity
• Shared listening socket as a central hub fed into each worker.
• Explicitly capture the OS kernel’s role in epoll and sendfile.