When connecting a Rust based mongodb client (rust mongodb 2.6.0) to a replica set, Is there a way to ignore a failure from one of the hosts?

huangapple go评论65阅读模式
英文:

When connecting a Rust based mongodb client (rust mongodb 2.6.0) to a replica set, Is there a way to ignore a failure from one of the hosts?

问题

我正在开发一个 Rust 应用程序,用于读取 MongoDB 文档。连接是通过一个代表复制集的字符串完成的: "mongodb://host1:port1,host2:port2..."。

我使用类似以下的代码:

let servers_list = "mongodb://host1:port1,host2:port2,host3:port3";
let mut client_options = ClientOptions::parse(servers_list).await?;
servers_list.app_name = Some("name".to_string());
let client = Client::with_options(client_options)?;
let db = client.database("database_name".to_string())?;
let collection = db.collection::<Document>("collection_name".to_string());

鉴于只有 host1:port1 是有效的,我的读取尝试会导致返回的 Result Err 崩溃。
我希望只从其中一个服务器读取,而不检查它们全部是否有效。

当使用 Compass 连接到相同的复制集时,我会连接到有效的主机,就像尝试连接到其中一个而不提及其他主机一样。

所以我的问题是:
我是否可以在 Rust 库中获得相同的行为,而不需要我自己进行特殊的错误处理?
是否可以使用选项来实现这种行为?

谢谢

英文:

I'm working on a rust application that reads a mongodb document. The connection is done using a string that should represent a replica set: "mongodb://host1:port1,host2:port2...".

I use similar code to the following:

    let servers_list = &quot;mongodb://host1:port1,host2:port2,host3:port3&quot;;
    let mut client_options = ClientOptions::parse(servers_list).await?;
    servers_list.app_name - Some(&quot;name&quot;.to_string());
    let client = Client::with_options(client_options)?;
    let db = client.database(&quot;database_name&quot;.to_string())?;
    let collection = db.collection::&lt;Document&gt;(&quot;collection_name&quot;.to_string());

Given that only host1:port1 is valid, my read attempt crashes with a returned Result Err.
I wish to read from just one of the servers, without checking all of them for validity.

When connecting to the same replica set using Compass, I get a connection to the valid host, and its just like trying to connect to that one without mentioning the others.

So my question is:
Can I get the same behavior using the rust library without any special error handling on my side?
Is it possible to use the options to get that behavior?

Thanks

答案1

得分: 0

问题原来是由于糟糕的服务器配置引起的,导致mongodb服务器只是3台独立的服务器,当客户端无法获取其中一个连接时,它返回了错误。

英文:

The issue turned out to be a bad server configuration, which caused the mongodb servers to just be 3 independent servers, and when the client failed to obtain one of the connections it returned the error.

huangapple
  • 本文由 发表于 2023年7月27日 21:36:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780322.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定