Solution for first highload mail.ru championship.
Task was to create http REST web-service, deploy it using docker and show best possible latency for each request. Detailed info about rules and request types can be found in official website
Solution created with these ideas in mind:
- Use modern C++17 features
- Keep sane and good structurized code
- Use libraries for network and json, so no invent bicycle
Task realisation is pretty straightforward and don't use complex algorithms:
- Get request, determine route according to documentation
- Perform operation with database
- Create response with result
- RequestHandler - respond for routing, extracting get parameters, passing queries to database, response formation
- SimpleDB - database realisation, build using hashmaps. It is optimal solution, because of high performance requirements and few number of requests. So it is possible to create function for each request type. For requests with aggregation used special "index" hash maps.
- PodTypes - just one-to-one mapping of needed primitive types. Used for json convertaion.
- HTTP server built using libwebsite - evhttp replacement for libevent
- For json interaction used nlohmann json library. Pretty high level and handy. In critical places replaced with plain sprintf formatting to achieve best possible performance.
Project used C++17 standard, especially:
- filesystem - for initial database filling from json files
- optional - for passing get parameters to query function
- string_view - as lightweight replacement for string, for selecting method, uri, etc. just in message buffer from socket
mdkir build && cd build
cmake -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_C_COMPILER=clang -DCMAKE_BUILD_TYPE=Release ..
cmake --build .
Tested with clang 4.0.1
Notice that project uses libc++ standard library instead of libstdc++.
Because championship has limits on docker container size and clang cannot fit inside need to manually copy runtime libraries into container.
You can either install clang inside docker or
create folder docker-libs
and populate with necessarily libraries.
mkdir docker-libs
cp /lib/libc++.so.1 docker-libs
cp /lib/libc++abi.so.1 docker-libs
cp /lib/x86_64-linux-gnu/libgcc_s.so.1 docker-libs
Create docker container:
docker build -t hlcup .
All source code belonging to this repo distributed under MIT License.