Skip to content

Commit

Permalink
dedup
Browse files Browse the repository at this point in the history
  • Loading branch information
John-LittleBearLabs committed Nov 17, 2023
1 parent 8b70126 commit bd36a2d
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 13 deletions.
17 changes: 10 additions & 7 deletions component/ipfs_url_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ void ipfs::IpfsUrlLoader::StartRequest(
me->root_ = cid_str;
me->api_->SetLoaderFactory(*(me->lower_loader_factory_));
auto whendone = [me](IpfsRequest const& req, ipfs::Response const& res) {
VLOG(1) << "whendone(" << req.path().to_string() << ',' << res.status_
<< ',' << res.body_.size() << "B mime=" << res.mime_ << ')';
LOG(INFO) << "whendone(" << req.path().to_string() << ',' << res.status_
<< ',' << res.body_.size() << "B mime=" << res.mime_ << ')';
if (!res.body_.empty()) {
me->ReceiveBlockBytes(res.body_);
}
Expand Down Expand Up @@ -125,7 +125,9 @@ void ipfs::IpfsUrlLoader::BlocksComplete(std::string mime_type) {
}
complete_ = true;
auto head = network::mojom::URLResponseHead::New();
head->mime_type = mime_type;
if (mime_type.size()) {
head->mime_type = mime_type;
}
std::uint32_t byte_count = partial_block_.size();
VLOG(1) << "Calling WriteData(" << byte_count << ")";
pipe_prod_->WriteData(partial_block_.data(), &byte_count,
Expand All @@ -143,7 +145,9 @@ void ipfs::IpfsUrlLoader::BlocksComplete(std::string mime_type) {
auto status_line = base::StringPrintf("HTTP/1.1 %d %s", status_, reason);
VLOG(1) << "Returning with status line '" << status_line << "'.\n";
head->headers->ReplaceStatusLine(status_line);
head->headers->SetHeader("Content-Type", mime_type);
if (mime_type.size()) {
head->headers->SetHeader("Content-Type", mime_type);
}
head->headers->SetHeader("Access-Control-Allow-Origin", "*");
head->was_fetched_via_spdy = false;
for (auto& [n, v] : additional_outgoing_headers_) {
Expand All @@ -153,9 +157,8 @@ void ipfs::IpfsUrlLoader::BlocksComplete(std::string mime_type) {
VLOG(1) << "Calling PopulateParsedHeaders";
head->parsed_headers =
network::PopulateParsedHeaders(head->headers.get(), GURL{original_url_});
VLOG(1) << "Sending response for " << original_url_ << " with mime type "
<< head->mime_type << " @" << (void*)(this)
;
LOG(INFO) << "Sending response for " << original_url_ << " with mime type "
<< head->mime_type << " and status line " << status_line;
client_->OnReceiveResponse(std::move(head), std::move(pipe_cons_),
absl::nullopt);
client_->OnComplete(network::URLLoaderCompletionStatus{});
Expand Down
4 changes: 3 additions & 1 deletion library/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ project(ipfs_client
DESCRIPTION "Code related to using IPFS http gateways to resolve ipfs:// and ipns:// URLs"
LANGUAGES CXX
)

if(NOT CXX_VERSION)
set(CXX_VERSION 20)
endif()
if(NOT SETUP_CMAKE_INCLUDED)
include(../cmake/setup.cmake)
endif()
Expand Down
1 change: 1 addition & 0 deletions library/include/ipfs_client/context_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct HttpRequestDescription {
std::string accept;
std::optional<std::size_t> max_response_size;
bool operator==(HttpRequestDescription const&) const;
bool operator<(HttpRequestDescription const&) const;
};

/**
Expand Down
4 changes: 4 additions & 0 deletions library/src/ipfs_client/context_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ bool ipfs::HttpRequestDescription::operator==(
HttpRequestDescription const& r) const {
return url == r.url && accept == r.accept;
}
bool ipfs::HttpRequestDescription::operator<(

Check warning on line 9 in library/src/ipfs_client/context_api.cc

View check run for this annotation

Codecov / codecov/patch

library/src/ipfs_client/context_api.cc#L9

Added line #L9 was not covered by tests
HttpRequestDescription const& r) const {
return url == r.url ? accept < r.accept : url < r.url;

Check warning on line 11 in library/src/ipfs_client/context_api.cc

View check run for this annotation

Codecov / codecov/patch

library/src/ipfs_client/context_api.cc#L11

Added line #L11 was not covered by tests
}
41 changes: 41 additions & 0 deletions library/src/ipfs_client/gw/deduplicating_requestor.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "deduplicating_requestor.h"

#include <ipfs_client/gw/gateway_request.h>

#include "log_macros.h"

using Self = ipfs::gw::DeduplicatingRequestor;

std::string_view Self::name() const {
return "dedup";

Check warning on line 10 in library/src/ipfs_client/gw/deduplicating_requestor.cc

View check run for this annotation

Codecov / codecov/patch

library/src/ipfs_client/gw/deduplicating_requestor.cc#L9-L10

Added lines #L9 - L10 were not covered by tests
}
auto Self::handle(ipfs::gw::Requestor::RequestPtr r) -> HandleOutcome {
auto d = r->describe_http();
if (!d.has_value()) {
return HandleOutcome::NOT_HANDLED;

Check warning on line 15 in library/src/ipfs_client/gw/deduplicating_requestor.cc

View check run for this annotation

Codecov / codecov/patch

library/src/ipfs_client/gw/deduplicating_requestor.cc#L12-L15

Added lines #L12 - L15 were not covered by tests
}
auto& k = d.value();
auto it = seen_.find(k);
if (seen_.end() == it) {
seen_.emplace(k, r);
return HandleOutcome::NOT_HANDLED;

Check warning on line 21 in library/src/ipfs_client/gw/deduplicating_requestor.cc

View check run for this annotation

Codecov / codecov/patch

library/src/ipfs_client/gw/deduplicating_requestor.cc#L17-L21

Added lines #L17 - L21 were not covered by tests
}
auto& w = it->second;
auto old = w.lock();
if (old == r) {
LOG(INFO)
<< name()

Check warning on line 27 in library/src/ipfs_client/gw/deduplicating_requestor.cc

View check run for this annotation

Codecov / codecov/patch

library/src/ipfs_client/gw/deduplicating_requestor.cc#L23-L27

Added lines #L23 - L27 were not covered by tests
<< " has seen the EXACT same request pass by (same object in memory).";
return HandleOutcome::NOT_HANDLED;

Check warning on line 29 in library/src/ipfs_client/gw/deduplicating_requestor.cc

View check run for this annotation

Codecov / codecov/patch

library/src/ipfs_client/gw/deduplicating_requestor.cc#L29

Added line #L29 was not covered by tests
}
if (old) {
LOG(INFO) << "Dedup squashed a new version of the request "
<< old->debug_string() << " in " << r->debug_string();
return HandleOutcome::DONE;

Check warning on line 34 in library/src/ipfs_client/gw/deduplicating_requestor.cc

View check run for this annotation

Codecov / codecov/patch

library/src/ipfs_client/gw/deduplicating_requestor.cc#L31-L34

Added lines #L31 - L34 were not covered by tests
} else {
LOG(INFO) << r->debug_string()

Check warning on line 36 in library/src/ipfs_client/gw/deduplicating_requestor.cc

View check run for this annotation

Codecov / codecov/patch

library/src/ipfs_client/gw/deduplicating_requestor.cc#L36

Added line #L36 was not covered by tests
<< " has occurred before, but the old copy is dead and gone.";
it->second = r;
return HandleOutcome::NOT_HANDLED;

Check warning on line 39 in library/src/ipfs_client/gw/deduplicating_requestor.cc

View check run for this annotation

Codecov / codecov/patch

library/src/ipfs_client/gw/deduplicating_requestor.cc#L38-L39

Added lines #L38 - L39 were not covered by tests
}
}

Check warning on line 41 in library/src/ipfs_client/gw/deduplicating_requestor.cc

View check run for this annotation

Codecov / codecov/patch

library/src/ipfs_client/gw/deduplicating_requestor.cc#L41

Added line #L41 was not covered by tests
19 changes: 19 additions & 0 deletions library/src/ipfs_client/gw/deduplicating_requestor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef IPFS_DEDUPLICATING_REQUESTOR_H_
#define IPFS_DEDUPLICATING_REQUESTOR_H_

#include <ipfs_client/gw/requestor.h>

#include <ipfs_client/context_api.h>

#include <map>

namespace ipfs::gw {
class DeduplicatingRequestor final : public Requestor {
std::string_view name() const override;
HandleOutcome handle(RequestPtr) override;

std::map<HttpRequestDescription, std::weak_ptr<GatewayRequest>> seen_;
};
} // namespace ipfs::gw

#endif // IPFS_DEDUPLICATING_REQUESTOR_H_
2 changes: 2 additions & 0 deletions library/src/ipfs_client/gw/default_requestor.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <ipfs_client/gw/default_requestor.h>

#include <ipfs_client/gw/block_request_splitter.h>
#include <ipfs_client/gw/deduplicating_requestor.h>
#include <ipfs_client/gw/dnslink_requestor.h>
#include <ipfs_client/gw/gateway_http_requestor.h>
#include <ipfs_client/gw/inline_request_handler.h>
Expand All @@ -12,6 +13,7 @@ auto ipfs::gw::default_requestor(ipfs::GatewayList gws,
std::shared_ptr<ContextApi> api)
-> std::shared_ptr<Requestor> {
auto result = std::make_shared<gw::InlineRequestHandler>();
result->or_else(std::make_shared<DeduplicatingRequestor>());
if (early) {
result->or_else(early);
early->api(api);

Check warning on line 19 in library/src/ipfs_client/gw/default_requestor.cc

View check run for this annotation

Codecov / codecov/patch

library/src/ipfs_client/gw/default_requestor.cc#L16-L19

Added lines #L16 - L19 were not covered by tests
Expand Down
4 changes: 4 additions & 0 deletions library/src/ipfs_client/gw/gateway_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <ipfs_client/ipld/ipns_name.h>

#include <ipfs_client/dag_block.h>
#include <ipfs_client/ipfs_request.h>
#include <ipfs_client/ipns_record.h>
#include <ipfs_client/orchestrator.h>
#include <ipfs_client/response.h>
Expand Down Expand Up @@ -198,6 +199,9 @@ std::string Self::debug_string() const {
if (!path.empty()) {
oss << ' ' << path;
}
if (dependent) {
oss << " for=" << dependent->path().to_string();
}
oss << " plel=" << parallel << '}';
return oss.str();
}
Expand Down
11 changes: 6 additions & 5 deletions library/src/ipfs_client/gw/requestor_pool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,17 @@ auto Self::check(ipfs::gw::RequestPtr req, std::size_t start) -> HandleOutcome {
}
}
if (req->parallel > 0) {
VLOG(2) << req->parallel << " requestors have picked up the task.";
LOG(INFO) << req->parallel << " requestors have picked up the task "
<< req->debug_string();
return O::PENDING;
}
if (next_retry < pool.size()) {
VLOG(1) << "No requestors are available for " << req->debug_string()
<< " right now, will retry at index " << next_retry;
LOG(INFO) << "No requestors are available for " << req->debug_string()
<< " right now, will retry at index " << next_retry;
waiting.emplace(req, next_retry);
return O::PENDING;
}
VLOG(1) << "Have exhausted all requestors in pool looking for "
<< req->debug_string();
LOG(INFO) << "Have exhausted all requestors in pool looking for "
<< req->debug_string();

Check warning on line 65 in library/src/ipfs_client/gw/requestor_pool.cc

View check run for this annotation

Codecov / codecov/patch

library/src/ipfs_client/gw/requestor_pool.cc#L64-L65

Added lines #L64 - L65 were not covered by tests
return O::NOT_HANDLED;
}

0 comments on commit bd36a2d

Please sign in to comment.