diff --git a/src/cpp/ripple/LoadManager.cpp b/src/cpp/ripple/LoadManager.cpp index 0b040c5ad..4cb8d20ee 100644 --- a/src/cpp/ripple/LoadManager.cpp +++ b/src/cpp/ripple/LoadManager.cpp @@ -1,5 +1,22 @@ #include "LoadManager.h" +LoadManager::LoadManager(int creditRate, int creditLimit, int debitWarn, int debitLimit) : + mCreditRate(creditRate), mCreditLimit(creditLimit), mDebitWarn(debitWarn), mDebitLimit(debitLimit) +{ + addLoadCost(LoadCost(LT_InvalidRequest, 10, LC_CPU | LC_Network)); + addLoadCost(LoadCost(LT_RequestNoReply, 1, LC_CPU | LC_Disk)); + addLoadCost(LoadCost(LT_InvalidSignature, 100, LC_CPU)); + addLoadCost(LoadCost(LT_UnwantedData, 5, LC_CPU | LC_Network)); + + addLoadCost(LoadCost(LT_NewTrusted, 10, 0)); + addLoadCost(LoadCost(LT_NewTransaction, 2, 0)); + addLoadCost(LoadCost(LT_NeededData, 10, 0)); + + addLoadCost(LoadCost(LT_RequestData, 5, LC_Disk | LC_Network)); + addLoadCost(LoadCost(LT_CheapQuery, 1, LC_CPU)); +} + + int LoadManager::getCreditRate() const { boost::mutex::scoped_lock sl(mLock); diff --git a/src/cpp/ripple/LoadManager.h b/src/cpp/ripple/LoadManager.h index 08478e140..45d1396c2 100644 --- a/src/cpp/ripple/LoadManager.h +++ b/src/cpp/ripple/LoadManager.h @@ -1,10 +1,46 @@ #ifndef LOADSOURCE__H #define LOADSOURCE__H +#include + #include #include "types.h" +enum LoadType +{ // types of load that can be placed on the server + + // Bad things + LT_InvalidRequest, // A request that we can immediately tell is invalid + LT_RequestNoReply, // A request that we cannot satisfy + LT_InvalidSignature, // An object whose signature we had to check and it failed + LT_UnwantedData, // Data we have no use for + + // Good things + LT_NewTrusted, // A new transaction/validation/proposal we trust + LT_NewTransaction, // A new, valid transaction + LT_NeededData, // Data we requested + + // Requests + LT_RequestData, // A request that is hard to satisfy, disk access + LT_CheapQuery, // A query that is trivial, cached data +}; + +// load categoryies +static const int LC_Disk = 1; +static const int LC_CPU = 2; +static const int LC_Network = 4; + +class LoadCost +{ +public: + LoadType mType; + int mCost; + int mCategories; + + LoadCost(LoadType t, int cost, int cat) : mType(t), mCost(cost), mCategories(cat) { ; } +}; + class LoadSource { // a single endpoint that can impose load friend class LoadManager; @@ -47,10 +83,13 @@ protected: void canonicalize(LoadSource&, const time_t now) const; + std::vector mCosts; + + void addLoadCost(const LoadCost& c) { mCosts[static_cast(c.mType)] = c; } + public: - LoadManager(int creditRate, int creditLimit, int debitWarn, int debitLimit) : - mCreditRate(creditRate), mCreditLimit(creditLimit), mDebitWarn(debitWarn), mDebitLimit(debitLimit) { ; } + LoadManager(int creditRate, int creditLimit, int debitWarn, int debitLimit); int getCreditRate() const; int getCreditLimit() const; @@ -64,6 +103,8 @@ public: bool shouldWarn(LoadSource&) const; bool shouldCutoff(LoadSource&) const; bool adjust(LoadSource&, int credits) const; // return value: false=balance okay, true=warn/cutoff + + int getCost(LoadType t) { return mCosts[static_cast(t)].mCost; } }; class LoadFeeTrack