How youngyangyang04/kama-webserver works
Project type and purpose
• This is a high-performance C++ web server inspired by the Muduo library, implementing a multi-threaded, multi-Reactor model.
• It includes custom memory pooling, asynchronous double-buffered logging, and an LFU cache.
File structure
• Top-level directories:
• / – public headers for all modules
• src/ – core server implementation and main()
• log/ – asynchronous logging implementation
• memory/ – memory pool implementation
• lib/ – any shared libraries
• img/, LICENSE, CMakeLists.txt, README.md
• Core modules under include/ and src/ correspond one-to-one:
• EventLoop, Channel, Poller/EPollPoller – Reactor pattern
• EventLoopThread, EventLoopThreadPool – “one loop per thread”
• Acceptor, TcpServer, TcpConnection, Socket, InetAddress – connection management
• Buffer – I/O buffering
• Thread, Timer, TimerQueue – threading and timed callbacks
• Logger, AsyncLogging, LogFile, LogStream – async logging
• memoryPool – custom allocator
• LFU, KICachePolicy – cache eviction policy
README for additional insights
• Confirms use of multi-Reactor + thread pool model
• Highlights memory pool, async double buffer logging, LFU cache as differentiators
• Describes module responsibilities (network, logging, memory, cache)
Main components and interactions
a. Components:
• Main Thread & MainLoop (EventLoop instance)
• Acceptor (listens on socket, registers Channel with main loop)
• Sub-Reactors (EventLoopThreads in the pool)
• Poller/EPollPoller (inside each EventLoop)
• Channels (one per FD, handle read/write callbacks)
• TcpServer (manages acceptor + loop pool)
• TcpConnection (per-connection state, read/write handlers)
• Buffer (per-connection input/output buffer)
• Thread Pool (EventLoopThreadPool)
• TimerQueue (for timers)
• AsyncLogging (logging thread with double buffer)
• MemoryPool (custom allocator for fixed blocks)
• LFU Cache (optional, used to cache content)
• External Clients (e.g., nc or browser)
• Filesystem (static file serving, if any)
b. Relationships / Data flow:
1. main() → creates EventLoop (main loop)
2. main() → creates EventLoopThreadPool (N subloops)
3. main() → constructs TcpServer(mainLoop, address, threadNum)
4. TcpServer starts:
• Acceptor binds/listens
• Registers acceptor's Channel on mainLoop’s Poller
5. New connection → Acceptor callback → hands new socket to subloop (round-robin)
6. Subloop:
• Creates TcpConnection object
• Registers its read/write Channel on Poller
7. When data arrives → Poller wakes EventLoop → Channel invokes TcpConnection::handleRead → Buffer → user callback (echo) → handleWrite
8. Logging: every Logger::log() → appends to AsyncLogging’s in-memory buffer → background logging thread flushes to disk
9. MemoryPool services all buffer allocations
10. LFU cache invoked by user-defined handler to cache static content
c. Architectural patterns / design principles
• Reactor: EventLoop + Poller + Channel
• Proactor-like for write callbacks
• One-loop-per-thread for concurrency
• Separation of concerns: networking, threading, logging, memory, caching
• Nonblocking I/O + edge-triggered epoll under the hood
• Double buffering to decouple log producer/consumer
d. Key technologies / frameworks
• C++17 (or later)
• Linux epoll
• pthreads (wrapped in Thread class)
• CMake build system