forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor resource manager (envoyproxy#11182)
This patch separates the Resource class from the resource manager implementation and allows for resource limit tracking in other parts of the code base. Signed-off-by: Tony Allen <[email protected]>
- Loading branch information
Showing
15 changed files
with
253 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include <cstdint> | ||
|
||
#include "envoy/common/pure.h" | ||
|
||
#pragma once | ||
|
||
namespace Envoy { | ||
|
||
/** | ||
* A handle for use by any resource managers. | ||
*/ | ||
class ResourceLimit { | ||
public: | ||
virtual ~ResourceLimit() = default; | ||
|
||
/** | ||
* @return true if the resource can be created. | ||
*/ | ||
virtual bool canCreate() PURE; | ||
|
||
/** | ||
* Increment the resource count. | ||
*/ | ||
virtual void inc() PURE; | ||
|
||
/** | ||
* Decrement the resource count. | ||
*/ | ||
virtual void dec() PURE; | ||
|
||
/** | ||
* Decrement the resource count by a specific amount. | ||
*/ | ||
virtual void decBy(uint64_t amount) PURE; | ||
|
||
/** | ||
* @return the current maximum allowed number of this resource. | ||
*/ | ||
virtual uint64_t max() PURE; | ||
|
||
/** | ||
* @return the current resource count. | ||
*/ | ||
virtual uint64_t count() const PURE; | ||
}; | ||
|
||
} // namespace Envoy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#pragma once | ||
|
||
#include <limits> | ||
|
||
#include "envoy/common/resource.h" | ||
#include "envoy/runtime/runtime.h" | ||
|
||
#include "common/common/assert.h" | ||
|
||
#include "absl/types/optional.h" | ||
|
||
namespace Envoy { | ||
|
||
/** | ||
* A handle to track some limited resource. | ||
* | ||
* NOTE: | ||
* This implementation makes some assumptions which favor simplicity over correctness. Though | ||
* atomics are used, it is possible for resources to temporarily go above the supplied maximums. | ||
* This should not effect overall behavior. | ||
*/ | ||
class BasicResourceLimitImpl : public ResourceLimit { | ||
public: | ||
BasicResourceLimitImpl(uint64_t max, Runtime::Loader& runtime, const std::string& runtime_key) | ||
: max_(max), runtime_(&runtime), runtime_key_(runtime_key) {} | ||
BasicResourceLimitImpl(uint64_t max) : max_(max), runtime_(nullptr) {} | ||
BasicResourceLimitImpl() : max_(std::numeric_limits<uint64_t>::max()), runtime_(nullptr) {} | ||
|
||
bool canCreate() override { return current_.load() < max(); } | ||
|
||
void inc() override { ++current_; } | ||
|
||
void dec() override { decBy(1); } | ||
|
||
void decBy(uint64_t amount) override { | ||
ASSERT(current_ >= amount); | ||
current_ -= amount; | ||
} | ||
|
||
uint64_t max() override { | ||
return (runtime_ != nullptr && runtime_key_.has_value()) | ||
? runtime_->snapshot().getInteger(runtime_key_.value(), max_) | ||
: max_; | ||
} | ||
|
||
uint64_t count() const override { return current_.load(); } | ||
|
||
void setMax(uint64_t new_max) { max_ = new_max; } | ||
void resetMax() { max_ = std::numeric_limits<uint64_t>::max(); } | ||
|
||
protected: | ||
std::atomic<uint64_t> current_{}; | ||
|
||
private: | ||
uint64_t max_; | ||
Runtime::Loader* runtime_{nullptr}; | ||
const absl::optional<std::string> runtime_key_; | ||
}; | ||
|
||
} // namespace Envoy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.