ffce2ed76c1fe9572639e205b422cdc6a37720c7
[folly.git] / folly / io / async / Request.h
1 /*
2  * Copyright 2014 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/ThreadLocal.h>
27 #include <folly/RWSpinLock.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() {}
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 extern RequestContext* defaultContext;
45
46 class RequestContext {
47  public:
48   // Create a unique requext context for this request.
49   // It will be passed between queues / threads (where implemented),
50   // so it should be valid for the lifetime of the request.
51   static void create() {
52     getStaticContext() = std::make_shared<RequestContext>();
53   }
54
55   // Get the current context.
56   static RequestContext* get() {
57     if (getStaticContext() == nullptr) {
58       if (defaultContext == nullptr) {
59         defaultContext = new RequestContext;
60       }
61       return defaultContext;
62     }
63     return getStaticContext().get();
64   }
65
66   // The following API may be used to set per-request data in a thread-safe way.
67   // This access is still performance sensitive, so please ask if you need help
68   // profiling any use of these functions.
69   void setContextData(
70     const std::string& val, std::unique_ptr<RequestData> data) {
71     folly::RWSpinLock::WriteHolder guard(lock);
72     if (data_.find(val) != data_.end()) {
73       LOG_FIRST_N(WARNING, 1) <<
74         "Called RequestContext::setContextData with data already set";
75
76       data_[val] = nullptr;
77     } else {
78       data_[val] = std::move(data);
79     }
80   }
81
82   bool hasContextData(const std::string& val) {
83     folly::RWSpinLock::ReadHolder guard(lock);
84     return data_.find(val) != data_.end();
85   }
86
87   RequestData* getContextData(const std::string& val) {
88     folly::RWSpinLock::ReadHolder guard(lock);
89     auto r = data_.find(val);
90     if (r == data_.end()) {
91       return nullptr;
92     } else {
93       return r->second.get();
94     }
95   }
96
97   void clearContextData(const std::string& val) {
98     folly::RWSpinLock::WriteHolder guard(lock);
99     data_.erase(val);
100   }
101
102   // The following API is used to pass the context through queues / threads.
103   // saveContext is called to geta shared_ptr to the context, and
104   // setContext is used to reset it on the other side of the queue.
105   //
106   // A shared_ptr is used, because many request may fan out across
107   // multiple threads, or do post-send processing, etc.
108
109   static std::shared_ptr<RequestContext>
110   setContext(std::shared_ptr<RequestContext> ctx) {
111     std::shared_ptr<RequestContext> old_ctx;
112     if (getStaticContext()) {
113       old_ctx = getStaticContext();
114     }
115     getStaticContext() = ctx;
116     return old_ctx;
117   }
118
119   static std::shared_ptr<RequestContext> saveContext() {
120     return getStaticContext();
121   }
122
123   // Used to solve static destruction ordering issue.  Any static object
124   // that uses RequestContext must call this function in its constructor.
125   //
126   // See below link for more details.
127   // http://stackoverflow.com/questions/335369/
128   // finding-c-static-initialization-order-problems#335746
129   static std::shared_ptr<RequestContext>&
130   getStaticContext() {
131     static folly::ThreadLocal<std::shared_ptr<RequestContext> > context;
132     return *context;
133   }
134
135  private:
136   folly::RWSpinLock lock;
137   std::map<std::string, std::unique_ptr<RequestData>> data_;
138 };
139
140 }