1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use r2d2::{Builder, ManageConnection, Pool};
use std::cell::RefCell;
use std::error::Error;
use std::io;
use std::io::Write;
use authenticators::Authenticator;
use cluster::NodeTcpConfig;
use compression::Compression;
use error;
use frame::parser::parse_frame;
use frame::{Frame, IntoBytes, Opcode};
use transport::{CDRSTransport, TransportTcp};
pub type TcpConnectionPool<A> = Pool<TcpConnectionsManager<A>>;
pub fn new_tcp_pool<A: Authenticator + Send + Sync + 'static>(
node_config: NodeTcpConfig<'static, A>,
) -> error::Result<TcpConnectionPool<A>> {
let manager = TcpConnectionsManager::new(node_config.addr, node_config.authenticator);
Builder::new()
.max_size(node_config.max_size)
.min_idle(node_config.min_idle)
.max_lifetime(node_config.max_lifetime)
.idle_timeout(node_config.idle_timeout)
.connection_timeout(node_config.connection_timeout)
.build(manager)
.map_err(|err| error::Error::from(err.description()))
}
pub struct TcpConnectionsManager<A> {
addr: &'static str,
auth: A,
}
impl<A> TcpConnectionsManager<A> {
pub fn new(addr: &'static str, auth: A) -> Self {
TcpConnectionsManager { addr, auth }
}
}
impl<A: Authenticator + 'static + Send + Sync> ManageConnection for TcpConnectionsManager<A> {
type Connection = RefCell<TransportTcp>;
type Error = error::Error;
fn connect(&self) -> Result<Self::Connection, Self::Error> {
let transport = RefCell::new(TransportTcp::new(&self.addr)?);
startup(&transport, &self.auth)?;
Ok(transport)
}
fn is_valid(&self, conn: &mut Self::Connection) -> Result<(), Self::Error> {
let options_frame = Frame::new_req_options().into_cbytes();
try!(conn.borrow_mut().write(options_frame.as_slice()));
parse_frame(conn, &Compression::None {}).map(|_| ())
}
fn has_broken(&self, conn: &mut Self::Connection) -> bool {
conn.borrow().is_alive()
}
}
pub fn startup<'b, T: CDRSTransport + 'static, A: Authenticator + 'static + Sized>(
transport: &RefCell<T>,
session_authenticator: &'b A,
) -> error::Result<()> {
let ref mut compression = Compression::None;
let startup_frame = Frame::new_req_startup(compression.as_str()).into_cbytes();
transport.borrow_mut().write(startup_frame.as_slice())?;
let start_response = try!(parse_frame(transport, compression));
if start_response.opcode == Opcode::Ready {
return Ok(());
}
if start_response.opcode == Opcode::Authenticate {
let body = start_response.get_body()?;
let authenticator = body.get_authenticator().expect(
"Cassandra Server did communicate that it neededs
authentication but the auth schema was missing in the body response",
);
let auth_check = session_authenticator
.get_cassandra_name()
.ok_or(error::Error::General(
"No authenticator was provided".to_string(),
))
.map(|auth| {
if authenticator != auth {
let io_err = io::Error::new(
io::ErrorKind::NotFound,
format!(
"Unsupported type of authenticator. {:?} got,
but {} is supported.",
authenticator, auth
),
);
return Err(error::Error::Io(io_err));
}
Ok(())
});
if let Err(err) = auth_check {
return Err(err);
}
let auth_token_bytes = session_authenticator.get_auth_token().into_cbytes();
try!(
transport.borrow_mut().write(
Frame::new_req_auth_response(auth_token_bytes)
.into_cbytes()
.as_slice()
)
);
try!(parse_frame(transport, compression));
return Ok(());
}
unreachable!();
}