Skip to content

Commit

Permalink
🎊add epoll for Mayhem server
Browse files Browse the repository at this point in the history
🐛ASan error
  • Loading branch information
Lord-Turmoil committed Dec 1, 2024
1 parent a969245 commit 9d15ac4
Show file tree
Hide file tree
Showing 20 changed files with 652 additions and 63 deletions.
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ project(${PROJECT_NAME}
DESCRIPTION "A mini HTTP server library with C++"
LANGUAGES CXX)

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()

# dependencies
add_subdirectory(libs)

Expand All @@ -25,6 +29,10 @@ endif()
# strict warnings
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror")

# optimization flags
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")

# library properties
set(LIBRARY_BASE_PATH "${PROJECT_SOURCE_DIR}")
set(PUBLIC_INCLUDE_DIRECTORIES "${LIBRARY_BASE_PATH}/include")
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,13 @@ The server settings include the `name` of the server, and the `port` to listen o
}
```

Notably, `name` specifies which server to use. Currently, **minet-core** has two server implementation.
Notably, `name` specifies which server to use. Currently, **minet-core** has three server implementation.

- `Basic`: Default option, a blocking server that handles requests synchronously.
- `Mayhem`: An experimental server that handles requests asynchronously.
- `Threaded`: A server that handles requests in multiple threads with a thread pool.
- `Mayhem`: An experimental server that handles requests using both epoll and thread pool.

If you choose to use `Basic` server, then `threads` and `capacity` are ignored. For `Mayhem` server, `threads` is the number of worker threads, and `capacity` is the maximum requests queued on each worker thread.
If you choose to use `Basic` server, then `threads` and `capacity` are ignored. For `Threaded` and `Mayhem` server, `threads` is the number of worker threads, and `capacity` is the maximum requests queued on each worker thread.

### Logging

Expand Down
35 changes: 35 additions & 0 deletions demo/appsettings.mayhem.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"server": {
"name": "Mayhem",
"threads": 4,
"capacity": 1024,
"port": 5001
},
"logging": {
"level": "Debug",
"pattern": "[%Y-%m-%d %H:%M:%S] %^%=8l%$ [%n]: %v",
"sinks": [
{
"file": "stdout"
},
{
"pattern": "[%Y-%m-%d %H:%M:%S] %l [%n]: %v",
"file": "server.log"
}
],
"loggers": {
"Demo": {
"level": "Debug",
"sinks": [
{
"file": "stdout"
},
{
"pattern": "[%Y-%m-%d %H:%M:%S] %l: %v",
"file": "app.log"
}
]
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@
},
"logging": {
"level": "Debug",
"pattern": "",
"pattern": "[%Y-%m-%d %H:%M:%S] %^%=8l%$ [%n]: %v",
"sinks": [
{
"file": "stdout"
},
{
"pattern": "[%Y-%m-%d %H:%M:%S] %8l [%6n]: %v",
"pattern": "[%Y-%m-%d %H:%M:%S] %l [%n]: %v",
"file": "server.log"
}
],
"loggers": {
"Demo": {
"level": "Debug",
"pattern": "[%Y-%m-%d %H:%M:%S] %^%l%$ [%6n]: %v",
"sinks": [
{
"file": "stdout"
},
{
"file": "demo.log"
"pattern": "[%Y-%m-%d %H:%M:%S] %l: %v",
"file": "app.log"
}
]
}
Expand Down
9 changes: 9 additions & 0 deletions include/minet/core/HttpContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ struct HttpContext
*/
int CreateHttpContext(const network::AcceptData& data, Ref<HttpContext>* context);

/**
* @brief Create HTTP context from file descriptor alone.
* @param fd The socket file descriptor.
* @param context Output new HTTP context.
* @return 0 on success, otherwise, see @link ParseHttpRequest.
* @warning In this case, the Host field in request will be "unknown".
*/
int CreateHttpContext(int fd, Ref<HttpContext>* context);

/**
* @brief Destroy a HTTP context after handling.
* @param context The context to destroy.
Expand Down
2 changes: 1 addition & 1 deletion libs/json
Submodule json updated 67 files
+11 −0 .clang-tidy
+1 −3 .github/CONTRIBUTING.md
+3 −3 .github/workflows/check_amalgamation.yml
+1 −1 .github/workflows/cifuzz.yml
+2 −2 .github/workflows/codeql-analysis.yml
+61 −9 .github/workflows/macos.yml
+2 −2 .github/workflows/publish_documentation.yml
+48 −27 .github/workflows/ubuntu.yml
+7 −7 .github/workflows/windows.yml
+0 −1 BUILD.bazel
+6 −1 CMakeLists.txt
+0 −1 Makefile
+56 −42 README.md
+15 −4 cmake/ci.cmake
+1 −1 docs/examples/at__keytype_const.c++17.cpp
+1 −1 docs/mkdocs/docs/api/basic_json/is_structured.md
+1 −1 docs/mkdocs/docs/api/basic_json/operator_gt.md
+1 −1 docs/mkdocs/docs/api/basic_json/operator_le.md
+1 −1 docs/mkdocs/docs/api/basic_json/operator_lt.md
+1 −1 docs/mkdocs/docs/api/basic_json/update.md
+1 −1 docs/mkdocs/docs/api/macros/json_has_static_rtti.md
+118 −0 docs/mkdocs/docs/api/macros/nlohmann_define_derived_type.md
+1 −1 docs/mkdocs/docs/features/arbitrary_types.md
+3 −3 docs/mkdocs/docs/features/json_pointer.md
+22 −0 include/nlohmann/detail/conversions/from_json.hpp
+7 −7 include/nlohmann/detail/conversions/to_chars.hpp
+21 −1 include/nlohmann/detail/conversions/to_json.hpp
+16 −0 include/nlohmann/detail/exceptions.hpp
+9 −0 include/nlohmann/detail/input/input_adapters.hpp
+4 −4 include/nlohmann/detail/input/json_sax.hpp
+1 −1 include/nlohmann/detail/input/lexer.hpp
+2 −2 include/nlohmann/detail/input/parser.hpp
+2 −2 include/nlohmann/detail/iterators/iteration_proxy.hpp
+30 −0 include/nlohmann/detail/macro_scope.hpp
+1 −1 include/nlohmann/detail/meta/cpp_future.hpp
+1 −1 include/nlohmann/detail/meta/std_fs.hpp
+3 −3 include/nlohmann/detail/meta/type_traits.hpp
+1 −1 include/nlohmann/detail/output/binary_writer.hpp
+3 −3 include/nlohmann/detail/output/serializer.hpp
+20 −20 include/nlohmann/json.hpp
+129 −129 nlohmann_json.natvis
+144 −46 single_include/nlohmann/json.hpp
+1 −1 tests/src/unit-32bit.cpp
+3 −3 tests/src/unit-alt-string.cpp
+5 −5 tests/src/unit-bjdata.cpp
+1 −1 tests/src/unit-bson.cpp
+5 −6 tests/src/unit-cbor.cpp
+1 −1 tests/src/unit-class_lexer.cpp
+2 −2 tests/src/unit-class_parser.cpp
+0 −1 tests/src/unit-concepts.cpp
+2 −2 tests/src/unit-convenience.cpp
+92 −2 tests/src/unit-conversions.cpp
+18 −14 tests/src/unit-deserialization.cpp
+14 −14 tests/src/unit-element_access1.cpp
+10 −10 tests/src/unit-element_access2.cpp
+1 −1 tests/src/unit-json_patch.cpp
+3 −3 tests/src/unit-msgpack.cpp
+5 −5 tests/src/unit-readme.cpp
+72 −13 tests/src/unit-regression2.cpp
+1 −1 tests/src/unit-serialization.cpp
+2 −2 tests/src/unit-testsuites.cpp
+2 −2 tests/src/unit-ubjson.cpp
+29 −16 tests/src/unit-udt.cpp
+194 −9 tests/src/unit-udt_macro.cpp
+1 −1 tests/src/unit-unicode1.cpp
+3 −3 tools/gdb_pretty_printer/nlohmann-json.py
+13 −13 tools/generate_natvis/nlohmann_json.natvis.j2
2 changes: 1 addition & 1 deletion libs/spdlog
13 changes: 9 additions & 4 deletions scripts/demo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ function run_server() {

ARGS="demo/appsettings.jsonc"
if [ "$1" == "mayhem" ]; then
ARGS="demo/appsettings.mayhem.jsonc"
echo -e "\033[0;36mRun with Mayhem server...\033[0m"
ARGS="demo/appsettings.mayhem.json"
elif [ "$1" == "threaded" ]; then
echo -e "\033[0;36mRun with Threaded server...\033[0m"
ARGS="demo/appsettings.threaded.json"
fi

echo -e "\033[0;36m$BIN $ARGS\033[0m"
Expand Down Expand Up @@ -85,7 +89,8 @@ elif [ "$option" == "client" ]; then
run_client $@
else
echo "Usage: $0 [server|client] [args]"
echo " server - run with Basic server"
echo " server mayhem - run with Mayhem server"
echo " client N - 4 processes, each sends N requests"
echo " server - run with Basic server"
echo " server threaded - run with Threaded server"
echo " server mayhem - run with Mayhem server"
echo " client N - 4 processes, each sends N requests"
fi
30 changes: 12 additions & 18 deletions src/components/BasicServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,6 @@ void BasicServer::Stop()
_isRunning = false;
}

void BasicServer::_OnNewConnection(const network::AcceptData& data)
{
Ref<HttpContext> context;
int r = CreateHttpContext(data, &context);
if (r == 0)
{
_DecorateContext(context);
_logger->Debug("New connection from {}", context->Request.Host);
_onConnectionCallback(context);
}
else
{
_logger->Error("Failed to create HTTP context, error code: {}", r);
}
}

void BasicServer::_Serve()
{
network::AcceptData data;
Expand All @@ -83,7 +67,17 @@ void BasicServer::_Serve()
{
while (AcceptSocket(_listenFd, &data) && _isRunning)
{
_OnNewConnection(data);
int r = CreateHttpContext(data, &context);
if (r == 0)
{
_DecorateContext(context);
_logger->Debug("New connection from {}", context->Request.Host);
_onConnectionCallback(context);
}
else
{
_logger->Error("Failed to create HTTP context, error code: {}", r);
}
}
}

Expand All @@ -93,7 +87,7 @@ void BasicServer::_Serve()
_logger->Info("{} server shut down", Name());
}

void BasicServer::_DecorateContext(const Ref<HttpContext>& context)
void BasicServer::_DecorateContext(const Ref<HttpContext>& context) const
{
context->Response.Headers["Server"] = Name();
}
Expand Down
15 changes: 4 additions & 11 deletions src/components/BasicServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ MINET_BEGIN
/**
* @brief The default server implementation.
*/
class BasicServer : public IServer
class BasicServer final : public IServer
{
public:
explicit BasicServer(const Ref<ServerConfig>& config);
Expand All @@ -29,29 +29,22 @@ class BasicServer : public IServer

void Stop() override;

void SetOnConnection(const OnConnectionCallback& callback) override
{
_onConnectionCallback = callback;
}

const char* Name() const override
{
return Identifier();
}

protected:
virtual void _OnNewConnection(const network::AcceptData& data);
void _DecorateContext(const Ref<HttpContext>& context);

private:
void _Serve();

void _DecorateContext(const Ref<HttpContext>& context) const;

void _OpenSocket();
void _CloseSocket();

protected:
Ref<ServerConfig> _config;
OnConnectionCallback _onConnectionCallback;

int _listenFd;
bool _isRunning;
};
Expand Down
Loading

0 comments on commit 9d15ac4

Please sign in to comment.