English | 简体中文
Fiber Gateway is a performance-first C++23 framework for gateways, reverse
proxies, and asynchronous network services. The repository is centered on the
reusable fiber_lib static library; examples, tests, and the modules under
apps/ build on the same runtime and protocol stack.
This is a framework repository rather than a single executable. A normal build
produces the core library, runnable examples, tests, the lite_nginx
application, and optional application-layer libraries such as
fiber::nacos, fiber::cat, fiber::prometheus, and
fiber::http_compression.
- Linux
epollevent loop with timers, cross-thread notification, and multi-loop scheduling. - C++23 coroutine tasks and asynchronous primitives, including mutexes, read/write locks, signals, timeouts, wait groups, and versioned watches.
- TCP, UDP, Unix domain sockets, DNS resolution and caches, and a Happy Eyeballs TCP connector for already-resolved IPv4/IPv6 addresses.
- In-tree TLS 1.2/1.3 client and server engines, integrated with TCP streams and QUIC, using BoringSSL crypto primitives.
- HTTP/1.1, HTTP/2, and HTTP/3 server stacks.
- HTTP/1.1 and HTTP/2 connection pools, plus HTTP/3 client connections.
- In-tree QUIC v1 and v2 (RFC 9369) transport with TLS, streams, loss recovery, congestion control, pacing, connection IDs, address validation, and UDP GSO support where available, plus version negotiation and compatible version negotiation (RFC 9368).
- HPACK, QPACK, streaming request and response bodies, and WebSocket proxying through HTTP/1 Upgrade or HTTP/2/3 Extended CONNECT.
- A Nacos client library with its own private gRPC/protobuf transport.
- A purpose-built JS-like bytecode engine with compile-time host bindings, request-scoped GC, and native coroutine-aware HTTP APIs.
- In-tree streaming gzip encoding and an optional HTTP gzip response writer.
- An in-tree linear-time regex engine ported from RE2 without external dependencies: RE2 syntax, many-pattern set matching, Aho-Corasick literal sets, streaming scans, and bounded work for untrusted patterns and inputs.
- Common JSON codecs, structured logging, and allocation-conscious buffer and memory utilities.
Gateway behavior often needs to change faster than the native data plane. A static configuration language is sufficient for wiring listeners and upstreams, but becomes awkward for conditional routing, request inspection, header and payload transformation, canary decisions, and multi-step upstream calls. Implementing every policy in C++ keeps the hot path fast, but turns each policy adjustment into a rebuild and deployment. Fiber's scripting engine is the deliberately narrow layer between those two extremes: configuration can select and compile a policy, while the request still runs inside the same C++ gateway process.
The runtime is an in-tree, JS-like bytecode interpreter rather than an
ECMAScript implementation. It provides integers and floating-point numbers,
strings, binary values, arrays, objects, templates, control flow, exceptions,
and a gateway-oriented standard library. HTTP bindings expose request data,
response construction, route and connection constants, and directive-bound
upstream request() and streaming proxyPass() operations:
directive backend = http "@api";
if ($header.x_canary == "1") {
return backend.proxyPass({
headers: {"X-Route": "canary"}
});
}
resp.sendJson(200, {status: "ready", path: $req.path});Scripts are compiled when configuration is loaded, so unknown functions,
invalid argument counts, unavailable route constants, and invalid directives
fail before serving traffic. The resulting read-only bytecode can be reused
across requests; execution state and garbage-collected values belong to a
request-local GcHeap. Host functions are registered explicitly through a
Library, which keeps the script-visible capability surface small and makes
the C++/script ABI explicit.
A dedicated engine is important here because embedding a general-purpose
JavaScript runtime would bring another garbage collector and object model, a
larger dependency and ABI surface, and language features unrelated to gateway
policy. It would also require either a second event-loop/asynchronous model or
a substantial bridge to Fiber's scheduler. That integration would work against
Fiber's coroutine ownership and allocation model. Instead, asynchronous host
functions compile to dedicated opcodes: the VM suspends directly on
fiber::async::Task and resumes without requiring Promise or await syntax.
Execution results distinguish returned values, catchable script exceptions,
and host/runtime aborts so the gateway can map failures deliberately.
This engine is therefore not intended to compete with a browser or Node.js runtime. Its value is that its language surface, bytecode, memory ownership, and HTTP capabilities can evolve with this gateway's performance and lifecycle requirements. The explicit capability model reduces exposed machinery, but it is not by itself a security sandbox; hosts must still apply request-size, timeout, trust, and resource policies. See the script module guide for the complete language, standard library, HTTP API, and C++ embedding contract.
.
├── include/fiber # Public fiber_lib headers
├── src/ # Core fiber_lib implementations
├── example/ # Small runnable examples and benchmark helpers
├── apps/ # Applications and optional app-layer libraries
├── tests/ # Core GoogleTest suite
├── fuzz/ # TLS, QUIC, and regex libFuzzer harnesses and corpora
├── docs/ # Stable module documentation
├── feature/ # Design notes, audits, and implementation reports
├── cmake/ # Toolchain, dependency, and target helpers
└── scripts/ # Build, interoperability, and benchmark tooling
The main core modules have public headers under include/fiber/ and
implementations plus private headers under the corresponding src/ directory.
The include/ root is propagated to consumers for <fiber/...> includes;
src/ is available privately to the core library and its white-box tests:
event/— event loops, pollers, timers, and loop groups.async/— coroutine tasks, scheduling, and synchronization primitives.net/— socket, listener, stream, TLS, and address abstractions.tls/— TLS handshakes, records, certificates, and session resumption.compression/— streaming gzip encoder.quic/— QUIC transport, crypto, recovery, congestion, and streams.http/— HTTP/1.1, HTTP/2, HTTP/3, clients, servers, and pools.dns/— DNS messages, clients, resolvers, and caches.regex/— linear-time regular expression engine ported from RE2.common/— errors, JSON, memory, containers, and shared utilities.script/andhttp_script/— scripting runtime and HTTP bindings.log/— logger hierarchy, formatting, and appenders.
- Linux. The current event backend is based on
epoll. - CMake 4.1 or newer.
- GCC 13+ or Clang 17+ with the C++23 standard library features required by
the project, including
std::expected. - Network access during the first configure unless the dependency sources are
already cached under
temp/_depsor a differentFIBER_DEPS_DIR.
CMake prefers a suitable Clang toolchain, then GCC, when no compiler or
toolchain is selected explicitly. The core library links BoringSSL
crypto; TLS protocol handling lives in Fiber. BoringSSL ssl is used by
test peers. protobuf-lite and protoc are prepared only when the Nacos
component is enabled. GoogleTest is used when tests are enabled; jemalloc is
optional.
Configure and build all default targets:
cmake -S . -B build
cmake --build buildRun the discovered GoogleTest cases through CTest:
ctest --test-dir build --output-on-failureUseful default outputs include:
build/fiber_tests
build/example/http1_echo
build/example/https_echo
build/example/dns_dig
build/example/http3_benchmark_server
build/example/http3_benchmark_client
build/apps/lite_nginx
For example:
./build/example/http1_echo 8080
./build/example/dns_dig example.com
./build/apps/lite_nginx --check-config
./build/apps/lite_nginxSee the examples guide for every example and the HTTP/3 benchmark invocation.
| Option | Default | Purpose |
|---|---|---|
FIBER_BUILD_EXAMPLES |
ON |
Build programs under example/. |
FIBER_BUILD_TESTS |
ON |
Build core and application tests when GoogleTest is available. |
FIBER_BUILD_APPS |
ON |
Build runnable applications discovered under apps/. |
FIBER_BUILD_NACOS |
initial value of FIBER_BUILD_APPS |
Build the reusable fiber::nacos component. |
FIBER_BUILD_CAT |
initial value of FIBER_BUILD_APPS |
Build the reusable fiber::cat component. |
FIBER_BUILD_PROMETHEUS |
initial value of FIBER_BUILD_APPS |
Build the reusable fiber::prometheus component. |
FIBER_BUILD_HTTP_COMPRESSION |
initial value of FIBER_BUILD_APPS |
Build fiber::http_compression; required by lite_nginx. |
FIBER_BUILD_FUZZERS |
OFF |
Build TLS, QUIC, and regex libFuzzer harnesses with Clang, ASan and UBSan; disables LTO. |
FIBER_BUILD_CAT_DEMO |
OFF |
Build the CAT demo program. |
FIBER_BUILD_PROMETHEUS_BENCHMARK |
OFF |
Build the Prometheus record-path benchmark. |
FIBER_FETCH_DEPS |
ON |
Allow fetching missing optional dependencies such as GoogleTest and jemalloc. |
FIBER_USE_JEMALLOC |
OFF |
Link final executables against jemalloc. |
FIBER_ENABLE_LTO |
ON |
Enable IPO/LTO when supported. |
FIBER_ALLOW_GCC_LTO |
OFF |
Opt in to GCC LTO, which is disabled by default for stability. |
FIBER_ENABLE_UDP_GSO |
ON |
Compile Linux UDP GSO support when system headers provide it. |
FIBER_FORCE_TIMERFD_POLLER |
OFF |
Force the timerfd-based poll timeout path instead of epoll_pwait2. |
FIBER_ENABLE_BENCHMARK_TRACE |
OFF |
Enable benchmark-only hot-path trace counters. |
FIBER_USE_LIBCXX |
OFF |
Use libc++ with Clang. |
FIBER_STATIC_LIBCXX |
ON |
Statically link libc++ runtimes when FIBER_USE_LIBCXX=ON. |
FIBER_FETCH_DEPS=OFF disables fallback downloads for GoogleTest and
jemalloc. It does not disable BoringSSL population or protobuf population
when Nacos is enabled. Use FIBER_DEPS_DIR to select a pre-populated source
cache. gzip uses the in-tree implementation in src/compression/; core tests
compile the checked-in zlib 1.3.2 reference for differential checks, without
a system zlib dependency.
Component defaults are initialized when a build directory is first configured;
changing FIBER_BUILD_APPS later does not reset cached component options.
Keep FIBER_BUILD_HTTP_COMPRESSION=ON when building lite_nginx.
All downloaded source archives are SHA-256 verified. Restricted-network and downstream builds can replace an archive URL and its expected digest without patching Fiber by setting the corresponding cache variables:
| Dependency | URL variable | SHA-256 variable |
|---|---|---|
| BoringSSL | FIBER_BORINGSSL_URL |
FIBER_BORINGSSL_SHA256 |
| protobuf | FIBER_PROTOBUF_URL |
FIBER_PROTOBUF_SHA256 |
| GoogleTest | FIBER_GOOGLETEST_URL |
FIBER_GOOGLETEST_SHA256 |
| jemalloc | FIBER_JEMALLOC_URL |
FIBER_JEMALLOC_SHA256 |
For example, a FetchContent consumer can select an approved mirror while retaining verification:
set(FIBER_PROTOBUF_URL "https://mirror.example/protobuf-v21.12.tar.gz"
CACHE STRING "" FORCE)
set(FIBER_PROTOBUF_SHA256 "<mirror-archive-sha256>"
CACHE STRING "" FORCE)
FetchContent_MakeAvailable(fiber_gateway_cpp)A typical release build with jemalloc is:
cmake -S . -B build-release \
-DCMAKE_BUILD_TYPE=Release \
-DFIBER_USE_JEMALLOC=ON
cmake --build build-release
ctest --test-dir build-release --output-on-failureThe single-file programs under example/ cover HTTP/HTTPS servers, TCP and
UDP echo services, DNS lookup, a Git smart-HTTP server, HTTP/2 pool and
HTTP/3 benchmarks, a regex benchmark over the OWASP Core Rule Set, and TLS
probes. nacos_demo is also built when the Nacos component is enabled. They
are intended as compact API references and smoke tests, not as production
application layouts.
Read example/README.md for details.
The top-level build exposes optional reusable components layered on
fiber_lib. Nacos, CAT, and Prometheus sources live under apps/, but that
path is an internal repository detail: in-tree and FetchContent consumers select them
with top-level options and link their stable aliases.
include(FetchContent)
FetchContent_Declare(
fiber_gateway_cpp
GIT_REPOSITORY https://github.com/fiber-net-gateway/fiber-gateway-cpp.git
GIT_TAG <pinned-revision>)
set(FIBER_BUILD_APPS OFF CACHE BOOL "" FORCE)
set(FIBER_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
set(FIBER_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(FIBER_BUILD_NACOS ON CACHE BOOL "" FORCE)
set(FIBER_BUILD_CAT ON CACHE BOOL "" FORCE)
set(FIBER_BUILD_PROMETHEUS ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(fiber_gateway_cpp)
target_link_libraries(my_gateway PRIVATE
fiber::nacos
fiber::cat
fiber::prometheus)The CAT demo and Prometheus benchmark require their dedicated opt-in options.
nacos_demo follows FIBER_BUILD_EXAMPLES when Nacos is enabled; component
tests follow FIBER_BUILD_TESTS.
Link fiber_lib directly for the core runtime and protocols. To use
<fiber/http/GzipResponseWriter.h>, enable FIBER_BUILD_HTTP_COMPRESSION
and link fiber::http_compression; it can be built with applications disabled.
The reusable components and complete applications are:
- AI Gateway — the
repository-owned LLM gateway and the authoritative home of
ai-server. The legacy application code has been removed from this repository; see the migration pointer for provenance. apps/lite_nginx— a lightweight reverse proxy with nginx-style configuration. It accepts HTTP/1.1, HTTP/2, and HTTP/3, supports TLS and WebSocket tunnelling, and proxies to HTTP/1.1 upstreams with pooled connections.apps/nacos— thefiber::nacosclient library with authentication, Nacos 2.x gRPC transport, ConfigService, NamingService, subscriptions, reconnection, and service-state replay.apps/cat— thefiber::catnative CAT 3.0 client library.apps/prometheus— thefiber::prometheusfixed-schema metrics library with EventLoop-owned shards and Prometheus text exposition.
See apps/README.md for module layout and CMake conventions.
- Examples
- Applications
- Script module guide (简体中文)
- HTTP/1 connection pool
- HTTP/2 connection pool
- Happy Eyeballs TCP connector
- TLS, QUIC, and regex fuzzing
- OpenSSL interoperability matrix
- TLS client certificate identity
- Script function signature ABI
- Regex engine design
- HTTP/3 client design
- QUIC client design
- QUIC v2 and version negotiation
- Nacos ConfigService design
- Nacos NamingService design
- Prometheus metrics design
The feature/ directory also contains evolving design notes, audit records,
and benchmark reports. Treat those documents as engineering history unless a
file explicitly describes a current contract.
Fiber Gateway is under active development. The repository already contains a broad protocol and runtime test suite, but APIs and application contracts may continue to evolve. The examples and module-specific READMEs are the best starting points for current usage.
Licensed under the MIT License.