2012-06-04 16:48:31 +02:00
|
|
|
/*
|
2014-03-30 12:09:21 +02:00
|
|
|
* nghttp2 - HTTP/2 C Library
|
2012-06-04 16:48:31 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#ifndef SHRPX_DOWNSTREAM_QUEUE_H
|
|
|
|
#define SHRPX_DOWNSTREAM_QUEUE_H
|
|
|
|
|
|
|
|
#include "shrpx.h"
|
|
|
|
|
2015-05-13 17:17:45 +02:00
|
|
|
#include <cinttypes>
|
2012-06-04 16:48:31 +02:00
|
|
|
#include <map>
|
2014-12-04 17:07:00 +01:00
|
|
|
#include <set>
|
2014-08-18 15:59:31 +02:00
|
|
|
#include <memory>
|
2012-06-04 16:48:31 +02:00
|
|
|
|
2015-03-11 16:17:05 +01:00
|
|
|
#include "template.h"
|
|
|
|
|
|
|
|
using namespace nghttp2;
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
namespace shrpx {
|
|
|
|
|
|
|
|
class Downstream;
|
|
|
|
|
2015-03-11 16:17:05 +01:00
|
|
|
// Link entry in HostEntry.blocked and downstream because downstream
|
|
|
|
// could be deleted in anytime and we'd like to find Downstream in
|
|
|
|
// O(1). Downstream has field to link back to this object.
|
|
|
|
struct BlockedLink {
|
|
|
|
Downstream *downstream;
|
|
|
|
BlockedLink *dlnext, *dlprev;
|
|
|
|
};
|
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
class DownstreamQueue {
|
|
|
|
public:
|
2014-12-04 17:07:00 +01:00
|
|
|
struct HostEntry {
|
|
|
|
// Set of stream ID that blocked by conn_max_per_host_.
|
2015-03-11 16:17:05 +01:00
|
|
|
DList<BlockedLink> blocked;
|
2014-12-04 17:07:00 +01:00
|
|
|
// The number of connections currently made to this host.
|
|
|
|
size_t num_active;
|
|
|
|
HostEntry();
|
|
|
|
};
|
|
|
|
|
2015-06-21 12:37:50 +02:00
|
|
|
using HostEntryMap = std::map<std::string, HostEntry>;
|
2014-12-04 17:07:00 +01:00
|
|
|
|
|
|
|
// conn_max_per_host == 0 means no limit for downstream connection.
|
2015-01-02 04:53:27 +01:00
|
|
|
DownstreamQueue(size_t conn_max_per_host = 0, bool unified_host = true);
|
2012-06-04 16:48:31 +02:00
|
|
|
~DownstreamQueue();
|
2015-03-11 16:17:05 +01:00
|
|
|
// Add |downstream| to this queue. This is entry point for
|
|
|
|
// Downstream object.
|
2014-08-18 15:59:31 +02:00
|
|
|
void add_pending(std::unique_ptr<Downstream> downstream);
|
2015-03-11 16:17:05 +01:00
|
|
|
// Set |downstream| to failure state, which means that downstream
|
|
|
|
// failed to connect to backend.
|
|
|
|
void mark_failure(Downstream *downstream);
|
|
|
|
// Set |downstream| to active state, which means that downstream
|
|
|
|
// connection has started.
|
|
|
|
void mark_active(Downstream *downstream);
|
|
|
|
// Set |downstream| to blocked state, which means that download
|
|
|
|
// connection was blocked because conn_max_per_host_ limit.
|
|
|
|
void mark_blocked(Downstream *downstream);
|
2014-12-04 17:07:00 +01:00
|
|
|
// Returns true if we can make downstream connection to given
|
|
|
|
// |host|.
|
2016-03-10 14:42:07 +01:00
|
|
|
bool can_activate(const StringRef &host) const;
|
2015-03-11 16:17:05 +01:00
|
|
|
// Removes and frees |downstream| object. If |downstream| is in
|
2015-11-15 16:12:54 +01:00
|
|
|
// Downstream::DISPATCH_ACTIVE, and |next_blocked| is true, this
|
|
|
|
// function may return Downstream object with the same target host
|
|
|
|
// in Downstream::DISPATCH_BLOCKED if its connection is now not
|
|
|
|
// blocked by conn_max_per_host_ limit.
|
|
|
|
Downstream *remove_and_get_blocked(Downstream *downstream,
|
|
|
|
bool next_blocked = true);
|
2015-03-11 16:17:05 +01:00
|
|
|
Downstream *get_downstreams() const;
|
2014-12-04 17:07:00 +01:00
|
|
|
HostEntry &find_host_entry(const std::string &host);
|
2016-03-10 14:42:07 +01:00
|
|
|
std::string make_host_key(const StringRef &host) const;
|
|
|
|
std::string make_host_key(Downstream *downstream) const;
|
2014-12-04 17:07:00 +01:00
|
|
|
|
2012-06-04 16:48:31 +02:00
|
|
|
private:
|
2014-12-04 17:07:00 +01:00
|
|
|
// Per target host structure to keep track of the number of
|
|
|
|
// connections to the same host.
|
|
|
|
std::map<std::string, HostEntry> host_entries_;
|
2015-03-11 16:17:05 +01:00
|
|
|
DList<Downstream> downstreams_;
|
|
|
|
// Maximum number of concurrent connections to the same host.
|
|
|
|
size_t conn_max_per_host_;
|
2015-01-02 04:53:27 +01:00
|
|
|
// true if downstream host is treated as the same. Used for reverse
|
|
|
|
// proxying.
|
|
|
|
bool unified_host_;
|
2012-06-04 16:48:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace shrpx
|
|
|
|
|
|
|
|
#endif // SHRPX_DOWNSTREAM_QUEUE_H
|