Imported Upstream version 1.0.0
[platform/upstream/nghttp2.git] / src / shrpx_downstream_queue.h
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2012 Tatsuhiro Tsujikawa
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 #ifndef SHRPX_DOWNSTREAM_QUEUE_H
26 #define SHRPX_DOWNSTREAM_QUEUE_H
27
28 #include "shrpx.h"
29
30 #include <cinttypes>
31 #include <map>
32 #include <set>
33 #include <memory>
34
35 #include "template.h"
36
37 using namespace nghttp2;
38
39 namespace shrpx {
40
41 class Downstream;
42
43 // Link entry in HostEntry.blocked and downstream because downstream
44 // could be deleted in anytime and we'd like to find Downstream in
45 // O(1).  Downstream has field to link back to this object.
46 struct BlockedLink {
47   Downstream *downstream;
48   BlockedLink *dlnext, *dlprev;
49 };
50
51 class DownstreamQueue {
52 public:
53   struct HostEntry {
54     // Set of stream ID that blocked by conn_max_per_host_.
55     DList<BlockedLink> blocked;
56     // The number of connections currently made to this host.
57     size_t num_active;
58     HostEntry();
59   };
60
61   typedef std::map<std::string, HostEntry> HostEntryMap;
62
63   // conn_max_per_host == 0 means no limit for downstream connection.
64   DownstreamQueue(size_t conn_max_per_host = 0, bool unified_host = true);
65   ~DownstreamQueue();
66   // Add |downstream| to this queue.  This is entry point for
67   // Downstream object.
68   void add_pending(std::unique_ptr<Downstream> downstream);
69   // Set |downstream| to failure state, which means that downstream
70   // failed to connect to backend.
71   void mark_failure(Downstream *downstream);
72   // Set |downstream| to active state, which means that downstream
73   // connection has started.
74   void mark_active(Downstream *downstream);
75   // Set |downstream| to blocked state, which means that download
76   // connection was blocked because conn_max_per_host_ limit.
77   void mark_blocked(Downstream *downstream);
78   // Returns true if we can make downstream connection to given
79   // |host|.
80   bool can_activate(const std::string &host) const;
81   // Removes and frees |downstream| object.  If |downstream| is in
82   // Downstream::DISPATCH_ACTIVE, this function may return Downstream
83   // object with the same target host in Downstream::DISPATCH_BLOCKED
84   // if its connection is now not blocked by conn_max_per_host_ limit.
85   Downstream *remove_and_get_blocked(Downstream *downstream);
86   Downstream *get_downstreams() const;
87   HostEntry &find_host_entry(const std::string &host);
88   const std::string &make_host_key(const std::string &host) const;
89   const std::string &make_host_key(Downstream *downstream) const;
90
91 private:
92   // Per target host structure to keep track of the number of
93   // connections to the same host.
94   std::map<std::string, HostEntry> host_entries_;
95   DList<Downstream> downstreams_;
96   // Maximum number of concurrent connections to the same host.
97   size_t conn_max_per_host_;
98   // true if downstream host is treated as the same.  Used for reverse
99   // proxying.
100   bool unified_host_;
101 };
102
103 } // namespace shrpx
104
105 #endif // SHRPX_DOWNSTREAM_QUEUE_H