KeepaliveRequestTracker.cpp [plain text]
#include "config.h"
#include "KeepaliveRequestTracker.h"
namespace WebCore {
const uint64_t maxInflightKeepaliveBytes { 65536 };
KeepaliveRequestTracker::~KeepaliveRequestTracker()
{
auto inflightRequests = WTFMove(m_inflightKeepaliveRequests);
for (auto& resource : inflightRequests)
resource->removeClient(*this);
}
bool KeepaliveRequestTracker::tryRegisterRequest(CachedResource& resource)
{
ASSERT(resource.options().keepAlive);
auto* body = resource.resourceRequest().httpBody();
if (!body)
return true;
uint64_t bodySize = body->lengthInBytes();
if (m_inflightKeepaliveBytes + bodySize > maxInflightKeepaliveBytes)
return false;
registerRequest(resource);
return true;
}
void KeepaliveRequestTracker::registerRequest(CachedResource& resource)
{
ASSERT(resource.options().keepAlive);
auto* body = resource.resourceRequest().httpBody();
if (!body)
return;
ASSERT(!m_inflightKeepaliveRequests.contains(&resource));
m_inflightKeepaliveRequests.append(&resource);
m_inflightKeepaliveBytes += body->lengthInBytes();
ASSERT(m_inflightKeepaliveBytes <= maxInflightKeepaliveBytes);
resource.addClient(*this);
}
void KeepaliveRequestTracker::responseReceived(CachedResource& resource, const ResourceResponse&, CompletionHandler<void()>&& completionHandler)
{
unregisterRequest(resource);
if (completionHandler)
completionHandler();
}
void KeepaliveRequestTracker::notifyFinished(CachedResource& resource, const NetworkLoadMetrics&)
{
unregisterRequest(resource);
}
void KeepaliveRequestTracker::unregisterRequest(CachedResource& resource)
{
ASSERT(resource.options().keepAlive);
resource.removeClient(*this);
bool wasRemoved = m_inflightKeepaliveRequests.removeFirst(&resource);
ASSERT_UNUSED(wasRemoved, wasRemoved);
m_inflightKeepaliveBytes -= resource.resourceRequest().httpBody()->lengthInBytes();
ASSERT(m_inflightKeepaliveBytes <= maxInflightKeepaliveBytes);
}
}