Reverted commit D3156698
[folly.git] / folly / io / async / Request.h
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
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
11  *
12  *   http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21 #pragma once
22
23 #include <map>
24 #include <memory>
25 #include <glog/logging.h>
26 #include <folly/RWSpinLock.h>
27 #include <folly/SingletonThreadLocal.h>
28
29 namespace folly {
30
31 // Some request context that follows an async request through a process
32 // Everything in the context must be thread safe
33
34 class RequestData {
35  public:
36   virtual ~RequestData() = default;
37 };
38
39 class RequestContext;
40
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 {
45  public:
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>();
51   }
52
53   // Get the current context.
54   static RequestContext* get() {
55     auto context = getStaticContext();
56     if (!context) {
57       static RequestContext defaultContext;
58       return std::addressof(defaultContext);
59     }
60     return context.get();
61   }
62
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.
66   void setContextData(
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";
72
73       data_[val] = nullptr;
74     } else {
75       data_[val] = std::move(data);
76     }
77   }
78
79   // Unlike setContextData, this method does not panic if the key is already
80   // present. Returns true iff the new value has been inserted.
81   bool setContextDataIfAbsent(const std::string& val,
82                               std::unique_ptr<RequestData> data) {
83     folly::RWSpinLock::UpgradedHolder guard(lock);
84     if (data_.find(val) != data_.end()) {
85       return false;
86     }
87
88     folly::RWSpinLock::WriteHolder writeGuard(std::move(guard));
89     data_[val] = std::move(data);
90     return true;
91   }
92
93   bool hasContextData(const std::string& val) {
94     folly::RWSpinLock::ReadHolder guard(lock);
95     return data_.find(val) != data_.end();
96   }
97
98   RequestData* getContextData(const std::string& val) {
99     folly::RWSpinLock::ReadHolder guard(lock);
100     auto r = data_.find(val);
101     if (r == data_.end()) {
102       return nullptr;
103     } else {
104       return r->second.get();
105     }
106   }
107
108   void clearContextData(const std::string& val) {
109     folly::RWSpinLock::WriteHolder guard(lock);
110     data_.erase(val);
111   }
112
113   // The following API is used to pass the context through queues / threads.
114   // saveContext is called to geta shared_ptr to the context, and
115   // setContext is used to reset it on the other side of the queue.
116   //
117   // A shared_ptr is used, because many request may fan out across
118   // multiple threads, or do post-send processing, etc.
119
120   static std::shared_ptr<RequestContext>
121   setContext(std::shared_ptr<RequestContext> ctx) {
122     using std::swap;
123     swap(ctx, getStaticContext());
124     return ctx;
125   }
126
127   static std::shared_ptr<RequestContext> saveContext() {
128     return getStaticContext();
129   }
130
131  private:
132   static std::shared_ptr<RequestContext>& getStaticContext();
133
134   folly::RWSpinLock lock;
135   std::map<std::string, std::unique_ptr<RequestData>> data_;
136 };
137
138 }