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
use error::MemcacheError;
use std::io;
use std::io::{Read, Write};
use std::net::TcpStream;
#[cfg(unix)]
use std::os::unix::net::UnixStream;
use std::time::Duration;
use udp_stream::UdpStream;
#[cfg(unix)]
use url::Host;
use url::Url;
enum Stream {
TcpStream(TcpStream),
UdpSocket(UdpStream),
#[cfg(unix)]
UnixStream(UnixStream),
}
pub struct Connection {
stream: Stream,
pub url: String,
}
impl Connection {
pub fn connect(addr: &str) -> Result<Self, MemcacheError> {
let addr = match Url::parse(addr) {
Ok(v) => v,
Err(_) => return Err(MemcacheError::ClientError("Invalid memcache URL".into())),
};
if addr.scheme() != "memcache" {
return Err(MemcacheError::ClientError(
"memcache URL should start with 'memcache://'".into(),
));
}
let is_udp = addr
.query_pairs()
.any(|(ref k, ref v)| k == "udp" && v == "true");
if is_udp {
let udp_stream = Stream::UdpSocket(UdpStream::new(addr.clone())?);
return Ok(Connection {
url: addr.into_string(),
stream: udp_stream,
});
}
#[cfg(unix)]
{
if addr.host() == Some(Host::Domain("")) && addr.port() == None {
let stream = UnixStream::connect(addr.path())?;
return Ok(Connection {
url: addr.into_string(),
stream: Stream::UnixStream(stream),
});
}
}
let stream = TcpStream::connect(addr.clone())?;
let disable_tcp_nodelay = addr
.query_pairs()
.any(|(ref k, ref v)| k == "tcp_nodelay" && v == "false");
if !disable_tcp_nodelay {
stream.set_nodelay(true)?;
}
let timeout = addr.query_pairs()
.find(|&(ref k, ref _v)| k == "timeout")
.and_then(|(ref _k, ref v)| v.parse::<u64>().ok())
.map(Duration::from_secs);
if timeout.is_some() {
stream.set_read_timeout(timeout)?;
stream.set_write_timeout(timeout)?;
}
return Ok(Connection {
url: addr.into_string(),
stream: Stream::TcpStream(stream),
});
}
pub(crate) fn set_read_timeout(&mut self, timeout: Option<Duration>) -> Result<(), MemcacheError> {
if let Stream::TcpStream(ref mut conn) = self.stream {
conn.set_read_timeout(timeout)?;
}
Ok(())
}
pub(crate) fn set_write_timeout(&mut self, timeout: Option<Duration>) -> Result<(), MemcacheError> {
if let Stream::TcpStream(ref mut conn) = self.stream {
conn.set_write_timeout(timeout)?;
}
Ok(())
}
}
impl Read for Connection {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
match self.stream {
Stream::TcpStream(ref mut stream) => stream.read(buf),
Stream::UdpSocket(ref mut stream) => stream.read(buf),
#[cfg(unix)]
Stream::UnixStream(ref mut stream) => stream.read(buf),
}
}
}
impl Write for Connection {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
match self.stream {
Stream::TcpStream(ref mut stream) => stream.write(buf),
Stream::UdpSocket(ref mut stream) => stream.write(buf),
#[cfg(unix)]
Stream::UnixStream(ref mut stream) => stream.write(buf),
}
}
fn flush(&mut self) -> io::Result<()> {
match self.stream {
Stream::TcpStream(ref mut stream) => stream.flush(),
Stream::UdpSocket(ref mut stream) => stream.flush(),
#[cfg(unix)]
Stream::UnixStream(ref mut stream) => stream.flush(),
}
}
}
#[cfg(test)]
mod tests {
#[test]
fn tcp_nodelay() {
super::Connection::connect("memcache://localhost:12345?tcp_nodelay=true").unwrap();
}
}