Commit 32ea1ed5 authored by Amlan Nayak's avatar Amlan Nayak Committed by Facebook GitHub Bot

Add RequestContext::try_get() (re-do of D31006847)

Summary:
Returns the RequestContext object if it already exists. Uses
SingletonThreadLocal::try_get() underneath.

Reviewed By: mshneer

Differential Revision: D31046069

fbshipit-source-id: 0d1b7af870494b70d890d0b8ef9cf0b89ac67ccc
parent 78a00dec
...@@ -608,10 +608,15 @@ void RequestContext::clearContextData(const RequestToken& val) { ...@@ -608,10 +608,15 @@ void RequestContext::clearContextData(const RequestToken& val) {
return prevCtx; return prevCtx;
} }
RequestContext::StaticContext& RequestContext::getStaticContext() { /* static */ RequestContext::StaticContext& RequestContext::getStaticContext() {
return StaticContextThreadLocal::get(); return StaticContextThreadLocal::get();
} }
/* static */ RequestContext::StaticContext*
RequestContext::tryGetStaticContext() {
return StaticContextThreadLocal::try_get();
}
/* static */ RequestContext::StaticContextAccessor /* static */ RequestContext::StaticContextAccessor
RequestContext::accessAllThreads() { RequestContext::accessAllThreads() {
return StaticContextAccessor{StaticContextThreadLocal::accessAllThreads()}; return StaticContextAccessor{StaticContextThreadLocal::accessAllThreads()};
...@@ -641,7 +646,7 @@ RequestContext::setShallowCopyContext() { ...@@ -641,7 +646,7 @@ RequestContext::setShallowCopyContext() {
return child; return child;
} }
RequestContext* RequestContext::get() { /* static */ RequestContext* RequestContext::get() {
auto& context = getStaticContext().requestContext; auto& context = getStaticContext().requestContext;
if (!context) { if (!context) {
static RequestContext defaultContext(0); static RequestContext defaultContext(0);
...@@ -649,4 +654,12 @@ RequestContext* RequestContext::get() { ...@@ -649,4 +654,12 @@ RequestContext* RequestContext::get() {
} }
return context.get(); return context.get();
} }
/* static */ RequestContext* RequestContext::try_get() {
if (auto* staticContext = tryGetStaticContext()) {
return staticContext->requestContext.get();
}
return nullptr;
}
} // namespace folly } // namespace folly
...@@ -154,6 +154,9 @@ class RequestContext { ...@@ -154,6 +154,9 @@ class RequestContext {
// Get the current context. // Get the current context.
static RequestContext* get(); static RequestContext* get();
// Get the current context, if it has already been created, or nullptr.
static RequestContext* try_get();
intptr_t getRootId() const { return rootId_; } intptr_t getRootId() const { return rootId_; }
struct RootIdInfo { struct RootIdInfo {
...@@ -252,6 +255,7 @@ class RequestContext { ...@@ -252,6 +255,7 @@ class RequestContext {
private: private:
static StaticContext& getStaticContext(); static StaticContext& getStaticContext();
static StaticContext* tryGetStaticContext();
static std::shared_ptr<RequestContext> setContextHelper( static std::shared_ptr<RequestContext> setContextHelper(
std::shared_ptr<RequestContext>& newCtx, StaticContext& staticCtx); std::shared_ptr<RequestContext>& newCtx, StaticContext& staticCtx);
......
...@@ -566,3 +566,22 @@ TEST_F(RequestContextTest, AccessAllThreadsDestructionGuard) { ...@@ -566,3 +566,22 @@ TEST_F(RequestContextTest, AccessAllThreadsDestructionGuard) {
thread.join(); thread.join();
} }
} }
TEST(RequestContextTryGetTest, TryGetTest) {
// try_get() should not create a default RequestContext object if none exists.
EXPECT_EQ(RequestContext::try_get(), nullptr);
// Explicitly create a new instance so that subsequent calls to try_get()
// return it.
RequestContext::create();
EXPECT_NE(RequestContext::saveContext(), nullptr);
EXPECT_NE(RequestContext::try_get(), nullptr);
// Make sure that the pointers returned by both get() and try_get() point to
// the same underlying instance.
EXPECT_EQ(RequestContext::try_get(), RequestContext::get());
// Set some context data and read it out via try_get() accessor.
RequestContext::get()->setContextData("test", std::make_unique<TestData>(10));
auto rc = RequestContext::try_get();
EXPECT_TRUE(rc->hasContextData("test"));
auto* dataPtr = dynamic_cast<TestData*>(rc->getContextData("test"));
EXPECT_EQ(dataPtr->data_, 10);
}
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