Skip to content

Commit 72d0d96

Browse files
committed
don't directly expose blackboards to filters
1 parent 28e8f2b commit 72d0d96

File tree

5 files changed

+29
-15
lines changed

5 files changed

+29
-15
lines changed

BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,6 +2043,7 @@ grpc_cc_library(
20432043
"//src/core:arena_promise",
20442044
"//src/core:atomic_utils",
20452045
"//src/core:bitset",
2046+
"//src/core:blackboard",
20462047
"//src/core:call_destination",
20472048
"//src/core:call_filters",
20482049
"//src/core:call_final_info",

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build_autogenerated.yaml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core/ext/filters/gcp_authentication/gcp_authentication_filter.cc

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -176,19 +176,13 @@ GcpAuthenticationFilter::Create(const ChannelArgs& args,
176176
"gcp_auth: xds config not found in channel args");
177177
}
178178
// Get existing cache or create new one.
179-
RefCountedPtr<CallCredentialsCache> cache;
180-
if (filter_args.old_blackboard() != nullptr) {
181-
cache = filter_args.old_blackboard()->Get<CallCredentialsCache>(
182-
filter_config->filter_instance_name);
183-
cache->SetMaxSize(filter_config->cache_size);
184-
}
185-
if (cache == nullptr) {
186-
cache = MakeRefCounted<CallCredentialsCache>(filter_config->cache_size);
187-
}
188-
if (filter_args.new_blackboard() != nullptr) {
189-
filter_args.new_blackboard()->Set(filter_config->filter_instance_name,
190-
cache);
191-
}
179+
auto cache = filter_args.GetOrCreateState<CallCredentialsCache>(
180+
filter_config->filter_instance_name, [&]() {
181+
return MakeRefCounted<CallCredentialsCache>(filter_config->cache_size);
182+
});
183+
// Make sure size is updated, in case we're reusing a pre-existing
184+
// cache but it has the wrong size.
185+
cache->SetMaxSize(filter_config->cache_size);
192186
// Instantiate filter.
193187
return std::unique_ptr<GcpAuthenticationFilter>(new GcpAuthenticationFilter(
194188
filter_config, std::move(xds_config), std::move(cache)));

src/core/lib/channel/promise_based_filter.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include <grpc/grpc.h>
4343
#include <grpc/support/port_platform.h>
4444

45+
#include "src/core/filter/blackboard.h"
4546
#include "src/core/lib/channel/call_finalization.h"
4647
#include "src/core/lib/channel/channel_args.h"
4748
#include "src/core/lib/channel/channel_fwd.h"
@@ -121,8 +122,20 @@ class ChannelFilter {
121122
[](const V3Based& v3) { return v3.instance_id; });
122123
}
123124

124-
const Blackboard* old_blackboard() const { return old_blackboard_; }
125-
Blackboard* new_blackboard() const { return new_blackboard_; }
125+
// If a filter state object of type T exists for key from a previous
126+
// filter stack, retains it for the new filter stack we're constructing.
127+
// Otherwise, invokes create_func() to create a new filter state
128+
// object for the new filter stack. Returns the new filter state object.
129+
template <typename T>
130+
RefCountedPtr<T> GetOrCreateState(
131+
const std::string& key,
132+
absl::FunctionRef<RefCountedPtr<T>()> create_func) {
133+
RefCountedPtr<T> state;
134+
if (old_blackboard_ != nullptr) state = old_blackboard_->Get<T>(key);
135+
if (state == nullptr) state = create_func();
136+
if (new_blackboard_ != nullptr) new_blackboard_->Set(key, state);
137+
return state;
138+
}
126139

127140
private:
128141
friend class ChannelFilter;

0 commit comments

Comments
 (0)