Commit bc9d9e8c authored by Mirek Klimos's avatar Mirek Klimos Committed by Facebook Github Bot 5

Reverted commit D3156698

Summary:There're currently two ways to set RequestContext
- RequestContext::create() - creates new context and sets it
- RequestContext::setContext(context) - sets context previously captured by saveContext
In most cases, the RequestContext is set back after the request is processed but sometimes it's not (especially with RequestContext::create). We want to measure cpu time for a request by measuring the total cpu time when a RequestContext is set, so we need to make sure that it's properly reset after the thread is done with the request. Scope guards can help us with that.

Reviewed By: haijunz

Differential Revision: D3156698

fb-gh-sync-id: 7c3eb06c1cc27849071625011bf64c5ad36c0612
fbshipit-source-id: 7c3eb06c1cc27849071625011bf64c5ad36c0612
parent aa088ebe
...@@ -111,15 +111,12 @@ class RequestContext { ...@@ -111,15 +111,12 @@ class RequestContext {
} }
// The following API is used to pass the context through queues / threads. // The following API is used to pass the context through queues / threads.
// saveContext is called to get a shared_ptr to the context, and // saveContext is called to geta shared_ptr to the context, and
// setContext is used to reset it on the other side of the queue. // setContext is used to reset it on the other side of the queue.
// //
// Whenever possible, use RequestContextScopeGuard instead of setContext
// to make sure that RequestContext is reset to the original value when
// we exit the scope.
//
// A shared_ptr is used, because many request may fan out across // A shared_ptr is used, because many request may fan out across
// multiple threads, or do post-send processing, etc. // multiple threads, or do post-send processing, etc.
static std::shared_ptr<RequestContext> static std::shared_ptr<RequestContext>
setContext(std::shared_ptr<RequestContext> ctx) { setContext(std::shared_ptr<RequestContext> ctx) {
using std::swap; using std::swap;
...@@ -138,24 +135,4 @@ class RequestContext { ...@@ -138,24 +135,4 @@ class RequestContext {
std::map<std::string, std::unique_ptr<RequestData>> data_; std::map<std::string, std::unique_ptr<RequestData>> data_;
}; };
class RequestContextScopeGuard {
private:
std::shared_ptr<RequestContext> prev_;
public:
// Create a new RequestContext and reset to the original value when
// this goes out of scope.
RequestContextScopeGuard() : prev_(RequestContext::saveContext()) {
RequestContext::create();
}
// Set a RequestContext that was previously captured by saveContext(). It will
// be automatically reset to the original value when this goes out of scope.
explicit RequestContextScopeGuard(std::shared_ptr<RequestContext> ctx)
: prev_(RequestContext::setContext(std::move(ctx))) {}
~RequestContextScopeGuard() {
RequestContext::setContext(std::move(prev_));
}
};
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment