-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[EventEngine] Add public methods to allow EventEngine Endpoints to su…
…pport optional Extensions. PiperOrigin-RevId: 587071965
- Loading branch information
1 parent
49f7ee9
commit 8467882
Showing
10 changed files
with
319 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright 2023 gRPC Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
#ifndef GRPC_SRC_CORE_LIB_EVENT_ENGINE_QUERY_EXTENSIONS_H | ||
#define GRPC_SRC_CORE_LIB_EVENT_ENGINE_QUERY_EXTENSIONS_H | ||
|
||
#include <grpc/support/port_platform.h> | ||
|
||
#include "absl/strings/string_view.h" | ||
|
||
#include <grpc/event_engine/event_engine.h> | ||
|
||
namespace grpc_event_engine { | ||
namespace experimental { | ||
|
||
namespace endpoint_detail { | ||
|
||
template <typename Querying, typename... Es> | ||
struct QueryExtensionRecursion; | ||
|
||
template <typename Querying, typename E, typename... Es> | ||
struct QueryExtensionRecursion<Querying, E, Es...> { | ||
static void* Query(absl::string_view id, Querying* p) { | ||
if (id == E::EndpointExtensionName()) return static_cast<E*>(p); | ||
return QueryExtensionRecursion<Querying, Es...>::Query(id, p); | ||
} | ||
}; | ||
|
||
template <typename Querying> | ||
struct QueryExtensionRecursion<Querying> { | ||
static void* Query(absl::string_view, Querying*) { return nullptr; } | ||
}; | ||
|
||
} // namespace endpoint_detail | ||
|
||
// A helper class to derive from some set of base classes and export | ||
// QueryExtension for them all. | ||
// Endpoint implementations which need to support different extensions just need | ||
// to derive from ExtendedEndpoint class. | ||
template <typename... Exports> | ||
class ExtendedEndpoint : public EventEngine::Endpoint, public Exports... { | ||
public: | ||
void* QueryExtension(absl::string_view id) override { | ||
return endpoint_detail::QueryExtensionRecursion<ExtendedEndpoint, | ||
Exports...>::Query(id, | ||
this); | ||
} | ||
}; | ||
|
||
/// A helper method which returns a valid pointer if the extension is supported | ||
/// by the endpoint. | ||
template <typename T> | ||
T* QueryExtension(EventEngine::Endpoint* endpoint) { | ||
return static_cast<T*>(endpoint->QueryExtension(T::EndpointExtensionName())); | ||
} | ||
|
||
} // namespace experimental | ||
} // namespace grpc_event_engine | ||
|
||
#endif // GRPC_SRC_CORE_LIB_EVENT_ENGINE_QUERY_EXTENSIONS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright 2023 gRPC authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
#include <grpc/support/port_platform.h> | ||
|
||
#include "src/core/lib/event_engine/query_extensions.h" | ||
|
||
#include <string> | ||
|
||
#include "absl/functional/any_invocable.h" | ||
#include "absl/status/status.h" | ||
#include "gtest/gtest.h" | ||
|
||
#include <grpc/event_engine/event_engine.h> | ||
#include <grpc/event_engine/slice_buffer.h> | ||
|
||
#include "src/core/lib/gprpp/crash.h" | ||
|
||
namespace grpc_event_engine { | ||
namespace experimental { | ||
namespace { | ||
|
||
template <int i> | ||
class TestExtension { | ||
public: | ||
TestExtension() = default; | ||
~TestExtension() = default; | ||
|
||
static std::string EndpointExtensionName() { | ||
return "grpc.test.test_extension" + std::to_string(i); | ||
} | ||
|
||
int GetValue() const { return val_; } | ||
|
||
private: | ||
int val_ = i; | ||
}; | ||
|
||
class ExtendedTestEndpoint | ||
: public ExtendedEndpoint<TestExtension<0>, TestExtension<1>, | ||
TestExtension<2>> { | ||
public: | ||
ExtendedTestEndpoint() = default; | ||
~ExtendedTestEndpoint() override = default; | ||
bool Read(absl::AnyInvocable<void(absl::Status)> /*on_read*/, | ||
SliceBuffer* /*buffer*/, const ReadArgs* /*args*/) override { | ||
grpc_core::Crash("Not implemented"); | ||
}; | ||
bool Write(absl::AnyInvocable<void(absl::Status)> /*on_writable*/, | ||
SliceBuffer* /*data*/, const WriteArgs* /*args*/) override { | ||
grpc_core::Crash("Not implemented"); | ||
} | ||
/// Returns an address in the format described in DNSResolver. The returned | ||
/// values are expected to remain valid for the life of the Endpoint. | ||
const EventEngine::ResolvedAddress& GetPeerAddress() const override { | ||
grpc_core::Crash("Not implemented"); | ||
} | ||
const EventEngine::ResolvedAddress& GetLocalAddress() const override { | ||
grpc_core::Crash("Not implemented"); | ||
}; | ||
}; | ||
|
||
TEST(QueryExtensionsTest, EndpointSupportsMultipleExtensions) { | ||
ExtendedTestEndpoint endpoint; | ||
TestExtension<0>* extension_0 = QueryExtension<TestExtension<0>>(&endpoint); | ||
TestExtension<1>* extension_1 = QueryExtension<TestExtension<1>>(&endpoint); | ||
TestExtension<2>* extension_2 = QueryExtension<TestExtension<2>>(&endpoint); | ||
|
||
EXPECT_NE(extension_0, nullptr); | ||
EXPECT_NE(extension_1, nullptr); | ||
EXPECT_NE(extension_2, nullptr); | ||
|
||
EXPECT_EQ(extension_0->GetValue(), 0); | ||
EXPECT_EQ(extension_1->GetValue(), 1); | ||
EXPECT_EQ(extension_2->GetValue(), 2); | ||
} | ||
} // namespace | ||
|
||
} // namespace experimental | ||
} // namespace grpc_event_engine | ||
|
||
int main(int argc, char** argv) { | ||
testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.