2 * Copyright 2016 Facebook, Inc.
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
25 #include <glog/logging.h>
26 #include <folly/ThreadLocal.h>
27 #include <folly/RWSpinLock.h>
28 #include <folly/SingletonThreadLocal.h>
32 // Some request context that follows an async request through a process
33 // Everything in the context must be thread safe
37 virtual ~RequestData() = default;
42 // If you do not call create() to create a unique request context,
43 // this default request context will always be returned, and is never
44 // copied between threads.
45 class RequestContext {
47 // Create a unique requext context for this request.
48 // It will be passed between queues / threads (where implemented),
49 // so it should be valid for the lifetime of the request.
50 static void create() {
51 getStaticContext() = std::make_shared<RequestContext>();
54 // Get the current context.
55 static RequestContext* get() {
56 auto context = getStaticContext();
58 static RequestContext defaultContext;
59 return std::addressof(defaultContext);
64 // The following API may be used to set per-request data in a thread-safe way.
65 // This access is still performance sensitive, so please ask if you need help
66 // profiling any use of these functions.
68 const std::string& val, std::unique_ptr<RequestData> data) {
69 folly::RWSpinLock::WriteHolder guard(lock);
70 if (data_.find(val) != data_.end()) {
71 LOG_FIRST_N(WARNING, 1) <<
72 "Called RequestContext::setContextData with data already set";
76 data_[val] = std::move(data);
80 // Unlike setContextData, this method does not panic if the key is already
81 // present. Returns true iff the new value has been inserted.
82 bool setContextDataIfAbsent(const std::string& val,
83 std::unique_ptr<RequestData> data) {
84 folly::RWSpinLock::UpgradedHolder guard(lock);
85 if (data_.find(val) != data_.end()) {
89 folly::RWSpinLock::WriteHolder writeGuard(std::move(guard));
90 data_[val] = std::move(data);
94 bool hasContextData(const std::string& val) {
95 folly::RWSpinLock::ReadHolder guard(lock);
96 return data_.find(val) != data_.end();
99 RequestData* getContextData(const std::string& val) {
100 folly::RWSpinLock::ReadHolder guard(lock);
101 auto r = data_.find(val);
102 if (r == data_.end()) {
105 return r->second.get();
109 void clearContextData(const std::string& val) {
110 folly::RWSpinLock::WriteHolder guard(lock);
114 // The following API is used to pass the context through queues / threads.
115 // saveContext is called to geta shared_ptr to the context, and
116 // setContext is used to reset it on the other side of the queue.
118 // A shared_ptr is used, because many request may fan out across
119 // multiple threads, or do post-send processing, etc.
121 static std::shared_ptr<RequestContext>
122 setContext(std::shared_ptr<RequestContext> ctx) {
124 swap(ctx, getStaticContext());
128 static std::shared_ptr<RequestContext> saveContext() {
129 return getStaticContext();
133 static std::shared_ptr<RequestContext>& getStaticContext() {
134 using SingletonT = SingletonThreadLocal<std::shared_ptr<RequestContext>>;
135 static SingletonT singleton;
136 return singleton.get();
139 folly::RWSpinLock lock;
140 std::map<std::string, std::unique_ptr<RequestData>> data_;