-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
3,634 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
name: lb-ci | ||
|
||
on: | ||
push: | ||
branches: [ "main", "ci", "dev" ] | ||
pull_request: | ||
branches: [ "main", "ci", "dev" ] | ||
|
||
jobs: | ||
test-build-no-conan: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Do build | ||
run: bash lbbuild.sh build Release | ||
|
||
test-build: | ||
runs-on: | ||
- ubuntu-latest | ||
- macos-latest | ||
- centos-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Get Conan | ||
id: conan | ||
uses: turtlebrowser/get-conan@main | ||
|
||
- name: Conan version | ||
run: echo "${{ steps.conan.outputs.version }}" | ||
|
||
- name: Conan detect profile | ||
run: conan profile detect --force | ||
|
||
- name: Do build | ||
run: bash lbbuild.sh build Release | ||
|
||
|
||
run-unit-tests: | ||
runs-on: ubuntu-latest | ||
needs: test-build | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Run unit tests | ||
run: bash lbbuild.sh test Release | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,7 @@ | |
*.exe | ||
*.out | ||
*.app | ||
|
||
build | ||
CMakeUserPresets.json | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
cmake_minimum_required(VERSION 3.16) | ||
project(lb2) | ||
|
||
message("Building with CMake version: ${CMAKE_VERSION}") | ||
set(CMAKE_CXX_STANDARD 17) | ||
set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
|
||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lb2) | ||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lb2) | ||
|
||
include(cmake/install_dependencies.cmake) | ||
add_compile_definitions(SPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_TRACE) | ||
|
||
# App library | ||
file(GLOB lb2_sources | ||
src/lb/*.cpp | ||
src/lb/tcp/*.cpp | ||
) | ||
add_library(lb2 ${lb2_sources}) | ||
target_include_directories(lb2 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src) | ||
target_link_libraries( | ||
lb2 PUBLIC | ||
Boost::system | ||
Boost::thread | ||
Boost::program_options | ||
Boost::stacktrace | ||
Boost::stacktrace_backtrace | ||
spdlog::spdlog | ||
yaml-cpp::yaml-cpp | ||
jemalloc::jemalloc | ||
ctre::ctre | ||
) | ||
|
||
# Copying configs | ||
add_custom_target( | ||
copy_configs ALL | ||
COMMAND ${CMAKE_COMMAND} -E copy_directory | ||
${PROJECT_SOURCE_DIR}/configs | ||
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/configs | ||
COMMENT "Copying configs...." | ||
) | ||
|
||
# Main app | ||
add_executable(lb_app src/main.cpp) | ||
target_compile_definitions(lb_app PUBLIC LB_APP_LOGGING) | ||
target_link_libraries(lb_app PUBLIC lb2) | ||
add_dependencies(lb_app copy_configs) | ||
|
||
# Setting up unit-tests | ||
enable_testing() | ||
include(GoogleTest) | ||
file(GLOB unit_tests_sources tests/*.cpp) | ||
add_executable(lb_tests ${unit_tests_sources}) | ||
target_link_libraries( | ||
lb_tests PUBLIC | ||
lb2 | ||
GTest::gtest | ||
spdlog::spdlog | ||
yaml-cpp::yaml-cpp | ||
jemalloc::jemalloc | ||
) | ||
|
||
add_dependencies(lb_tests copy_configs) | ||
gtest_discover_tests( | ||
lb_tests | ||
WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY} | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,142 @@ | ||
# lb2 | ||
1. [Introduction](#lb2) | ||
1. [Supported algorithms](#supported-algorithms) | ||
2. [Installation](#installation) | ||
3. [YAML-config example](#config-example) | ||
4. [Run app](#run-app) | ||
|
||
# lb2 | ||
|
||
lb2 is an asynchronous multithreaded load balancer written in C++. It is designed to be highly configurable and supports six different algorithms of load balancing. This project aims to provide a flexible and efficient solution for distributing network traffic across multiple servers. | ||
|
||
## Supported algorithms | ||
* `round-robin` | ||
* `weighted round-robin` | ||
* `ip-hash` | ||
* `consistent hash` | ||
* `least-connections` | ||
* `least-response-time` | ||
|
||
# Installation: | ||
|
||
First of all make sure, that `pip` is installed. It will be used for installing `CMake` and `Conan` if needed. | ||
|
||
> To automatically install all dependencies and build application type: | ||
```bash | ||
./lbbuild.sh build Release | ||
``` | ||
|
||
This script builds app and copies `configs` folder to ``` build/lb2/configs``` | ||
|
||
> To build and run unit tests type: | ||
```bash | ||
./lbbuild.sh test Release | ||
``` | ||
|
||
# Config example: | ||
|
||
```yaml | ||
# Configure logging | ||
logging: | ||
# Optional value. | ||
file: | ||
name: logs/logs.txt # Logfile name | ||
level: debug # Possible values: trace, debug, info, warning, error | ||
console: | ||
level: debug | ||
|
||
thread_pool: | ||
threads_number: auto # or number | ||
|
||
acceptor: | ||
port: 9090 # Port number | ||
ip_version: 4 # or 6 | ||
|
||
# Configure load balancing algorithm | ||
load_balancing: | ||
# Possible values: | ||
# - round_robin | ||
# - weighted_round_robin | ||
# - consistent_hash | ||
# - ip_hash | ||
# - least_connections, | ||
# - least_response_time | ||
|
||
|
||
# Example config of least connections | ||
algorithm: least_connections | ||
endpoints: | ||
- ip: "127.0.0.1" | ||
port: 8081 | ||
- ip: "127.0.0.2" | ||
port: 8082 | ||
- ip: "127.0.0.3" | ||
port: 8083 | ||
|
||
# Example config of least response time | ||
# algorithm: least_response_time | ||
# endpoints: | ||
# - ip: "127.0.0.1" | ||
# port: 8081 | ||
# - ip: "127.0.0.2" | ||
# port: 8082 | ||
# - ip: "127.0.0.3" | ||
# port: 8083 | ||
|
||
# Example config of round robin | ||
# algorithm: round_robibin | ||
# endpoints: | ||
# - ip: "127.0.0.1" | ||
# port: 8081 | ||
# - ip: "127.0.0.2" | ||
# port: 8082 | ||
# - ip: "127.0.0.3" | ||
# port: 8083 | ||
|
||
# Example config of weighted round robin | ||
# algorithm: weighted_round_robin | ||
# endpoints: | ||
# - ip: "127.0.0.1" | ||
# port: 8081 | ||
# weight: 1 | ||
# - ip: "127.0.0.2" | ||
# port: 8082 | ||
# weight: 2 | ||
# - ip: "127.0.0.3" | ||
# port: 8083 | ||
# weight: 3 | ||
|
||
# Example config of consistent hash | ||
# algorithm: consistent_hash | ||
# replicas: 5 | ||
# endpoints: | ||
# - ip: "127.0.0.1" | ||
# port: 8081 | ||
# - ip: "127.0.0.2" | ||
# port: 8082 | ||
# - ip: "127.0.0.3" | ||
# port: 8083 | ||
|
||
# Example config of ip hash | ||
# algorithm: ip_hash | ||
# endpoints: | ||
# - ip: "127.0.0.1" | ||
# port: 8081 | ||
# - ip: "127.0.0.2" | ||
# port: 8082 | ||
# - ip: "127.0.0.3" | ||
# port: 8083 | ||
``` | ||
|
||
# Run app | ||
|
||
* Firstly, build app. To do it type: ```./lbbuild.sh build Release``` | ||
|
||
* Then, go to ```build/lb2/``` | ||
|
||
* Modify configs in ```build/lb2/configs/config.yaml``` or write your own. | ||
|
||
* Run app using: ```./lb_app``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
find_package( | ||
Boost 1.74 REQUIRED | ||
system | ||
thread | ||
program_options | ||
stacktrace | ||
COMPONENTS | ||
) | ||
|
||
find_package(yaml-cpp REQUIRED) | ||
find_package(spdlog REQUIRED) | ||
find_package(jemalloc REQUIRED) | ||
find_package(GTest REQUIRED) | ||
find_package(ctre REQUIRED) | ||
|
||
include_directories(${Boost_INCLUDE_DIRS}) | ||
include_directories(${yaml-cpp_INCLUDE_DIRS}) | ||
|
||
include_directories(third_party/pumba/include) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[requires] | ||
boost/1.74.0 | ||
yaml-cpp/0.8.0 | ||
spdlog/1.13.0 | ||
jemalloc/5.3.0 | ||
ctre/3.8.1 | ||
gtest/1.14.0 | ||
|
||
[generators] | ||
CMakeDeps | ||
CMakeToolchain |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# Configure logging | ||
logging: | ||
# Optional value. | ||
file: | ||
name: logs/logs.txt # Logfile name | ||
level: debug # Possible values: trace, debug, info, warning, error | ||
console: | ||
level: debug | ||
|
||
thread_pool: | ||
threads_number: auto # or number | ||
|
||
acceptor: | ||
port: 9090 # Port number | ||
ip_version: 4 # or 6 | ||
|
||
# Configure load balancing algorithm | ||
load_balancing: | ||
# Possible values: | ||
# - round_robin | ||
# - weighted_round_robin | ||
# - consistent_hash | ||
# - ip_hash | ||
# - least_connections, | ||
# - least_response_time | ||
|
||
|
||
# Example config of least connections | ||
algorithm: least_connections | ||
endpoints: | ||
- ip: "127.0.0.1" | ||
port: 8081 | ||
- ip: "127.0.0.2" | ||
port: 8082 | ||
- ip: "127.0.0.3" | ||
port: 8083 | ||
|
||
# Example config of least response time | ||
# algorithm: least_response_time | ||
# endpoints: | ||
# - ip: "127.0.0.1" | ||
# port: 8081 | ||
# - ip: "127.0.0.2" | ||
# port: 8082 | ||
# - ip: "127.0.0.3" | ||
# port: 8083 | ||
|
||
# Example config of round robin | ||
# algorithm: round_robibin | ||
# endpoints: | ||
# - ip: "127.0.0.1" | ||
# port: 8081 | ||
# - ip: "127.0.0.2" | ||
# port: 8082 | ||
# - ip: "127.0.0.3" | ||
# port: 8083 | ||
|
||
# Example config of weighted round robin | ||
# algorithm: weighted_round_robin | ||
# endpoints: | ||
# - ip: "127.0.0.1" | ||
# port: 8081 | ||
# weight: 1 | ||
# - ip: "127.0.0.2" | ||
# port: 8082 | ||
# weight: 2 | ||
# - ip: "127.0.0.3" | ||
# port: 8083 | ||
# weight: 3 | ||
|
||
# Example config of consistent hash | ||
# algorithm: consistent_hash | ||
# replicas: 5 | ||
# endpoints: | ||
# - ip: "127.0.0.1" | ||
# port: 8081 | ||
# - ip: "127.0.0.2" | ||
# port: 8082 | ||
# - ip: "127.0.0.3" | ||
# port: 8083 | ||
|
||
# Example config of ip hash | ||
# algorithm: ip_hash | ||
# endpoints: | ||
# - ip: "127.0.0.1" | ||
# port: 8081 | ||
# - ip: "127.0.0.2" | ||
# port: 8082 | ||
# - ip: "127.0.0.3" | ||
# port: 8083 |
Oops, something went wrong.