class RequestData {
public:
virtual ~RequestData() = default;
+ virtual void onSet(){};
+ virtual void onUnset(){};
};
class RequestContext;
// copied between threads.
class RequestContext {
public:
- // Create a unique requext context for this request.
+ // Create a unique request context for this request.
// It will be passed between queues / threads (where implemented),
// so it should be valid for the lifetime of the request.
static void create() {
}
}
+ void onSet() {
+ folly::RWSpinLock::ReadHolder guard(lock);
+ for (auto const& ent : data_) {
+ if (RequestData* data = ent.second.get()) {
+ data->onSet();
+ }
+ }
+ }
+
+ void onUnset() {
+ folly::RWSpinLock::ReadHolder guard(lock);
+ for (auto const& ent : data_) {
+ if (RequestData* data = ent.second.get()) {
+ data->onUnset();
+ }
+ }
+ }
+
void clearContextData(const std::string& val) {
folly::RWSpinLock::WriteHolder guard(lock);
data_.erase(val);
// multiple threads, or do post-send processing, etc.
static std::shared_ptr<RequestContext>
setContext(std::shared_ptr<RequestContext> ctx) {
- using std::swap;
- swap(ctx, getStaticContext());
+ auto& prev = getStaticContext();
+ if (ctx != prev) {
+ using std::swap;
+ if (ctx) {
+ ctx->onSet();
+ }
+ if (prev) {
+ prev->onUnset();
+ }
+ swap(ctx, prev);
+ }
return ctx;
}