Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(82)

Unified Diff: net/http/disk_based_cert_cache.cc

Issue 361513003: Improving and adding an in-memory MRU cache to DiskBasedCertCache. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@DBCC_Implement
Patch Set: Added tests, and fixed issues with last patch. Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/http/disk_based_cert_cache.cc
diff --git a/net/http/disk_based_cert_cache.cc b/net/http/disk_based_cert_cache.cc
index b6c5b7e2731b5ede6215fefe0f2ffceba679f5e6..138d5a63195da00b6685e6ee479a27d3d67389d4 100644
--- a/net/http/disk_based_cert_cache.cc
+++ b/net/http/disk_based_cert_cache.cc
@@ -19,6 +19,9 @@ namespace net {
namespace {
+// TODO(brandonsalmon): change this number to improve performance.
+const int kMemoryCacheMaxSize = 30;
Ryan Sleevi 2014/07/02 21:49:58 size_t
+
// Used to obtain a unique cache key for a certificate in the form of
// "cert:<hash>".
std::string GetCacheKeyToCert(const X509Certificate::OSCertHandle cert_handle) {
@@ -206,10 +209,9 @@ int DiskBasedCertCache::WriteWorker::DoOpen() {
}
int DiskBasedCertCache::WriteWorker::DoOpenComplete(int rv) {
- if (rv < 0) {
- state_ = STATE_NONE;
+ if (rv < 0)
return rv;
- }
+
state_ = STATE_WRITE;
return OK;
}
@@ -218,10 +220,8 @@ int DiskBasedCertCache::WriteWorker::DoWrite() {
std::string write_data;
bool encoded = X509Certificate::GetDEREncoded(cert_handle_, &write_data);
- if (!encoded) {
- state_ = STATE_NONE;
+ if (!encoded)
return ERR_FAILED;
- }
buffer_ = new IOBuffer(write_data.size());
io_buf_len_ = write_data.size();
@@ -238,7 +238,6 @@ int DiskBasedCertCache::WriteWorker::DoWrite() {
}
int DiskBasedCertCache::WriteWorker::DoWriteComplete(int rv) {
- state_ = STATE_NONE;
if (rv < io_buf_len_)
return ERR_FAILED;
@@ -287,7 +286,7 @@ class DiskBasedCertCache::ReadWorker {
// regardless of success or failure.
ReadWorker(disk_cache::Backend* backend,
const std::string& key,
- const base::Closure& cleanup_callback);
+ const GetCallback& cleanup_callback);
~ReadWorker();
@@ -335,15 +334,14 @@ class DiskBasedCertCache::ReadWorker {
scoped_refptr<IOBuffer> buffer_;
int io_buf_len_;
- base::Closure cleanup_callback_;
+ GetCallback cleanup_callback_;
std::vector<GetCallback> user_callbacks_;
CompletionCallback io_callback_;
};
-DiskBasedCertCache::ReadWorker::ReadWorker(
- disk_cache::Backend* backend,
- const std::string& key,
- const base::Closure& cleanup_callback)
+DiskBasedCertCache::ReadWorker::ReadWorker(disk_cache::Backend* backend,
+ const std::string& key,
+ const GetCallback& cleanup_callback)
: backend_(backend),
cert_handle_(NULL),
key_(key),
@@ -418,10 +416,9 @@ int DiskBasedCertCache::ReadWorker::DoOpen() {
}
int DiskBasedCertCache::ReadWorker::DoOpenComplete(int rv) {
- if (rv < 0) {
- state_ = STATE_NONE;
+ if (rv < 0)
return rv;
- }
+
state_ = STATE_READ;
return OK;
}
@@ -435,7 +432,6 @@ int DiskBasedCertCache::ReadWorker::DoRead() {
}
int DiskBasedCertCache::ReadWorker::DoReadComplete(int rv) {
- state_ = STATE_NONE;
if (rv < io_buf_len_)
return ERR_FAILED;
@@ -457,7 +453,7 @@ void DiskBasedCertCache::ReadWorker::RunCallbacks() {
}
void DiskBasedCertCache::ReadWorker::Finish(int rv) {
- cleanup_callback_.Run();
+ cleanup_callback_.Run(cert_handle_);
cleanup_callback_.Reset();
RunCallbacks();
delete this;
@@ -475,7 +471,11 @@ DiskBasedCertCache::ReadWorker::~ReadWorker() {
}
DiskBasedCertCache::DiskBasedCertCache(disk_cache::Backend* backend)
- : backend_(backend), weak_factory_(this) {
+ : backend_(backend),
+ mru_cert_cache_(kMemoryCacheMaxSize),
+ mem_cache_hits_(0),
+ mem_cache_misses_(0),
+ weak_factory_(this) {
DCHECK(backend_);
}
@@ -495,6 +495,17 @@ DiskBasedCertCache::~DiskBasedCertCache() {
void DiskBasedCertCache::Get(const std::string& key, const GetCallback& cb) {
DCHECK(!key.empty());
+ // If the handle is already in the MRU cache, just return that (via callback).
+ // Note, this will also bring the cert_handle to the front
wtc 2014/07/02 20:51:50 Nit: this line break is too short.
+ // of the recency list in the MRU cache.
+ MRUCertCache::iterator mru_it = mru_cert_cache_.Get(key);
+ if (mru_it != mru_cert_cache_.end()) {
+ ++mem_cache_hits_;
+ cb.Run(mru_it->second);
+ return;
rvargas (doing something else) 2014/07/02 22:14:38 You may want to tell the backend about this: backe
+ }
+ ++mem_cache_misses_;
+
ReadWorkerMap::iterator it = read_worker_map_.find(key);
if (it == read_worker_map_.end()) {
@@ -518,6 +529,16 @@ void DiskBasedCertCache::Set(const X509Certificate::OSCertHandle cert_handle,
DCHECK(cert_handle);
std::string key = GetCacheKeyToCert(cert_handle);
+ // If |cert_handle| already exists in the MRU cache, there is no need to
+ // re-write it to the disk cache. This will also bring |cert_handle|
+ // to the front of the recency list in the MRU cache.
+ if (mru_cert_cache_.Get(key) != mru_cert_cache_.end()) {
+ ++mem_cache_hits_;
+ cb.Run(key);
+ return;
+ }
+ ++mem_cache_misses_;
wtc 2014/07/02 20:51:50 IMPORTANT: We should think about whether Set() sho
Ryan Sleevi 2014/07/02 21:49:58 Yes. I think we only want Get to be satisfied by
+
WriteWorkerMap::iterator it = write_worker_map_.find(key);
if (it == write_worker_map_.end()) {
@@ -527,7 +548,8 @@ void DiskBasedCertCache::Set(const X509Certificate::OSCertHandle cert_handle,
cert_handle,
base::Bind(&DiskBasedCertCache::FinishedWriteOperation,
weak_factory_.GetWeakPtr(),
- key));
+ key,
+ cert_handle));
write_worker_map_[key] = worker;
worker->AddCallback(cb);
worker->Start();
@@ -536,11 +558,19 @@ void DiskBasedCertCache::Set(const X509Certificate::OSCertHandle cert_handle,
}
}
-void DiskBasedCertCache::FinishedWriteOperation(const std::string& key) {
+void DiskBasedCertCache::FinishedWriteOperation(
+ const std::string& key,
+ X509Certificate::OSCertHandle cert_handle) {
write_worker_map_.erase(key);
+ if (key != std::string())
wtc 2014/07/02 20:51:49 Test !key.empty().
+ mru_cert_cache_.Put(key, X509Certificate::DupOSCertHandle(cert_handle));
}
-void DiskBasedCertCache::FinishedReadOperation(const std::string& key) {
+void DiskBasedCertCache::FinishedReadOperation(
+ const std::string& key,
+ X509Certificate::OSCertHandle cert_handle) {
+ if (cert_handle)
+ mru_cert_cache_.Put(key, X509Certificate::DupOSCertHandle(cert_handle));
read_worker_map_.erase(key);
}

Powered by Google App Engine
This is Rietveld 408576698