Skip to content

Commit ec8dbcc

Browse files
Resolving the TLS url issue (#459)
* resolving the url issue Signed-off-by: limbooverlambda <[email protected]> * fix formatting Signed-off-by: limbooverlambda <[email protected]> * make check fixes Signed-off-by: limbooverlambda <[email protected]> --------- Signed-off-by: limbooverlambda <[email protected]>
1 parent 54fd720 commit ec8dbcc

File tree

5 files changed

+31
-21
lines changed

5 files changed

+31
-21
lines changed

src/common/security.rs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ use std::time::Duration;
88

99
use log::info;
1010
use regex::Regex;
11-
use tonic::transport::Certificate;
1211
use tonic::transport::Channel;
1312
use tonic::transport::ClientTlsConfig;
1413
use tonic::transport::Identity;
14+
use tonic::transport::{Certificate, Endpoint};
1515

1616
use crate::internal_err;
1717
use crate::Result;
@@ -77,27 +77,40 @@ impl SecurityManager {
7777
where
7878
Factory: FnOnce(Channel) -> Client,
7979
{
80-
let addr = "http://".to_string() + &SCHEME_REG.replace(addr, "");
81-
8280
info!("connect to rpc server at endpoint: {:?}", addr);
81+
let channel = if !self.ca.is_empty() {
82+
self.tls_channel(addr).await?
83+
} else {
84+
self.default_channel(addr).await?
85+
};
86+
let ch = channel.connect().await?;
8387

84-
let mut builder = Channel::from_shared(addr)?
85-
.tcp_keepalive(Some(Duration::from_secs(10)))
86-
.keep_alive_timeout(Duration::from_secs(3));
88+
Ok(factory(ch))
89+
}
8790

88-
if !self.ca.is_empty() {
89-
let tls = ClientTlsConfig::new()
90-
.ca_certificate(Certificate::from_pem(&self.ca))
91-
.identity(Identity::from_pem(
92-
&self.cert,
93-
load_pem_file("private key", &self.key)?,
94-
));
95-
builder = builder.tls_config(tls)?;
96-
};
91+
async fn tls_channel(&self, addr: &str) -> Result<Endpoint> {
92+
let addr = "https://".to_string() + &SCHEME_REG.replace(addr, "");
93+
let builder = self.endpoint(addr.to_string())?;
94+
let tls = ClientTlsConfig::new()
95+
.ca_certificate(Certificate::from_pem(&self.ca))
96+
.identity(Identity::from_pem(
97+
&self.cert,
98+
load_pem_file("private key", &self.key)?,
99+
));
100+
let builder = builder.tls_config(tls)?;
101+
Ok(builder)
102+
}
97103

98-
let ch = builder.connect().await?;
104+
async fn default_channel(&self, addr: &str) -> Result<Endpoint> {
105+
let addr = "http://".to_string() + &SCHEME_REG.replace(addr, "");
106+
self.endpoint(addr)
107+
}
99108

100-
Ok(factory(ch))
109+
fn endpoint(&self, addr: String) -> Result<Endpoint> {
110+
let endpoint = Channel::from_shared(addr)?
111+
.tcp_keepalive(Some(Duration::from_secs(10)))
112+
.keep_alive_timeout(Duration::from_secs(3));
113+
Ok(endpoint)
101114
}
102115
}
103116

src/kv/key.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
use std::fmt;
44
use std::ops::Bound;
5-
use std::u8;
65

76
#[allow(unused_imports)]
87
#[cfg(test)]

src/kv/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
22
use std::fmt;
3-
use std::u8;
43

54
mod bound_range;
65
pub mod codec;

src/raw/client.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use core::ops::Range;
44
use std::str::FromStr;
55
use std::sync::Arc;
6-
use std::u32;
76

87
use futures::StreamExt;
98
use log::debug;

src/transaction/requests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ pub fn new_prewrite_request(
252252
req.start_version = start_version;
253253
req.lock_ttl = lock_ttl;
254254
// FIXME: Lite resolve lock is currently disabled
255-
req.txn_size = std::u64::MAX;
255+
req.txn_size = u64::MAX;
256256

257257
req
258258
}

0 commit comments

Comments
 (0)