Skip to content
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

Add list of hosts with ports for setting connection #1

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

df530
Copy link
Collaborator

@df530 df530 commented Nov 29, 2021

SetHost can take std::vector of ClientOptions::HostPort structures, that contains host name and optionally port number. If HostPort.port is nullopt, then port from SetPort (or default port if SetPort wasn't called) is used.

When ResetConnection is called, it tries to connect to the first host. If connection failed, then it tries to connect to the next host in list, while list doesn't end up. If list ends up, ResetConnection throws exception of last host.

@df530 df530 changed the title List of hosts with ports and triend connect to all Add list of hosts with ports for setting connection Dec 13, 2021
if (!Handshake()) {
throw std::runtime_error("fail to connect to " + host_port.host);
}
} catch (...) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to catch specific error

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it better? We can't predict what type of object will be thrown. Yea, in this function we throw only runtime_error, but we don't exactly know what could be thrown from functions, that are used in ResetConnection.

Moreover, in RetryGuard previous developers don't catch specific error -- they also use .... It is partly confirms hypothesis, that any object can be thrown from subfunctions of ResetConnetction.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we can't predict the type of exception, we don't know the code. But we should do.
It is better because when you read the code, you understand what you throw.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can catch runtime_error and other exceptions and then use the ...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -510,6 +510,67 @@ int main() {
.SetCompressionMethod(CompressionMethod::LZ4));
RunTests(client);
}

{
std::cout << "test 1" << std::endl;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please give names to tests

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oo, I deleted this line before review, but fogot to commit it. I think I would better to leave tests without names, because previous tests don't have names.

@@ -481,7 +481,7 @@ static void RunTests(Client& client) {
ArrayExample(client);
CancelableExample(client);
DateExample(client);
DateTime64Example(client);
// DateTime64Example(client);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't delete tests

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also forgot to commit uncommented line.

I had commented it because this example always fails there.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, you are right, but I am not sure it's your problem, so you can keep it unchanged for now

ClientOptions::HostPort("1127.91.2.2"), // wrong host
ClientOptions::HostPort("notlocalwronghost"), // wrong host
ClientOptions::HostPort("another_notlocalwronghost"), // wrong host
ClientOptions::HostPort("localhost", 9000),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you assert somehow that you have connected to exactly this host? And please add some more hosts after this one

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hosts added.

By previous version I couldn't assert connected host. But I add method getConnectedHost to Client and use it for assert.

std::string host = std::string();
inline ClientOptions& SetHost(const std::string& value) {
hosts_ports.emplace_back(value, std::nullopt);
host = hosts_ports.back().host;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so you keep host and HostPort. So host is duplicated. Can it be kept just one time?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it can be kept just one time, I made relevant changes.

<< " ping_before_query:" << opt.ping_before_query
os << "Client(" << opt.user << '@' << "{ ";
for (const ClientOptions::HostPort& hp : opt.hosts_ports) {
os << hp.host << ":" << opt.port << ",";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to make output so that if you have just one host it is the same as before your changes

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done and also fixed other errors (like forgotten }, etc)

/// Hostname of the server.
DECLARE_FIELD(host, std::string, SetHost, std::string());
std::string host = std::string();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to remove it so not to duplicate. If you don't remove it somehow, you need to declare this field with DECLARE_FIELD

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't remove host because it is a public field.
I returned declaration by DECLARE_FIELD

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, ok, leave it as is for now. But it would be great to write it in the pull request description that the host field is "duplicated"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this host is duplicated? Now host in host field isn't stored in hosts_ports.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants