Skip to content

Commit

Permalink
mobile: API registration takes string&&, retrieval takes string_view (e…
Browse files Browse the repository at this point in the history
…nvoyproxy#32056)

`string&&` makes it clear on setting that the `name` param will be `std::move`'d when creating the key.
`string_view` for lookup because flat_hash_map supports heterogenous lookups: https://abseil.io/docs/cpp/guides/container#heterogeneous-lookup

Signed-off-by: Ali Beyad <[email protected]>
  • Loading branch information
abeyad authored Jan 26, 2024
1 parent 5d2f3ae commit dc69662
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 15 deletions.
8 changes: 5 additions & 3 deletions mobile/library/common/api/external.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "external.h"

#include <string>

#include "source/common/common/assert.h"

#include "absl/container/flat_hash_map.h"
Expand All @@ -15,14 +17,14 @@ static absl::flat_hash_map<std::string, void*> registry_{};
// TODO(goaway): To expose this for general usage, it will need to be made thread-safe. For now it
// relies on the assumption that usage will occur only as part of Engine configuration, and thus be
// limited to a single thread.
void registerApi(std::string name, void* api) {
void registerApi(std::string&& name, void* api) {
RELEASE_ASSERT(api != nullptr, "cannot register null API");
registry_[name] = api;
registry_[std::move(name)] = api;
}

// TODO(goaway): This is not thread-safe, but the assumption here is that all writes will complete
// before any reads occur.
void* retrieveApi(std::string name, bool allow_absent) {
void* retrieveApi(absl::string_view name, bool allow_absent) {
void* api = registry_[name];
if (!allow_absent) {
RELEASE_ASSERT(api != nullptr, fmt::format("{} not registered", name));
Expand Down
6 changes: 4 additions & 2 deletions mobile/library/common/api/external.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@

#include <string>

#include "absl/strings/string_view.h"

namespace Envoy {
namespace Api {
namespace External {

/**
* Register an external runtime API for usage (e.g. in extensions).
*/
void registerApi(std::string name, void* api);
void registerApi(std::string&& name, void* api);

/**
* Retrieve an external runtime API for usage (e.g. in extensions).
*/
void* retrieveApi(std::string name, bool allow_absent = false);
void* retrieveApi(absl::string_view name, bool allow_absent = false);

} // namespace External
} // namespace Api
Expand Down
10 changes: 4 additions & 6 deletions mobile/library/common/jni/jni_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -964,8 +964,7 @@ Java_io_envoyproxy_envoymobile_engine_JniLibrary_registerKeyValueStore(JNIEnv* e

Envoy::JNI::JniHelper jni_helper(env);
Envoy::JNI::StringUtfUniquePtr native_java_string = jni_helper.getStringUtfChars(name, nullptr);
std::string api_name(native_java_string.get());
Envoy::Api::External::registerApi(api_name, api);
Envoy::Api::External::registerApi(/*name=*/std::string(native_java_string.get()), api);
return ENVOY_SUCCESS;
}

Expand Down Expand Up @@ -1003,8 +1002,7 @@ Java_io_envoyproxy_envoymobile_engine_JniLibrary_registerFilterFactory(JNIEnv* e
Envoy::JNI::JniHelper jni_helper(env);
Envoy::JNI::StringUtfUniquePtr native_java_string =
jni_helper.getStringUtfChars(filter_name, nullptr);
std::string api_name(native_java_string.get());
Envoy::Api::External::registerApi(api_name, api);
Envoy::Api::External::registerApi(/*name=*/std::string(native_java_string.get()), api);
return ENVOY_SUCCESS;
}

Expand Down Expand Up @@ -1129,8 +1127,8 @@ Java_io_envoyproxy_envoymobile_engine_JniLibrary_registerStringAccessor(JNIEnv*
Envoy::JNI::JniHelper jni_helper(env);
Envoy::JNI::StringUtfUniquePtr native_java_string =
jni_helper.getStringUtfChars(accessor_name, nullptr);
std::string api_name(native_java_string.get());
Envoy::Api::External::registerApi(api_name, string_accessor);
Envoy::Api::External::registerApi(/*name=*/std::string(native_java_string.get()),
string_accessor);
return ENVOY_SUCCESS;
}

Expand Down
2 changes: 1 addition & 1 deletion mobile/test/common/common/lambda_logger_delegate_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class LambdaDelegateTest : public testing::Test {
static envoy_event_tracker tracker;

static void SetUpTestSuite() {
Api::External::registerApi(envoy_event_tracker_api_name, &tracker);
Api::External::registerApi(std::string(envoy_event_tracker_api_name), &tracker);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class PlatformBridgeIntegrationTest : public testing::TestWithParam<Network::Add
setDownstreamProtocol(Http::CodecClient::Type::HTTP2);
setUpstreamProtocol(FakeHttpConnection::Type::HTTP2);

Api::External::registerApi("filter_1", filter_1);
Api::External::registerApi("filter_2", filter_2);
Api::External::registerApi(std::string("filter_1"), filter_1);
Api::External::registerApi(std::string("filter_2"), filter_2);

config_helper_.addFilter(filter_config_1);
config_helper_.addFilter(filter_config_2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class PlatformBridgeFilterTest : public testing::Test {
void setUpFilter(std::string name, envoy_http_filter* platform_filter) {
envoymobile::extensions::filters::http::platform_bridge::PlatformBridge config;
config.set_platform_filter_name(name);
Api::External::registerApi(config.platform_filter_name(), platform_filter);
Api::External::registerApi(std::string(config.platform_filter_name()), platform_filter);

config_ = std::make_shared<PlatformBridgeFilterConfig>(context_, config);
filter_ = std::make_shared<PlatformBridgeFilter>(config_, dispatcher_);
Expand Down

0 comments on commit dc69662

Please sign in to comment.