Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Weights.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2025, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include "util/StringHash.hpp"
23#include "util/config/ConfigDefinition.hpp"
24#include "web/dosguard/WeightsInterface.hpp"
25
26#include <boost/json/object.hpp>
27
28#include <cstddef>
29#include <functional>
30#include <optional>
31#include <string>
32#include <unordered_map>
33
34namespace web::dosguard {
35
43class Weights : public WeightsInterface {
44public:
50 struct Entry {
51 size_t weight;
52 std::optional<size_t> weightLedgerCurrent;
53 std::optional<size_t> weightLedgerValidated;
54 };
55
56private:
57 size_t defaultWeight_;
58 std::unordered_map<std::string, Entry, util::StringHash, std::equal_to<>> weights_;
59
60public:
67 Weights(size_t defaultWeight, std::unordered_map<std::string, Entry> weights);
68
75 static Weights
77
84 size_t
85 requestWeight(boost::json::object const& request) const override;
86};
87
88} // namespace web::dosguard
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:54
Interface for determining request weights in DOS protection.
Definition WeightsInterface.hpp:34
Implementation of WeightsInterface that manages command weights for DosGuard.
Definition Weights.hpp:43
static Weights make(util::config::ClioConfigDefinition const &config)
Create a Weights object from configuration.
Definition Weights.cpp:45
Weights(size_t defaultWeight, std::unordered_map< std::string, Entry > weights)
Construct a new Weights object.
Definition Weights.cpp:39
size_t requestWeight(boost::json::object const &request) const override
Get the weight assigned to a specific command.
Definition Weights.cpp:62
Structure representing weight configuration for a command.
Definition Weights.hpp:50