-
Notifications
You must be signed in to change notification settings - Fork 77
[feature] Support set ServiceUrlProvider when create client. #170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -39,6 +39,7 @@ typedef std::function<void(Result, Consumer)> SubscribeCallback; | |||||||||||||||||||||||||||||||
| typedef std::function<void(Result, Reader)> ReaderCallback; | ||||||||||||||||||||||||||||||||
| typedef std::function<void(Result, const std::vector<std::string>&)> GetPartitionsCallback; | ||||||||||||||||||||||||||||||||
| typedef std::function<void(Result)> CloseCallback; | ||||||||||||||||||||||||||||||||
| typedef std::function<const std::string&()> ServiceUrlProvider; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| class ClientImpl; | ||||||||||||||||||||||||||||||||
| class PulsarFriend; | ||||||||||||||||||||||||||||||||
|
|
@@ -66,6 +67,31 @@ class PULSAR_PUBLIC Client { | |||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||
| Client(const std::string& serviceUrl, const ClientConfiguration& clientConfiguration); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||
| * Create a Pulsar client object connecting to the specified cluster address and using the default | ||||||||||||||||||||||||||||||||
| * configuration. | ||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||
| * <p>Instead of specifying a static service URL string (with {@link #serviceUrl(String)}), an application | ||||||||||||||||||||||||||||||||
| * can pass a {@link ServiceUrlProvider} function that dynamically provide a service URL. | ||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||
| * @param serviceUrlProvider The serviceUrlProvider used to generate ServiceUrl. | ||||||||||||||||||||||||||||||||
| * @throw std::invalid_argument if `serviceUrlProvider()` return a invalid url. | ||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||
| Client(ServiceUrlProvider serviceUrlProvider); | ||||||||||||||||||||||||||||||||
|
Comment on lines
+70
to
+80
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Avoid unnecessary duplicated code and API docs. |
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||
| * Create a Pulsar client object connecting to the specified cluster address and using the specified | ||||||||||||||||||||||||||||||||
| * configuration. | ||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||
| * <p>Instead of specifying a static service URL string (with {@link #serviceUrl(String)}), an application | ||||||||||||||||||||||||||||||||
| * can pass a {@link ServiceUrlProvider} instance that dynamically provide a service URL. | ||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||
| * @param serviceUrlProvider The serviceUrlProvider used to generate ServiceUrl. | ||||||||||||||||||||||||||||||||
| * @param clientConfiguration the client configuration to use | ||||||||||||||||||||||||||||||||
| * @throw std::invalid_argument if `serviceUrlProvider()` return a invalid url. | ||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||
| Client(ServiceUrlProvider serviceUrlProvider, const ClientConfiguration& clientConfiguration); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||
| * Create a producer with default configuration | ||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||
|
|
@@ -333,6 +359,17 @@ class PULSAR_PUBLIC Client { | |||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||
| void getPartitionsForTopicAsync(const std::string& topic, GetPartitionsCallback callback); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||
| * Update the service URL this client is using. | ||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||
| * <p>This will force the client close all existing connections and to restart service discovery to the | ||||||||||||||||||||||||||||||||
| * new service endpoint. | ||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||
| * @param serviceUrl | ||||||||||||||||||||||||||||||||
| * the new service URL this client should connect to | ||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||
| Result updateServiceUrl(const std::string& serviceUrl); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||
| * @return | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,89 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 <gtest/gtest.h> | ||
| #include <pulsar/Client.h> | ||
|
|
||
| using namespace pulsar; | ||
|
|
||
| static const std::string lookupUrl = "pulsar://localhost:6650"; | ||
| static const std::string adminUrl = "http://localhost:8080/"; | ||
|
|
||
| class ServiceUrlProviderTest : public ::testing::TestWithParam<std::string> { | ||
| public: | ||
| void SetUp() override { serviceUrl = GetParam(); } | ||
|
|
||
| std::string serviceUrl; | ||
| }; | ||
|
|
||
| TEST(ServiceUrlProviderTest, testClientClose) { | ||
| const std::string topicName = "testClientClose-" + std::to_string(time(nullptr)); | ||
| Client client([]() -> const std::string& { return lookupUrl; }); | ||
| client.close(); | ||
| ASSERT_EQ(ResultAlreadyClosed, client.updateServiceUrl(lookupUrl)); | ||
| std::map<std::string, std::string> testMap; | ||
| } | ||
|
|
||
| TEST_P(ServiceUrlProviderTest, testBasicUpdateUrl) { | ||
| const std::string topicName = "basicUpdateUrl-" + std::to_string(time(nullptr)); | ||
| Client client([this]() -> const std::string& { return serviceUrl; }); | ||
|
|
||
| Producer producer1; | ||
| ASSERT_EQ(ResultOk, client.createProducer(topicName, producer1)); | ||
|
|
||
| Consumer consumer; | ||
| ASSERT_EQ(ResultOk, client.subscribe(topicName, "test-sub", consumer)); | ||
|
|
||
| // Update service url. | ||
| ASSERT_EQ(ResultOk, client.updateServiceUrl(serviceUrl)); | ||
| Producer producer2; | ||
| ASSERT_EQ(ResultOk, client.createProducer(topicName, producer2)); | ||
|
|
||
| // Assert that both producer1 and producer2 are available | ||
| int sendNum = 10; | ||
| for (int i = 0; i < sendNum; ++i) { | ||
| ASSERT_EQ(ResultOk, producer1.send(MessageBuilder().setContent("test").build())); | ||
| ASSERT_EQ(ResultOk, producer2.send(MessageBuilder().setContent("test").build())); | ||
| } | ||
|
|
||
| Message msg; | ||
| for (int i = 0; i < 2 * sendNum; ++i) { | ||
| ASSERT_EQ(ResultOk, consumer.receive(msg)); | ||
| } | ||
|
|
||
| client.close(); | ||
| } | ||
|
|
||
| TEST(ServiceUrlProviderTest, testInvalidServiceUrl) { | ||
| const std::string invalidServiceUrl = "invalid://localhost:6650"; | ||
|
|
||
| // Assert invalid url throw exception when create client. | ||
| { | ||
| ASSERT_THROW( | ||
| Client client([&invalidServiceUrl]() -> const std::string& { return invalidServiceUrl; }), | ||
| std::invalid_argument); | ||
| } | ||
|
|
||
| // Assert return ResultInvalidUrl when client.updateServiceUrl(invalidServiceUrl); | ||
| { | ||
| Client client([]() -> const std::string& { return lookupUrl; }); | ||
| ASSERT_EQ(ResultInvalidUrl, client.updateServiceUrl(invalidServiceUrl)); | ||
| } | ||
| } | ||
|
|
||
| INSTANTIATE_TEST_SUITE_P(BasicEndToEndTest, ServiceUrlProviderTest, testing::Values(lookupUrl, adminUrl)); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.