When connecting to certain Azure SQL instances, the server may return an Error::Routing { host, port }. This indicates that you must establish a new connection to the redirected address provided in the error.
To handle this, catch the Error::Routing variant, update your Config with the new host and port, and attempt the connection again. You should typically only expect one redirect.
use tiberius::{Client, Config, AuthMethod, error::Error};
use tokio_util::compat::TokioAsyncWriteCompatExt;
use tokio::net::TcpStream;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut config = Config::new();
config.host("0.0.0.0");
config.port(1433);
config.authentication(AuthMethod::sql_server("SA", "<Mys3cureP4ssW0rD>"));
let tcp = TcpStream::connect(config.get_addr()).await?;
tcp.set_nodelay(true)?;
let client = match Client::connect(config, tcp.compat_write()).await {
// Connection successful.
Ok(client) => client,
// The server wants us to redirect to a different address
Err(Error::Routing { host, port }) => {
let mut config = Config::new();
config.host(&host);
config.port(port);
config.authentication(AuthMethod::sql_server("SA", "<Mys3cureP4ssW0rD>"));
let tcp = TcpStream::connect(config.get_addr()).await?;
tcp.set_nodelay(true)?;
// we should not have more than one redirect, so we'll short-circuit here.
Client::connect(config, tcp.compat_write()).await?
}
Err(e) => Err(e)?,
};
Ok(())
}