Skip to content

Commit

Permalink
refactor: Move interval timer into a separate class (#1588)
Browse files Browse the repository at this point in the history
For #442.
  • Loading branch information
kuznetsss authored Aug 7, 2024
1 parent 27c9e2a commit 1b4eed3
Show file tree
Hide file tree
Showing 30 changed files with 498 additions and 288 deletions.
4 changes: 2 additions & 2 deletions src/app/ClioApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ ClioApplication::run()
boost::asio::io_context ioc{threads};

// Rate limiter, to prevent abuse
auto sweepHandler = web::IntervalSweepHandler{config_, ioc};
auto whitelistHandler = web::WhitelistHandler{config_};
auto dosGuard = web::DOSGuard{config_, whitelistHandler, sweepHandler};
auto dosGuard = web::DOSGuard{config_, whitelistHandler};
auto sweepHandler = web::IntervalSweepHandler{config_, ioc, dosGuard};

// Interface to the database
auto backend = data::make_Backend(config_);
Expand Down
1 change: 1 addition & 0 deletions src/etl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ target_sources(
NetworkValidatedLedgers.cpp
NFTHelpers.cpp
Source.cpp
impl/AmendmentBlockHandler.cpp
impl/ForwardingCache.cpp
impl/ForwardingSource.cpp
impl/GrpcSource.cpp
Expand Down
7 changes: 3 additions & 4 deletions src/etl/ETLService.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
#include "data/BackendInterface.hpp"
#include "data/LedgerCache.hpp"
#include "etl/CacheLoader.hpp"
#include "etl/ETLHelpers.hpp"
#include "etl/ETLState.hpp"
#include "etl/LoadBalancer.hpp"
#include "etl/NetworkValidatedLedgersInterface.hpp"
#include "etl/SystemState.hpp"
#include "etl/impl/AmendmentBlock.hpp"
#include "etl/impl/AmendmentBlockHandler.hpp"
#include "etl/impl/ExtractionDataPipe.hpp"
#include "etl/impl/Extractor.hpp"
#include "etl/impl/LedgerFetcher.hpp"
Expand All @@ -37,7 +37,6 @@
#include "util/log/Logger.hpp"

#include <boost/asio/io_context.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/json/object.hpp>
#include <grpcpp/grpcpp.h>
#include <org/xrpl/rpc/v1/get_ledger.pb.h>
Expand Down Expand Up @@ -85,7 +84,7 @@ class ETLService {
using ExtractorType = etl::impl::Extractor<DataPipeType, LedgerFetcherType>;
using LedgerLoaderType = etl::impl::LedgerLoader<LoadBalancerType, LedgerFetcherType>;
using LedgerPublisherType = etl::impl::LedgerPublisher<CacheType>;
using AmendmentBlockHandlerType = etl::impl::AmendmentBlockHandler<>;
using AmendmentBlockHandlerType = etl::impl::AmendmentBlockHandler;
using TransformerType =
etl::impl::Transformer<DataPipeType, LedgerLoaderType, LedgerPublisherType, AmendmentBlockHandlerType>;

Expand Down
95 changes: 0 additions & 95 deletions src/etl/impl/AmendmentBlock.hpp

This file was deleted.

56 changes: 56 additions & 0 deletions src/etl/impl/AmendmentBlockHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//------------------------------------------------------------------------------
/*
This file is part of clio: https://github.com/XRPLF/clio
Copyright (c) 2024, the clio developers.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================

#include "etl/impl/AmendmentBlockHandler.hpp"

#include "etl/SystemState.hpp"
#include "util/log/Logger.hpp"

#include <boost/asio/io_context.hpp>

#include <chrono>
#include <functional>
#include <utility>

namespace etl::impl {

AmendmentBlockHandler::ActionType const AmendmentBlockHandler::defaultAmendmentBlockAction = []() {
static util::Logger const log{"ETL"};
LOG(log.fatal()) << "Can't process new ledgers: The current ETL source is not compatible with the version of "
<< "the libxrpl Clio is currently using. Please upgrade Clio to a newer version.";
};

AmendmentBlockHandler::AmendmentBlockHandler(
boost::asio::io_context& ioc,
SystemState& state,
std::chrono::steady_clock::duration interval,
ActionType action
)
: state_{std::ref(state)}, repeat_{ioc}, interval_{interval}, action_{std::move(action)}
{
}

void
AmendmentBlockHandler::onAmendmentBlock()
{
state_.get().isAmendmentBlocked = true;
repeat_.start(interval_, action_);
}

} // namespace etl::impl
59 changes: 59 additions & 0 deletions src/etl/impl/AmendmentBlockHandler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//------------------------------------------------------------------------------
/*
This file is part of clio: https://github.com/XRPLF/clio
Copyright (c) 2023, the clio developers.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================

#pragma once

#include "etl/SystemState.hpp"
#include "util/Repeat.hpp"

#include <boost/asio/io_context.hpp>
#include <boost/asio/post.hpp>
#include <boost/asio/steady_timer.hpp>

#include <chrono>
#include <functional>

namespace etl::impl {

class AmendmentBlockHandler {
public:
using ActionType = std::function<void()>;

private:
std::reference_wrapper<SystemState> state_;
util::Repeat repeat_;
std::chrono::steady_clock::duration interval_;

ActionType action_;

public:
static ActionType const defaultAmendmentBlockAction;

AmendmentBlockHandler(
boost::asio::io_context& ioc,
SystemState& state,
std::chrono::steady_clock::duration interval = std::chrono::seconds{1},
ActionType action = defaultAmendmentBlockAction
);

void
onAmendmentBlock();
};

} // namespace etl::impl
2 changes: 1 addition & 1 deletion src/etl/impl/Transformer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "data/DBHelpers.hpp"
#include "data/Types.hpp"
#include "etl/SystemState.hpp"
#include "etl/impl/AmendmentBlock.hpp"
#include "etl/impl/AmendmentBlockHandler.hpp"
#include "etl/impl/LedgerLoader.hpp"
#include "util/Assert.hpp"
#include "util/LedgerUtils.hpp"
Expand Down
3 changes: 2 additions & 1 deletion src/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ target_sources(
prometheus/Prometheus.cpp
Random.cpp
Retry.cpp
SignalsHandler.cpp
Repeat.cpp
requests/RequestBuilder.cpp
requests/Types.cpp
requests/WsConnection.cpp
requests/impl/SslContext.cpp
SignalsHandler.cpp
Taggable.cpp
TerminationHandler.cpp
TimeUtils.cpp
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//------------------------------------------------------------------------------
/*
This file is part of clio: https://github.com/XRPLF/clio
Copyright (c) 2023, the clio developers.
Copyright (c) 2024, the clio developers.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
Expand All @@ -17,17 +17,22 @@
*/
//==============================================================================

#pragma once
#include "util/Repeat.hpp"

#include <cstddef>
#include <functional>
#include <boost/asio/io_context.hpp>

struct FakeAmendmentBlockAction {
std::reference_wrapper<std::size_t> callCount;
namespace util {

void
operator()() const
{
++(callCount.get());
}
};
Repeat::Repeat(boost::asio::io_context& ioc) : timer_(ioc)
{
}

void
Repeat::stop()
{
stopping_ = true;
timer_.cancel();
semaphore_.acquire();
}

} // namespace util
Loading

0 comments on commit 1b4eed3

Please sign in to comment.