Skip to content

Commit

Permalink
expose API to fetch a cache entry without promoting uses
Browse files Browse the repository at this point in the history
Summary: In fblite, we want the ability to query the validity of currently stored PSK tickets, but fetching entries from the cache promotes the uses towards eviction, so I've added the ability to get the stored PSK without promoting its usage count.

Reviewed By: cgrushko, knekritz

Differential Revision: D69534167

fbshipit-source-id: dcb6de72bb0d1addcbc85778daaf2a583d188603
  • Loading branch information
Gil Sless authored and facebook-github-bot committed Feb 18, 2025
1 parent 596c2d8 commit d25e573
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
20 changes: 20 additions & 0 deletions proxygen/lib/transport/PersistentFizzPskCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ class PersistentFizzPskCache : public fizz::client::PskCache {
return folly::none;
}

/**
* Returns a PSK, without increasing its usage count. Keep in mind that it
* would still update the internal LRU cache
*/
folly::Optional<fizz::client::CachedPsk> peekPsk(
const std::string& identity) {
auto serialized = cache_.get(identity);
if (serialized) {
try {
return fizz::client::deserializePsk(
fizz::openssl::certificateSerializer(),
folly::ByteRange(serialized->serialized));
} catch (const std::exception& ex) {
LOG(ERROR) << "Error deserializing PSK: " << ex.what();
cache_.remove(identity);
}
}
return folly::none;
}

folly::Optional<fizz::client::CachedPsk> getPsk(
const std::string& identity) override {
auto serialized = cache_.get(identity);
Expand Down
19 changes: 19 additions & 0 deletions proxygen/lib/transport/test/PersistentFizzPskCacheTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,23 @@ TEST_F(PersistentFizzPskCacheTest, TestGetPskUses) {
cache_->getPsk("facebook.com");
EXPECT_EQ(folly::none, cache_->getPskUses("facebook.com"));
}

TEST_F(PersistentFizzPskCacheTest, TestPeekPsk) {
cache_->setMaxPskUses(5);

EXPECT_EQ(folly::none, cache_->getPskUses("facebook.com"));
cache_->putPsk("facebook.com", psk1_);
EXPECT_EQ(0u, cache_->getPskUses("facebook.com"));

cache_->getPsk("facebook.com");
EXPECT_EQ(1u, cache_->getPskUses("facebook.com"));

for (size_t i = 0; i < 10; i++) {
expectMatch(*cache_->peekPsk("facebook.com"), psk1_);
EXPECT_EQ(1u, cache_->getPskUses("facebook.com"));
}

cache_->getPsk("facebook.com");
EXPECT_EQ(2u, cache_->getPskUses("facebook.com"));
}
}} // namespace proxygen::test

0 comments on commit d25e573

Please sign in to comment.