2727#include < boost/asio/io_context.hpp>
2828#include < boost/asio/ip/tcp.hpp>
2929#include < fmt/format.h>
30+ #include < grpc/grpc.h>
3031#include < grpcpp/client_context.h>
3132#include < grpcpp/security/credentials.h>
3233#include < grpcpp/support/channel_arguments.h>
3334#include < grpcpp/support/status.h>
3435#include < org/xrpl/rpc/v1/get_ledger.pb.h>
3536#include < org/xrpl/rpc/v1/xrp_ledger.grpc.pb.h>
3637
38+ #include < chrono>
3739#include < cstddef>
3840#include < cstdint>
3941#include < exception>
@@ -52,17 +54,25 @@ GrpcSource::GrpcSource(std::string const& ip, std::string const& grpcPort, std::
5254 try {
5355 boost::asio::io_context ctx;
5456 boost::asio::ip::tcp::resolver resolver{ctx};
57+
5558 auto const resolverResult = resolver.resolve (ip, grpcPort);
56- if (resolverResult.empty ()) {
59+ if (resolverResult.empty ())
5760 throw std::runtime_error (" Failed to resolve " + ip + " :" + grpcPort);
58- }
61+
5962 std::stringstream ss;
6063 ss << resolverResult.begin ()->endpoint ();
64+
6165 grpc::ChannelArguments chArgs;
6266 chArgs.SetMaxReceiveMessageSize (-1 );
67+ chArgs.SetInt (GRPC_ARG_KEEPALIVE_TIME_MS, kKEEPALIVE_PING_INTERVAL_MS );
68+ chArgs.SetInt (GRPC_ARG_KEEPALIVE_TIMEOUT_MS, kKEEPALIVE_TIMEOUT_MS );
69+ chArgs.SetInt (GRPC_ARG_KEEPALIVE_PERMIT_WITHOUT_CALLS, static_cast <int >(kKEEPALIVE_PERMIT_WITHOUT_CALLS ));
70+ chArgs.SetInt (GRPC_ARG_HTTP2_MAX_PINGS_WITHOUT_DATA, kMAX_PINGS_WITHOUT_DATA );
71+
6372 stub_ = org::xrpl::rpc::v1::XRPLedgerAPIService::NewStub (
6473 grpc::CreateCustomChannel (ss.str (), grpc::InsecureChannelCredentials (), chArgs)
6574 );
75+
6676 LOG (log_.debug ()) << " Made stub for remote." ;
6777 } catch (std::exception const & e) {
6878 LOG (log_.warn ()) << " Exception while creating stub: " << e.what () << " ." ;
@@ -76,10 +86,11 @@ GrpcSource::fetchLedger(uint32_t sequence, bool getObjects, bool getObjectNeighb
7686 if (!stub_)
7787 return {{grpc::StatusCode::INTERNAL, " No Stub" }, response};
7888
79- // Ledger header with txns and metadata
8089 org::xrpl::rpc::v1::GetLedgerRequest request;
8190 grpc::ClientContext context;
8291
92+ context.set_deadline (std::chrono::system_clock::now () + kDEADLINE ); // Prevent indefinite blocking
93+
8394 request.mutable_ledger ()->set_sequence (sequence);
8495 request.set_transactions (true );
8596 request.set_expand (true );
0 commit comments