Commit 140e8ccd authored by Kino Li's avatar Kino Li Committed by Facebook GitHub Bot

Revert D31006847: Add RequestContext::try_get()

Differential Revision:
D31006847 (https://github.com/facebook/folly/commit/865e847605911ed0eab96a0170fc372d09c3de5c)

Original commit changeset: 2ce67a097c07

fbshipit-source-id: 1bd09fcd6233f8c88969856c47317fbe8c197754
parent 865e8476
......@@ -608,15 +608,10 @@ void RequestContext::clearContextData(const RequestToken& val) {
return prevCtx;
}
/* static */ RequestContext::StaticContext& RequestContext::getStaticContext() {
RequestContext::StaticContext& RequestContext::getStaticContext() {
return StaticContextThreadLocal::get();
}
/* static */ RequestContext::StaticContext*
RequestContext::tryGetStaticContext() {
return StaticContextThreadLocal::try_get();
}
/* static */ RequestContext::StaticContextAccessor
RequestContext::accessAllThreads() {
return StaticContextAccessor{StaticContextThreadLocal::accessAllThreads()};
......@@ -646,7 +641,7 @@ RequestContext::setShallowCopyContext() {
return child;
}
/* static */ RequestContext* RequestContext::get() {
RequestContext* RequestContext::get() {
auto& context = getStaticContext().requestContext;
if (!context) {
static RequestContext defaultContext(0);
......@@ -654,12 +649,4 @@ RequestContext::setShallowCopyContext() {
}
return context.get();
}
/* static */ RequestContext* RequestContext::try_get() {
if (auto* staticContext = tryGetStaticContext()) {
return staticContext->requestContext.get();
}
return nullptr;
}
} // namespace folly
......@@ -154,9 +154,6 @@ class RequestContext {
// Get the current context.
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_; }
struct RootIdInfo {
......@@ -255,7 +252,6 @@ class RequestContext {
private:
static StaticContext& getStaticContext();
static StaticContext* tryGetStaticContext();
static std::shared_ptr<RequestContext> setContextHelper(
std::shared_ptr<RequestContext>& newCtx, StaticContext& staticCtx);
......
......@@ -566,22 +566,3 @@ TEST_F(RequestContextTest, AccessAllThreadsDestructionGuard) {
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