Move RequestContext definitions to source files
[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 <folly/RWSpinLock.h>
26
27 namespace folly {
28
29 // Some request context that follows an async request through a process
30 // Everything in the context must be thread safe
31
32 class RequestData {
33  public:
34   virtual ~RequestData() = default;
35   virtual void onSet() {}
36   virtual void onUnset() {}
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 request 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,
68       std::unique_ptr<RequestData> data);
69
70   // Unlike setContextData, this method does not panic if the key is already
71   // present. Returns true iff the new value has been inserted.
72   bool setContextDataIfAbsent(
73       const std::string& val,
74       std::unique_ptr<RequestData> data);
75
76   bool hasContextData(const std::string& val) const;
77
78   RequestData* getContextData(const std::string& val);
79   const RequestData* getContextData(const std::string& val) const;
80
81   void onSet();
82   void onUnset();
83
84   void clearContextData(const std::string& val);
85
86   // The following API is used to pass the context through queues / threads.
87   // saveContext is called to get a shared_ptr to the context, and
88   // setContext is used to reset it on the other side of the queue.
89   //
90   // Whenever possible, use RequestContextScopeGuard instead of setContext
91   // to make sure that RequestContext is reset to the original value when
92   // we exit the scope.
93   //
94   // A shared_ptr is used, because many request may fan out across
95   // multiple threads, or do post-send processing, etc.
96   static std::shared_ptr<RequestContext> setContext(
97       std::shared_ptr<RequestContext> ctx);
98
99   static std::shared_ptr<RequestContext> saveContext() {
100     return getStaticContext();
101   }
102
103  private:
104   static std::shared_ptr<RequestContext>& getStaticContext();
105
106   mutable folly::RWSpinLock lock;
107   std::map<std::string, std::unique_ptr<RequestData>> data_;
108 };
109
110 class RequestContextScopeGuard {
111  private:
112   std::shared_ptr<RequestContext> prev_;
113
114  public:
115   // Create a new RequestContext and reset to the original value when
116   // this goes out of scope.
117   RequestContextScopeGuard() : prev_(RequestContext::saveContext()) {
118     RequestContext::create();
119   }
120
121   // Set a RequestContext that was previously captured by saveContext(). It will
122   // be automatically reset to the original value when this goes out of scope.
123   explicit RequestContextScopeGuard(std::shared_ptr<RequestContext> ctx)
124       : prev_(RequestContext::setContext(std::move(ctx))) {}
125
126   ~RequestContextScopeGuard() {
127     RequestContext::setContext(std::move(prev_));
128   }
129 };
130 }