├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── README.md ├── boost_asio ├── CMakeLists.txt ├── asio_io_service_pool.hpp ├── asio_io_service_pool_test.cpp ├── asio_thread_pool.hpp ├── asio_thread_pool_echo_server.cpp ├── asio_thread_pool_strand_test.cpp └── asio_thread_pool_test.cpp ├── boost_asio_coroutine ├── CMakeLists.txt ├── asio_coroutine_echo_server.cpp └── asio_thread_pool.hpp ├── boost_coroutine ├── CMakeLists.txt ├── echo_server.cpp ├── generator.cpp └── input.txt ├── grpc_dynamic_thread_pool ├── CMakeLists.txt ├── dynamic_thread_pool.cpp ├── dynamic_thread_pool.hpp └── main.cpp ├── jsonschema ├── CMakeLists.txt ├── error.hpp ├── jsonreader.hpp └── jsonschema.cpp └── prometheus ├── deployment.yaml ├── docker ├── Dockerfile ├── app.py └── requirements.txt └── service.yaml /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | # Emacs files 35 | *~ 36 | 37 | build -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required (VERSION 2.8) 2 | 3 | project(code-for-blog) 4 | 5 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -std=c++11 -Wall") 6 | 7 | find_package(Boost COMPONENTS system REQUIRED) 8 | find_package(Threads REQUIRED) 9 | 10 | set(SUBDIRS 11 | grpc_dynamic_thread_pool 12 | boost_coroutine 13 | boost_asio_coroutine 14 | boost_asio 15 | jsonschema 16 | gflags 17 | glog 18 | googletest 19 | ) 20 | 21 | foreach(dir ${SUBDIRS}) 22 | add_subdirectory(${dir}) 23 | endforeach() 24 | 25 | enable_testing() 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 詹春畅 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # code-for-blog 2 | Code samples from my blog 3 | -------------------------------------------------------------------------------- /boost_asio/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_executable(asio_thread_pool_test asio_thread_pool_test.cpp) 2 | target_link_libraries(asio_thread_pool_test ${Boost_SYSTEM_LIBRARY} ${CMAKE_THREAD_LIBS_INIT}) 3 | 4 | add_executable(asio_thread_pool_strand_test asio_thread_pool_strand_test.cpp) 5 | target_link_libraries(asio_thread_pool_strand_test ${Boost_SYSTEM_LIBRARY} ${CMAKE_THREAD_LIBS_INIT}) 6 | 7 | add_executable(asio_thread_pool_echo_server asio_thread_pool_echo_server.cpp) 8 | target_link_libraries(asio_thread_pool_echo_server ${Boost_SYSTEM_LIBRARY} ${CMAKE_THREAD_LIBS_INIT}) 9 | 10 | add_executable(asio_io_service_pool_test asio_io_service_pool_test.cpp) 11 | target_link_libraries(asio_io_service_pool_test ${Boost_SYSTEM_LIBRARY} ${CMAKE_THREAD_LIBS_INIT}) 12 | -------------------------------------------------------------------------------- /boost_asio/asio_io_service_pool.hpp: -------------------------------------------------------------------------------- 1 | #ifndef ASIO_IO_SERVICE_POOL_H 2 | #define ASIO_IO_SERVICE_POOL_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | class AsioIOServicePool 10 | { 11 | public: 12 | using IOService = boost::asio::io_service; 13 | using Work = boost::asio::io_service::work; 14 | using WorkPtr = std::unique_ptr; 15 | 16 | AsioIOServicePool(std::size_t size = std::thread::hardware_concurrency()) 17 | : ioServices_(size), 18 | works_(size), 19 | nextIOService_(0) 20 | { 21 | for (std::size_t i = 0; i < size; ++i) 22 | { 23 | works_[i] = std::unique_ptr(new Work(ioServices_[i])); 24 | } 25 | 26 | for (std::size_t i = 0; i < ioServices_.size(); ++i) 27 | { 28 | threads_.emplace_back([this, i] () 29 | { 30 | ioServices_[i].run(); 31 | }); 32 | } 33 | } 34 | 35 | AsioIOServicePool(const AsioIOServicePool &) = delete; 36 | AsioIOServicePool &operator=(const AsioIOServicePool &) = delete; 37 | 38 | boost::asio::io_service &getIOService() 39 | { 40 | auto &service = ioServices_[nextIOService_++]; 41 | if (nextIOService_ == ioServices_.size()) 42 | { 43 | nextIOService_ = 0; 44 | } 45 | 46 | return service; 47 | } 48 | 49 | void stop() 50 | { 51 | for (auto &work: works_) 52 | { 53 | work.reset(); 54 | } 55 | 56 | for (auto &t: threads_) 57 | { 58 | t.join(); 59 | } 60 | } 61 | 62 | private: 63 | std::vector ioServices_; 64 | std::vector works_; 65 | std::vector threads_; 66 | std::size_t nextIOService_; 67 | }; 68 | 69 | #endif /* ASIO_IO_SERVICE_POOL_H */ 70 | -------------------------------------------------------------------------------- /boost_asio/asio_io_service_pool_test.cpp: -------------------------------------------------------------------------------- 1 | #include "asio_io_service_pool.hpp" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | AsioIOServicePool pool(4); 12 | 13 | boost::asio::steady_timer timer1{pool.getIOService(), std::chrono::seconds{2}}; 14 | boost::asio::steady_timer timer2{pool.getIOService(), std::chrono::seconds{2}}; 15 | 16 | int value = 0; 17 | std::mutex mtx; // 互斥锁,用来保护 std::cout 和 value 18 | 19 | timer1.async_wait([&mtx, &value] (const boost::system::error_code &ec) 20 | { 21 | std::lock_guard lock(mtx); 22 | std::cout << "Hello, World! " << value++ << std::endl; 23 | }); 24 | 25 | timer2.async_wait([&mtx, &value] (const boost::system::error_code &ec) 26 | { 27 | std::lock_guard lock(mtx); 28 | std::cout << "Hello, World! " << value++ << std::endl; 29 | }); 30 | pool.stop(); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /boost_asio/asio_thread_pool.hpp: -------------------------------------------------------------------------------- 1 | #ifndef THREAD_POOL_H 2 | #define THREAD_POOL_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | class AsioThreadPool 10 | { 11 | public: 12 | AsioThreadPool(int threadNum = std::thread::hardware_concurrency()) 13 | : work_(new boost::asio::io_service::work(service_)) 14 | { 15 | for (int i = 0; i < threadNum; ++i) 16 | { 17 | threads_.emplace_back([this] () { service_.run(); }); 18 | } 19 | } 20 | 21 | AsioThreadPool(const AsioThreadPool &) = delete; 22 | AsioThreadPool &operator=(const AsioThreadPool &) = delete; 23 | 24 | boost::asio::io_service &getIOService() 25 | { 26 | return service_; 27 | } 28 | 29 | void stop() 30 | { 31 | work_.reset(); 32 | for (auto &t: threads_) 33 | { 34 | t.join(); 35 | } 36 | } 37 | private: 38 | boost::asio::io_service service_; 39 | std::unique_ptr work_; 40 | std::vector threads_; 41 | }; 42 | 43 | #endif /* THREAD_POOL_H */ 44 | -------------------------------------------------------------------------------- /boost_asio/asio_thread_pool_echo_server.cpp: -------------------------------------------------------------------------------- 1 | #include "asio_thread_pool.hpp" 2 | 3 | #include 4 | #include 5 | 6 | using boost::asio::ip::tcp; 7 | 8 | class TCPConnection : public std::enable_shared_from_this 9 | { 10 | public: 11 | TCPConnection(boost::asio::io_service &io_service) 12 | : socket_(io_service), 13 | strand_(io_service) 14 | { 15 | } 16 | 17 | tcp::socket &socket() 18 | { 19 | return socket_; 20 | } 21 | 22 | void start() 23 | { 24 | doRead(); 25 | } 26 | 27 | private: 28 | void doRead() 29 | { 30 | auto self = shared_from_this(); 31 | socket_.async_read_some(boost::asio::buffer(buffer_, buffer_.size()), 32 | strand_.wrap([this, self](boost::system::error_code ec, 33 | std::size_t bytes_transferred) 34 | { 35 | if (!ec) 36 | { 37 | doWrite(bytes_transferred); 38 | } 39 | })); 40 | } 41 | 42 | void doWrite(std::size_t length) 43 | { 44 | auto self = shared_from_this(); 45 | boost::asio::async_write(socket_, boost::asio::buffer(buffer_, length), 46 | strand_.wrap([this, self](boost::system::error_code ec, 47 | std::size_t /* bytes_transferred */) 48 | { 49 | if (!ec) 50 | { 51 | doRead(); 52 | } 53 | })); 54 | } 55 | 56 | private: 57 | tcp::socket socket_; 58 | boost::asio::io_service::strand strand_; 59 | std::array buffer_; 60 | }; 61 | 62 | class EchoServer 63 | { 64 | public: 65 | EchoServer(boost::asio::io_service &io_service, unsigned short port) 66 | : io_service_(io_service), 67 | acceptor_(io_service, tcp::endpoint(tcp::v4(), port)) 68 | { 69 | doAccept(); 70 | } 71 | 72 | void doAccept() 73 | { 74 | auto conn = std::make_shared(io_service_); 75 | acceptor_.async_accept(conn->socket(), 76 | [this, conn](boost::system::error_code ec) 77 | { 78 | if (!ec) 79 | { 80 | conn->start(); 81 | } 82 | this->doAccept(); 83 | }); 84 | } 85 | 86 | private: 87 | boost::asio::io_service &io_service_; 88 | tcp::acceptor acceptor_; 89 | }; 90 | 91 | int main(int argc, char *argv[]) 92 | { 93 | AsioThreadPool pool(4); 94 | 95 | unsigned short port = 5800; 96 | EchoServer server(pool.getIOService(), port); 97 | 98 | pool.stop(); 99 | 100 | return 0; 101 | } 102 | -------------------------------------------------------------------------------- /boost_asio/asio_thread_pool_strand_test.cpp: -------------------------------------------------------------------------------- 1 | #include "asio_thread_pool.hpp" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int main() 9 | { 10 | AsioThreadPool pool(4); 11 | 12 | boost::asio::steady_timer timer1{pool.getIOService(), std::chrono::seconds{1}}; 13 | boost::asio::steady_timer timer2{pool.getIOService(), std::chrono::seconds{1}}; 14 | 15 | int value = 0; 16 | boost::asio::io_service::strand strand{pool.getIOService()}; 17 | 18 | timer1.async_wait(strand.wrap([&value] (const boost::system::error_code &ec) 19 | { 20 | std::cout << "Hello, World! " << value++ << std::endl; 21 | })); 22 | 23 | timer2.async_wait(strand.wrap([&value] (const boost::system::error_code &ec) 24 | { 25 | std::cout << "Hello, World! " << value++ << std::endl; 26 | })); 27 | 28 | pool.stop(); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /boost_asio/asio_thread_pool_test.cpp: -------------------------------------------------------------------------------- 1 | #include "asio_thread_pool.hpp" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | AsioThreadPool pool(4); // 开启 4 个线程 12 | 13 | boost::asio::steady_timer timer1{pool.getIOService(), std::chrono::seconds{2}}; 14 | boost::asio::steady_timer timer2{pool.getIOService(), std::chrono::seconds{2}}; 15 | 16 | int value = 0; 17 | std::mutex mtx; // 互斥锁,用来保护 std::cout 和 value 18 | 19 | timer1.async_wait([&mtx, &value] (const boost::system::error_code &ec) 20 | { 21 | std::lock_guard lock(mtx); 22 | std::cout << "Hello, World! " << value++ << std::endl; 23 | }); 24 | 25 | timer2.async_wait([&mtx, &value] (const boost::system::error_code &ec) 26 | { 27 | std::lock_guard lock(mtx); 28 | std::cout << "Hello, World! " << value++ << std::endl; 29 | }); 30 | pool.stop(); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /boost_asio_coroutine/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_executable(asio_coroutine_echo_server asio_coroutine_echo_server.cpp) 2 | target_link_libraries(asio_coroutine_echo_server ${Boost_SYSTEM_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} boost_coroutine) 3 | -------------------------------------------------------------------------------- /boost_asio_coroutine/asio_coroutine_echo_server.cpp: -------------------------------------------------------------------------------- 1 | #include "asio_thread_pool.hpp" 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | using boost::asio::ip::tcp; 8 | 9 | class TCPConnection : public std::enable_shared_from_this 10 | { 11 | public: 12 | TCPConnection(boost::asio::io_service &io_service) 13 | : socket_(io_service), 14 | strand_(io_service) 15 | { 16 | } 17 | 18 | tcp::socket &socket() 19 | { 20 | return socket_; 21 | } 22 | 23 | void start() 24 | { 25 | auto self = shared_from_this(); 26 | boost::asio::spawn(strand_, 27 | [this, self](boost::asio::yield_context yield) 28 | { 29 | std::array data; 30 | while (true) 31 | { 32 | boost::system::error_code ec; 33 | std::size_t n = socket_.async_read_some(boost::asio::buffer(data, data.size()), 34 | yield[ec]); 35 | if (ec) 36 | { 37 | break; 38 | } 39 | boost::asio::async_write(socket_, boost::asio::buffer(data, n), yield[ec]); 40 | if (ec) 41 | { 42 | break; 43 | } 44 | } 45 | }); 46 | } 47 | 48 | private: 49 | tcp::socket socket_; 50 | boost::asio::io_service::strand strand_; 51 | }; 52 | 53 | class EchoServer 54 | { 55 | public: 56 | EchoServer(boost::asio::io_service &io_service, unsigned short port) 57 | : io_service_(io_service), 58 | acceptor_(io_service, tcp::endpoint(tcp::v4(), port)) 59 | { 60 | boost::asio::spawn(io_service_, 61 | [this] (boost::asio::yield_context yield) 62 | { 63 | while (true) 64 | { 65 | auto conn = std::make_shared(io_service_); 66 | 67 | boost::system::error_code ec; 68 | acceptor_.async_accept(conn->socket(), yield[ec]); 69 | 70 | if (!ec) 71 | { 72 | conn->start(); 73 | } 74 | } 75 | }); 76 | } 77 | 78 | private: 79 | boost::asio::io_service &io_service_; 80 | tcp::acceptor acceptor_; 81 | }; 82 | 83 | int main(int argc, char *argv[]) 84 | { 85 | AsioThreadPool pool; 86 | unsigned short port = 5800; 87 | 88 | EchoServer server(pool.getIOService(), port); 89 | pool.run(); 90 | 91 | return 0; 92 | } 93 | -------------------------------------------------------------------------------- /boost_asio_coroutine/asio_thread_pool.hpp: -------------------------------------------------------------------------------- 1 | #ifndef THREAD_POOL_H 2 | #define THREAD_POOL_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | class AsioThreadPool 10 | { 11 | public: 12 | AsioThreadPool(int threadNum = std::thread::hardware_concurrency()) 13 | : threadNum_(threadNum) 14 | { } 15 | 16 | AsioThreadPool(const AsioThreadPool &) = delete; 17 | AsioThreadPool &operator=(const AsioThreadPool &) = delete; 18 | 19 | boost::asio::io_service &getIOService() 20 | { 21 | return service_; 22 | } 23 | 24 | void run() 25 | { 26 | for (int i = 0; i < threadNum_; ++i) 27 | { 28 | threads_.emplace_back([this] () 29 | { 30 | service_.run(); 31 | }); 32 | } 33 | 34 | for (auto &t : threads_) 35 | { 36 | t.join(); 37 | } 38 | } 39 | 40 | private: 41 | std::size_t threadNum_; 42 | boost::asio::io_service service_; 43 | std::vector threads_; 44 | }; 45 | 46 | #endif /* THREAD_POOL_H */ 47 | -------------------------------------------------------------------------------- /boost_coroutine/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_executable(generator generator.cpp) 2 | target_link_libraries(generator ${Boost_SYSTEM_LIBRARY} boost_coroutine) 3 | 4 | add_executable(echo_server echo_server.cpp) 5 | target_link_libraries(echo_server ${Boost_SYSTEM_LIBRARY} boost_coroutine) 6 | -------------------------------------------------------------------------------- /boost_coroutine/echo_server.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using boost::asio::ip::tcp; 10 | 11 | class session : public std::enable_shared_from_this 12 | { 13 | public: 14 | explicit session(tcp::socket socket) 15 | : socket_(std::move(socket)), 16 | timer_(socket_.get_io_service()), 17 | strand_(socket_.get_io_service()) 18 | { 19 | } 20 | 21 | void go() 22 | { 23 | auto self(shared_from_this()); 24 | boost::asio::spawn(strand_, 25 | [this, self](boost::asio::yield_context yield) 26 | { 27 | try 28 | { 29 | char data[128]; 30 | for (;;) 31 | { 32 | timer_.expires_from_now(std::chrono::seconds(10)); 33 | std::size_t n = socket_.async_read_some(boost::asio::buffer(data), yield); 34 | boost::asio::async_write(socket_, boost::asio::buffer(data, n), yield); 35 | } 36 | } 37 | catch (std::exception& e) 38 | { 39 | socket_.close(); 40 | timer_.cancel(); 41 | } 42 | }); 43 | 44 | boost::asio::spawn(strand_, 45 | [this, self](boost::asio::yield_context yield) 46 | { 47 | while (socket_.is_open()) 48 | { 49 | boost::system::error_code ignored_ec; 50 | timer_.async_wait(yield[ignored_ec]); 51 | if (timer_.expires_from_now() <= std::chrono::seconds(0)) 52 | socket_.close(); 53 | } 54 | }); 55 | } 56 | 57 | private: 58 | tcp::socket socket_; 59 | boost::asio::steady_timer timer_; 60 | boost::asio::io_service::strand strand_; 61 | }; 62 | 63 | int main(int argc, char* argv[]) 64 | { 65 | try 66 | { 67 | if (argc != 2) 68 | { 69 | std::cerr << "Usage: echo_server \n"; 70 | return 1; 71 | } 72 | 73 | boost::asio::io_service io_service; 74 | 75 | boost::asio::spawn(io_service, 76 | [&](boost::asio::yield_context yield) 77 | { 78 | tcp::acceptor acceptor(io_service, 79 | tcp::endpoint(tcp::v4(), std::atoi(argv[1]))); 80 | 81 | for (;;) 82 | { 83 | boost::system::error_code ec; 84 | tcp::socket socket(io_service); 85 | acceptor.async_accept(socket, yield[ec]); 86 | if (!ec) std::make_shared(std::move(socket))->go(); 87 | } 88 | }); 89 | 90 | io_service.run(); 91 | } 92 | catch (std::exception& e) 93 | { 94 | std::cerr << "Exception: " << e.what() << "\n"; 95 | } 96 | 97 | return 0; 98 | } 99 | 100 | -------------------------------------------------------------------------------- /boost_coroutine/generator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using coro_t = boost::coroutines::coroutine; 7 | 8 | void cat(coro_t::push_type& yield, char *filename) 9 | { 10 | std::ifstream input(filename); 11 | while (true) 12 | { 13 | std::string line; 14 | if (!getline(input, line)) 15 | { 16 | break; 17 | } 18 | yield(line); 19 | } 20 | } 21 | 22 | 23 | int main(int argc, char *argv[]) 24 | { 25 | for (auto &line : coro_t::pull_type([argv](coro_t::push_type &yield) 26 | { 27 | cat(yield, argv[1]); 28 | })) 29 | { 30 | std::cout << line << std::endl; 31 | } 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /boost_coroutine/input.txt: -------------------------------------------------------------------------------- 1 | hello 2 | world 3 | -------------------------------------------------------------------------------- /grpc_dynamic_thread_pool/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(SRCS 2 | main.cpp 3 | dynamic_thread_pool.cpp 4 | ) 5 | 6 | add_executable(grpc_dynamic_thread_pool ${SRCS}) 7 | target_link_libraries(grpc_dynamic_thread_pool ${CMAKE_THREAD_LIBS_INIT}) 8 | -------------------------------------------------------------------------------- /grpc_dynamic_thread_pool/dynamic_thread_pool.cpp: -------------------------------------------------------------------------------- 1 | #include "dynamic_thread_pool.hpp" 2 | 3 | DynamicThreadPool::DynamicThreadPool(int reserve_threads) 4 | : shutdown_(false), 5 | reserve_threads_(reserve_threads), 6 | nthreads_(0), 7 | threads_waiting_(0) 8 | { 9 | for (int i = 0; i < reserve_threads_; i++) 10 | { 11 | std::lock_guard lock(mu_); 12 | nthreads_++; 13 | new DynamicThread(this); 14 | } 15 | } 16 | 17 | DynamicThreadPool::~DynamicThreadPool() 18 | { 19 | std::unique_lock lock_(mu_); 20 | shutdown_ = true; 21 | cv_.notify_all(); 22 | 23 | while (nthreads_ != 0) 24 | { 25 | shutdown_cv_.wait(lock_); 26 | } 27 | 28 | ReapThreads(&dead_threads_); 29 | } 30 | 31 | void DynamicThreadPool::Add(const std::function &callback) 32 | { 33 | std::lock_guard lock(mu_); 34 | 35 | // Add works to the callbacks list 36 | callbacks_.push(callback); 37 | 38 | // Increase pool size or notify as needed 39 | if (threads_waiting_ == 0) 40 | { 41 | // Kick off a new thread 42 | nthreads_++; 43 | new DynamicThread(this); 44 | } 45 | else 46 | { 47 | cv_.notify_one(); 48 | } 49 | 50 | // Also use this chance to harvest dead threads 51 | if (!dead_threads_.empty()) 52 | { 53 | ReapThreads(&dead_threads_); 54 | } 55 | } 56 | 57 | void DynamicThreadPool::ReapThreads(std::list *tlist) 58 | { 59 | for (auto t = tlist->begin(); t != tlist->end(); t = tlist->erase(t)) 60 | { 61 | delete *t; 62 | } 63 | } 64 | 65 | void DynamicThreadPool::ThreadFunc() 66 | { 67 | for (;;) 68 | { 69 | std::unique_lock lock(mu_); 70 | 71 | // Wait until work is available or we are shutting down. 72 | if (!shutdown_ && callbacks_.empty()) 73 | { 74 | // If there are too many threads waiting, then quit this thread 75 | if (threads_waiting_ >= reserve_threads_) 76 | { 77 | break; 78 | } 79 | 80 | threads_waiting_++; 81 | cv_.wait(lock); 82 | threads_waiting_--; 83 | } 84 | 85 | // Drain callbacks before considering shutdown to ensure all work gets completed. 86 | if (!callbacks_.empty()) 87 | { 88 | auto cb = callbacks_.front(); 89 | callbacks_.pop(); 90 | lock.unlock(); 91 | cb(); 92 | } 93 | else if (shutdown_) 94 | { 95 | break; 96 | } 97 | } 98 | } 99 | 100 | DynamicThreadPool::DynamicThread::DynamicThread(DynamicThreadPool *pool) 101 | : pool_(pool), 102 | thd_(new std::thread(&DynamicThreadPool::DynamicThread::ThreadFunc, this)) 103 | { 104 | } 105 | 106 | DynamicThreadPool::DynamicThread::~DynamicThread() 107 | { 108 | thd_->join(); 109 | thd_.reset(); 110 | } 111 | 112 | void DynamicThreadPool::DynamicThread::ThreadFunc() 113 | { 114 | pool_->ThreadFunc(); 115 | 116 | // Now that we have killed ourselves, we should reduce the thread count 117 | std::unique_lock lock(pool_->mu_); 118 | pool_->nthreads_--; 119 | 120 | // Move ourselves to dead list 121 | pool_->dead_threads_.push_back(this); 122 | 123 | if ((pool_->shutdown_) && (pool_->nthreads_ == 0)) 124 | { 125 | pool_->shutdown_cv_.notify_one(); 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /grpc_dynamic_thread_pool/dynamic_thread_pool.hpp: -------------------------------------------------------------------------------- 1 | #ifndef DYNAMIC_THREAD_POOL_H 2 | #define DYNAMIC_THREAD_POOL_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | class DynamicThreadPool 11 | { 12 | public: 13 | explicit DynamicThreadPool(int reserve_threads); 14 | ~DynamicThreadPool(); 15 | 16 | void Add(const std::function &callback); 17 | 18 | private: 19 | class DynamicThread 20 | { 21 | public: 22 | DynamicThread(DynamicThreadPool* pool); 23 | ~DynamicThread(); 24 | 25 | private: 26 | DynamicThreadPool* pool_; 27 | std::unique_ptr thd_; 28 | void ThreadFunc(); 29 | }; 30 | 31 | std::mutex mu_; 32 | std::condition_variable cv_; 33 | std::condition_variable shutdown_cv_; 34 | bool shutdown_; 35 | std::queue> callbacks_; 36 | int reserve_threads_; 37 | int nthreads_; 38 | int threads_waiting_; 39 | std::list dead_threads_; 40 | 41 | void ThreadFunc(); 42 | static void ReapThreads(std::list* tlist); 43 | }; 44 | 45 | #endif /* DYNAMIC_THREAD_POOL_H */ 46 | -------------------------------------------------------------------------------- /grpc_dynamic_thread_pool/main.cpp: -------------------------------------------------------------------------------- 1 | #include "dynamic_thread_pool.hpp" 2 | #include 3 | 4 | void fun() 5 | { 6 | std::cout << "Hello, World!" << std::endl; 7 | } 8 | 9 | int main(int argc, char *argv[]) 10 | { 11 | DynamicThreadPool pool(std::thread::hardware_concurrency()); 12 | pool.Add(fun); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /jsonschema/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/include) 2 | 3 | add_executable(jsonschema jsonschema.cpp) 4 | -------------------------------------------------------------------------------- /jsonschema/error.hpp: -------------------------------------------------------------------------------- 1 | #ifndef ERROR_H 2 | #define ERROR_H 3 | 4 | #include 5 | 6 | class Error 7 | { 8 | public: 9 | Error() 10 | : hasError_(false) 11 | { 12 | } 13 | 14 | Error(const std::string &reason) 15 | : hasError_(true), 16 | reason_(reason) 17 | { 18 | } 19 | 20 | Error(std::string &&reason) noexcept 21 | : hasError_(true), 22 | reason_(std::move(reason)) 23 | { 24 | } 25 | 26 | operator bool() const 27 | { 28 | return hasError_; 29 | } 30 | 31 | const std::string &reason() const 32 | { 33 | return reason_; 34 | } 35 | 36 | private: 37 | bool hasError_; 38 | std::string reason_; 39 | }; 40 | 41 | #endif /* ERROR_H */ 42 | -------------------------------------------------------------------------------- /jsonschema/jsonreader.hpp: -------------------------------------------------------------------------------- 1 | #ifndef JSONREADER_H 2 | #define JSONREADER_H 3 | 4 | #include "error.hpp" 5 | 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | 12 | class JSONReader 13 | { 14 | public: 15 | JSONReader() = default; 16 | 17 | rapidjson::Document readJSON(const std::string &path, Error &err) 18 | { 19 | rapidjson::Document doc; 20 | 21 | FILE *fp = fopen(path.c_str(), "r"); 22 | if (fp == nullptr) 23 | { 24 | err = Error(std::string("JSON file ") + path + " not found"); 25 | } 26 | else 27 | { 28 | rapidjson::FileReadStream fs(fp, buff_.data(), BUFFER_SIZE); 29 | doc.ParseStream(fs); 30 | if (doc.HasParseError()) 31 | { 32 | err = Error(std::string("JSON file ") + path + " is invalid"); 33 | } 34 | fclose(fp); 35 | } 36 | 37 | return doc; 38 | } 39 | 40 | private: 41 | static constexpr std::size_t BUFFER_SIZE = 1 * 1024 * 1024; 42 | std::array buff_; 43 | }; 44 | 45 | #endif /* JSONREADER_H */ 46 | -------------------------------------------------------------------------------- /jsonschema/jsonschema.cpp: -------------------------------------------------------------------------------- 1 | #include "jsonreader.hpp" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | std::string schemaPath = "schema.json"; 7 | std::string jsonPath = "test.json"; 8 | 9 | JSONReader reader; 10 | 11 | Error err; 12 | rapidjson::Document sd = reader.readJSON(schemaPath, err); 13 | if (err) 14 | { 15 | std::cerr << "Read schema file error: " << err.reason() << std::endl; 16 | exit(EXIT_FAILURE); 17 | } 18 | 19 | rapidjson::SchemaDocument schema(sd); 20 | rapidjson::SchemaValidator validator(schema); 21 | 22 | rapidjson::Document json = reader.readJSON(jsonPath, err); 23 | if (err) 24 | { 25 | std::cerr << "Read JSON file error: " << err.reason() << std::endl; 26 | exit(EXIT_FAILURE); 27 | } 28 | 29 | if (!json.Accept(validator)) 30 | { 31 | std::cout << "NO! Input JSON file " << jsonPath << " not match the schema " << "" << std::endl; 32 | } 33 | else 34 | { 35 | std::cout << "YES! Input JSON file " << jsonPath << " match the schema " << "" << std::endl; 36 | } 37 | validator.Reset(); 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /prometheus/deployment.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: extensions/v1beta1 2 | kind: Deployment 3 | metadata: 4 | name: hello-app 5 | spec: 6 | replicas: 1 7 | selector: 8 | matchLabels: 9 | app: hello-app 10 | template: 11 | metadata: 12 | labels: 13 | app: hello-app 14 | spec: 15 | containers: 16 | - name: flask-app 17 | image: senlinzhan/hello-app:v0.2 18 | ports: 19 | - containerPort: 5000 20 | -------------------------------------------------------------------------------- /prometheus/docker/Dockerfile: -------------------------------------------------------------------------------- 1 | from ubuntu:latest 2 | 3 | RUN apt-get update && apt-get install -y 4 | RUN apt-get install -y gunicorn python-pip python-dev build-essential 5 | 6 | WORKDIR /app 7 | COPY ./requirements.txt /app 8 | COPY ./app.py /app 9 | COPY ./gunicorn_config.py /app 10 | 11 | RUN pip install -r requirements.txt 12 | CMD ["/usr/bin/gunicorn", "--config", "gunicorn_config.py", "app:app"] 13 | 14 | EXPOSE 5000 15 | -------------------------------------------------------------------------------- /prometheus/docker/app.py: -------------------------------------------------------------------------------- 1 | import time 2 | 3 | from flask import Flask, Response, request 4 | from prometheus_client import ( 5 | Counter, Histogram, CONTENT_TYPE_LATEST, generate_latest 6 | ) 7 | 8 | APP_NAME = 'hello-app' 9 | 10 | REQUEST_COUNT = Counter( 11 | 'request_count', 'App Request Count', 12 | ['app_name', 'method', 'endpoint', 'http_status'] 13 | ) 14 | 15 | REQUEST_LATENCY = Histogram( 16 | 'request_latency_seconds', 'Request latency', 17 | ['app_name', 'endpoint'] 18 | ) 19 | 20 | 21 | def start_timer(): 22 | request.start_time = time.time() 23 | 24 | 25 | def stop_timer(response): 26 | resp_time = time.time() - request.start_time 27 | REQUEST_LATENCY.labels(APP_NAME, request.path).observe(resp_time) 28 | return response 29 | 30 | 31 | def record_request_data(response): 32 | REQUEST_COUNT.labels( 33 | APP_NAME, request.method, request.path, response.status_code 34 | ).inc() 35 | return response 36 | 37 | 38 | def setup_metrics(app): 39 | app.before_request(start_timer) 40 | 41 | # The order here matters since we want stop_timer to be executed first 42 | app.after_request(record_request_data) 43 | app.after_request(stop_timer) 44 | 45 | 46 | app = Flask(__name__) 47 | setup_metrics(app) 48 | 49 | @app.route('/') 50 | def index(): 51 | return 'Hello, World!' 52 | 53 | 54 | @app.route('/error') 55 | def error(): 56 | 1 / 0 57 | return 'Hello, World!' 58 | 59 | 60 | @app.errorhandler(500) 61 | def handle_500(error): 62 | return str(error), 500 63 | 64 | 65 | @app.route('/metrics') 66 | def metrics(): 67 | return Response( 68 | generate_latest(), 69 | mimetype=CONTENT_TYPE_LATEST 70 | ) 71 | 72 | 73 | if __name__ == '__main__': 74 | app.run(host='0.0.0.0', port=5000) 75 | -------------------------------------------------------------------------------- /prometheus/docker/requirements.txt: -------------------------------------------------------------------------------- 1 | prometheus-client==0.1.1 2 | Flask==0.12.2 3 | -------------------------------------------------------------------------------- /prometheus/service.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | name: hello-app 5 | labels: 6 | name: hello-app 7 | spec: 8 | ports: 9 | - port: 80 10 | targetPort: 5000 11 | selector: 12 | app: hello-app 13 | --------------------------------------------------------------------------------