nghttpx: Use ngtcp2_cid as a hash key

This commit is contained in:
Tatsuhiro Tsujikawa 2021-09-05 18:53:31 +09:00
parent b743ee21f0
commit 67afbbbaa6
4 changed files with 35 additions and 22 deletions

View File

@ -45,6 +45,10 @@
using namespace nghttp2;
bool operator==(const ngtcp2_cid &lhs, const ngtcp2_cid &rhs) {
return ngtcp2_cid_eq(&lhs, &rhs);
}
namespace shrpx {
ngtcp2_tstamp quic_timestamp() {

View File

@ -29,8 +29,30 @@
#include <stdint.h>
#include <functional>
#include <ngtcp2/ngtcp2.h>
namespace std {
template <> struct hash<ngtcp2_cid> {
std::size_t operator()(const ngtcp2_cid &cid) const noexcept {
// FNV-1a 64bits variant
constexpr uint64_t basis = 0xCBF29CE484222325ULL;
const uint8_t *p = cid.data, *end = cid.data + cid.datalen;
uint64_t h = basis;
for (; p != end;) {
h ^= *p++;
h *= basis;
}
return static_cast<size_t>(h);
}
};
} // namespace std
bool operator==(const ngtcp2_cid &lhs, const ngtcp2_cid &rhs);
namespace shrpx {
struct UpstreamAddr;

View File

@ -32,7 +32,6 @@
#include "shrpx_worker.h"
#include "shrpx_client_handler.h"
#include "shrpx_log.h"
#include "shrpx_quic.h"
#include "shrpx_http3_upstream.h"
#include "shrpx_connection_handler.h"
@ -43,18 +42,6 @@ QUICConnectionHandler::QUICConnectionHandler(Worker *worker)
QUICConnectionHandler::~QUICConnectionHandler() {}
namespace {
std::string make_cid_key(const uint8_t *dcid, size_t dcidlen) {
return std::string{dcid, dcid + dcidlen};
}
} // namespace
namespace {
std::string make_cid_key(const ngtcp2_cid *cid) {
return make_cid_key(cid->data, cid->datalen);
}
} // namespace
int QUICConnectionHandler::handle_packet(const UpstreamAddr *faddr,
const Address &remote_addr,
const Address &local_addr,
@ -80,7 +67,8 @@ int QUICConnectionHandler::handle_packet(const UpstreamAddr *faddr,
auto config = get_config();
auto dcid_key = make_cid_key(dcid, dcidlen);
ngtcp2_cid dcid_key;
ngtcp2_cid_init(&dcid_key, dcid, dcidlen);
auto conn_handler = worker_->get_connection_handler();
@ -462,24 +450,22 @@ int QUICConnectionHandler::send_connection_close(
void QUICConnectionHandler::add_connection_id(const ngtcp2_cid *cid,
ClientHandler *handler) {
auto key = make_cid_key(cid);
connections_.emplace(key, handler);
connections_.emplace(*cid, handler);
}
void QUICConnectionHandler::remove_connection_id(const ngtcp2_cid *cid) {
auto key = make_cid_key(cid);
connections_.erase(key);
connections_.erase(*cid);
}
void QUICConnectionHandler::add_close_wait(CloseWait *cw) {
for (auto &cid : cw->scids) {
close_waits_.emplace(make_cid_key(&cid), cw);
close_waits_.emplace(cid, cw);
}
}
void QUICConnectionHandler::remove_close_wait(const CloseWait *cw) {
for (auto &cid : cw->scids) {
close_waits_.erase(make_cid_key(&cid));
close_waits_.erase(cid);
}
}

View File

@ -36,6 +36,7 @@
#include <ev.h>
#include "shrpx_quic.h"
#include "network.h"
using namespace nghttp2;
@ -127,8 +128,8 @@ public:
private:
Worker *worker_;
std::unordered_map<std::string, ClientHandler *> connections_;
std::unordered_map<std::string, CloseWait *> close_waits_;
std::unordered_map<ngtcp2_cid, ClientHandler *> connections_;
std::unordered_map<ngtcp2_cid, CloseWait *> close_waits_;
};
} // namespace shrpx