Skip to content

Commit

Permalink
add keepAlive logic for front registerNode
Browse files Browse the repository at this point in the history
  • Loading branch information
cyjseagull committed Sep 9, 2024
1 parent f2f1b39 commit 568b3ef
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ GatewayImpl::GatewayImpl(Service::Ptr const& service,
boost::placeholders::_2));
m_gatewayRouterManager = std::make_shared<GatewayRouterManager>(
m_service, m_gatewayInfoFactory, m_localRouter, m_peerRouter);

// create and set the health-check handler
}

void GatewayImpl::start()
Expand Down
29 changes: 29 additions & 0 deletions cpp/wedpr-transport/sdk/Common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (C) 2022 WeDPR.
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @file Common.h
* @author: shawnhe
* @date 2022-10-23
*/

#pragma once

#include "ppc-framework/Common.h"
#include <bcos-utilities/BoostLog.h>

namespace ppc::sdk
{
#define TRANSPORT_LOG(LEVEL) BCOS_LOG(LEVEL) << "[TRASPORT]"
} // namespace ppc::sdk
36 changes: 32 additions & 4 deletions cpp/wedpr-transport/sdk/ProTransportImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* @date 2024-09-04
*/
#include "ProTransportImpl.h"
#include "Common.h"
#include "protocol/src/v1/MessageImpl.h"
#include "wedpr-protocol/grpc/client/GatewayClient.h"
#include "wedpr-protocol/grpc/server/FrontServer.h"
Expand All @@ -28,19 +29,19 @@ using namespace ppc::protocol;
using namespace ppc::sdk;


ProTransportImpl::ProTransportImpl(ppc::front::FrontConfig::Ptr config)
: m_config(std::move(config))
ProTransportImpl::ProTransportImpl(ppc::front::FrontConfig::Ptr config, int keepAlivePeriodMs)
: m_config(std::move(config)), m_keepAlivePeriodMs(keepAlivePeriodMs)
{
// default enable health-check
auto grpcServerConfig = std::make_shared<GrpcServerConfig>(config->selfEndPoint(), true);
m_server = std::make_shared<GrpcServer>(grpcServerConfig);

FrontFactory frontFactory;
auto gateway =
m_gateway =
std::make_shared<GatewayClient>(m_config->grpcConfig(), m_config->gatewayGrpcTarget());
m_front = frontFactory.build(std::make_shared<NodeInfoFactory>(),
std::make_shared<MessagePayloadBuilderImpl>(),
std::make_shared<MessageOptionalHeaderBuilderImpl>(), gateway, config);
std::make_shared<MessageOptionalHeaderBuilderImpl>(), m_gateway, config);

auto msgBuilder =
std::make_shared<MessageBuilderImpl>(std::make_shared<MessageHeaderBuilderImpl>());
Expand All @@ -52,11 +53,38 @@ ProTransportImpl::ProTransportImpl(ppc::front::FrontConfig::Ptr config)

void ProTransportImpl::start()
{
m_timer = std::make_shared<bcos::Timer>(m_keepAlivePeriodMs, "frontKeepAlive");
auto self = weak_from_this();
m_timer->registerTimeoutHandler([self]() {
auto transport = self.lock();
if (!transport)
{
return;
}
transport->keepAlive();
});
m_timer->start();
m_server->start();
m_front->start();
}

void ProTransportImpl::stop()
{
m_timer->stop();
m_server->stop();
m_front->stop();
}

void ProTransportImpl::keepAlive()
{
try
{
m_gateway->registerNodeInfo(m_config->generateNodeInfo());
}
catch (std::exception const& e)
{
TRANSPORT_LOG(WARNING) << LOG_DESC("keepAlive exception")
<< LOG_KV("error", boost::diagnostic_information(e));
}
m_timer->restart();
}
11 changes: 9 additions & 2 deletions cpp/wedpr-transport/sdk/ProTransportImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
#pragma once
#include "TransportImpl.h"
#include "bcos-utilities/Timer.h"

namespace ppc::protocol
{
Expand All @@ -28,17 +29,23 @@ class GrpcServer;

namespace ppc::sdk
{
class ProTransportImpl : public Transport
class ProTransportImpl : public Transport, public std::enable_shared_from_this<ProTransportImpl>
{
public:
using Ptr = std::shared_ptr<ProTransportImpl>;
ProTransportImpl(ppc::front::FrontConfig::Ptr config);
ProTransportImpl(ppc::front::FrontConfig::Ptr config, int keepAlivePeriodMs = 3000);

void start() override;
void stop() override;

protected:
void keepAlive();

protected:
ppc::front::FrontConfig::Ptr m_config;
ppc::gateway::IGateway::Ptr m_gateway;
std::shared_ptr<ppc::protocol::GrpcServer> m_server;
int m_keepAlivePeriodMs;
std::shared_ptr<bcos::Timer> m_timer;
};
} // namespace ppc::sdk

0 comments on commit 568b3ef

Please sign in to comment.