Skip to content

Commit

Permalink
Merge pull request #29 from nsg-ethz/addAsyncClient
Browse files Browse the repository at this point in the history
Add asynchronous client thread pool executor
  • Loading branch information
UsualSpec authored Oct 17, 2024
2 parents 193d872 + 03351ce commit 3f107ea
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 9 additions & 1 deletion client/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -802,11 +802,19 @@ void AutopowerClient::manageMsmt() {
std::cout << "Registering at server..." << std::endl;
std::unique_ptr<grpc::ClientReader<autopapi::srvRequest>> serverApiStream(stub->registerClient(&cc, cluid));

// create a executor to allow multiple concurrently running sRequests
// follows boosts thread_pool as described in https://stackoverflow.com/a/54436167
boost::asio::thread_pool srvRequestPool(4); // only allow 4 concurrent requests. There should not be too many anyway
while (serverApiStream->Read(&sRequest)) {
boost::asio::post(srvRequestPool, [this, sRequest, cluid] {
handleSrvRequest(sRequest, cluid);
});
// wait for requests from server.
handleSrvRequest(sRequest, cluid);
//handleSrvRequest(sRequest, cluid);
}

srvRequestPool.join();

grpc::Status sf = serverApiStream->Finish();
std::cerr << sf.error_code() << ": " << sf.error_message() << ": " << sf.error_details() << std::endl;
std::cerr << "Server stream exited. Will attempt re-register." << std::endl;
Expand Down
2 changes: 2 additions & 0 deletions client/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <shared_mutex>
#include <string>
#include <vector>
#include <boost/asio/thread_pool.hpp>
#include <boost/asio/post.hpp>

// describes a measurement sample
struct CMsmtSample {
Expand Down

0 comments on commit 3f107ea

Please sign in to comment.