-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add thread local cache of overload action states #4090
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
#pragma once | ||
|
||
#include <unordered_map> | ||
|
||
#include "envoy/common/pure.h" | ||
#include "envoy/thread_local/thread_local.h" | ||
|
||
namespace Envoy { | ||
namespace Server { | ||
|
@@ -21,6 +24,22 @@ enum class OverloadActionState { | |
*/ | ||
typedef std::function<void(OverloadActionState)> OverloadActionCb; | ||
|
||
/** | ||
* Thread-local cache of the current state of each configured overload action. | ||
*/ | ||
class OverloadActionStateCache : public ThreadLocal::ThreadLocalObject { | ||
public: | ||
bool isActive(const std::string& action) const { | ||
auto it = actions_.find(action); | ||
return it != actions_.end() && it->second == OverloadActionState::Active; | ||
} | ||
|
||
void setState(const std::string& action, OverloadActionState state) { actions_[action] = state; } | ||
|
||
private: | ||
std::unordered_map<std::string, OverloadActionState> actions_; | ||
}; | ||
|
||
/** | ||
* The OverloadManager protects the Envoy instance from being overwhelmed by client | ||
* requests. It monitors a set of resources and notifies registered listeners if | ||
|
@@ -46,6 +65,12 @@ class OverloadManager { | |
*/ | ||
virtual void registerForAction(const std::string& action, Event::Dispatcher& dispatcher, | ||
OverloadActionCb callback) PURE; | ||
|
||
/** | ||
* Get the thread-local overload action state cache. Lookups in this cache can be used as | ||
* an alternative to registering a callback for overload action state changes. | ||
*/ | ||
virtual const OverloadActionStateCache& getOverloadActionStateCache() PURE; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I might name this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed to ThreadLocalOverloadState |
||
}; | ||
|
||
} // namespace Server | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is eventually going to be called on the data path on HCM, I guess I'm wonder out loud whether we want to do a string hash/compare on new stream? It's probably lost in the noise of the overall overhead of new stream creation. An alternative would be to establish a map from the action strings to opaque integer tags that HCM can obtain at instantiation. Seems like micro-optimization, just curious about your thoughts here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea - changed to allow doing the map lookup at at initialization time.