How sylar-yin/sylar works
Project Type and Purpose
• Type: A C++ high-performance, full-featured server framework / library.
• Purpose: Provide building blocks (logging, configuration, threading, coroutine scheduling, I/O multiplexing, networking, protocol implementations, ORM, etc.) so that developers can rapidly assemble custom servers (HTTP, WebSocket, RPC, etc.) and services.
File Structure
• Top-level:
• bin/conf: YAML configs for logs, server, system, database, etc.
• cmake: build helper scripts.
• examples, samples, template: demonstration servers and module templates.
• sylar: core framework code, organized into subdirectories (db, ds, email, fiber, http, iomanager, module, orm, rock, streams, util, worker, zk_client, etc.).
• tests: unit tests covering each module.
• Architectural hints:
• Core modules: application lifecycle, module/plugin system (sylar/application*, module*).
• Concurrency: thread abstraction, fiber (user‐level threads), scheduler, I/O manager.
• Networking: Socket, Stream, TCPServer, HTTPServer/Client, WebSocket.
• Protocol libraries: HTTP/1.1, WebSocket, custom rock RPC, name server (ns).
• Utilities: logging, configuration, serialization (ByteArray), data structures, cryptography, JSON, URI parsing.
• Persistence: database drivers for MySQL, SQLite3, Redis; ORM layer.
• External services: Zookeeper client, SMTP email, service discovery, load balancing.
Read README for Insights
• Modules described sequentially: logging, config, threading, coroutine, scheduler, I/O scheduler (epoll), hook, socket, ByteArray, TCPServer, Stream, HTTP, Servlet.
• Emphasis on N:M coroutine scheduling, Reactor pattern via epoll, finite‐state‐machine HTTP parsing (Ragel), pluggable servlets.
Main Components and Their Interactions
a. Core Framework Layer
• Logger & Config (static, global singletons)
• Thread & Synchronization primitives
• Fiber (coroutine) abstraction
• Scheduler (N threads ↔ M fibers)
• Hook layer (shim for blocking syscalls to nonblocking)
• I/O Manager (epoll-based Reactor with timed events)
b. Networking Layer
• Socket abstractions (Address, Socket)
• Stream abstractions (AsyncSocketStream)
• TCPServer (accept loop, connection dispatch)
• HTTPServer / HTTPClient (based on TCPServer + HTTP protocol)
• WebSocket support
• Custom RPC protocols: rock, ns (name server)
c. Data & Persistence Layer
• ByteArray (binary serialization)
• Database drivers (MySQL, SQLite3, Redis)
• ORM (table/column/index abstraction)
• Zookeeper client for service discovery
d. Application Layer
• Module mechanism (plugins derived from IModule)
• Example modules (status servlet, config servlet)
• User‐defined servers (examples/ and template/)
e. External Dependencies
• YAML (for config)
• Ragel (for HTTP parser)
• POSIX threads, epoll
• Third-party libs: MySQL client, hiredis, SQLite3, zookeeper C
Architectural Patterns & Principles
• Reactor (epoll in I/O Manager)
• Proactor via coroutine scheduling + hook shim
• N:M coroutine scheduling (user-level threads over OS threads)
• Singleton & Module plugin pattern for extensibility
• Layered architecture: Core → Networking → Protocols → Applications
• Separation of concerns: logging/config separate from runtime; protocol-specific code separate from transport.