2 * Copyright 2015 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>
31 // Some request context that follows an async request through a process
32 // Everything in the context must be thread safe
36 virtual ~RequestData() = default;
41 // If you do not call create() to create a unique request context,
42 // this default request context will always be returned, and is never
43 // copied between threads.
44 class RequestContext {
46 // Create a unique requext context for this request.
47 // It will be passed between queues / threads (where implemented),
48 // so it should be valid for the lifetime of the request.
49 static void create() {
50 getStaticContext() = std::make_shared<RequestContext>();
53 // Get the current context.
54 static RequestContext* get() {
55 auto context = getStaticContext();
57 static RequestContext defaultContext;
58 return std::addressof(defaultContext);
63 // The following API may be used to set per-request data in a thread-safe way.
64 // This access is still performance sensitive, so please ask if you need help
65 // profiling any use of these functions.
67 const std::string& val, std::unique_ptr<RequestData> data) {
68 folly::RWSpinLock::WriteHolder guard(lock);
69 if (data_.find(val) != data_.end()) {
70 LOG_FIRST_N(WARNING, 1) <<
71 "Called RequestContext::setContextData with data already set";
75 data_[val] = std::move(data);
79 bool hasContextData(const std::string& val) {
80 folly::RWSpinLock::ReadHolder guard(lock);
81 return data_.find(val) != data_.end();
84 RequestData* getContextData(const std::string& val) {
85 folly::RWSpinLock::ReadHolder guard(lock);
86 auto r = data_.find(val);
87 if (r == data_.end()) {
90 return r->second.get();
94 void clearContextData(const std::string& val) {
95 folly::RWSpinLock::WriteHolder guard(lock);
99 // The following API is used to pass the context through queues / threads.
100 // saveContext is called to geta shared_ptr to the context, and
101 // setContext is used to reset it on the other side of the queue.
103 // A shared_ptr is used, because many request may fan out across
104 // multiple threads, or do post-send processing, etc.
106 static std::shared_ptr<RequestContext>
107 setContext(std::shared_ptr<RequestContext> ctx) {
109 swap(ctx, getStaticContext());
113 static std::shared_ptr<RequestContext> saveContext() {
114 return getStaticContext();
118 // Used to solve static destruction ordering issue. Any static object
119 // that uses RequestContext must call this function in its constructor.
121 // See below link for more details.
122 // http://stackoverflow.com/questions/335369/
123 // finding-c-static-initialization-order-problems#335746
124 static std::shared_ptr<RequestContext> &getStaticContext() {
125 static folly::ThreadLocal<std::shared_ptr<RequestContext> > context;
129 folly::RWSpinLock lock;
130 std::map<std::string, std::unique_ptr<RequestData>> data_;