Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WASI suuport. #325

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ io-enum = "1.0.0"
flate2 = { version = "1.0", default-features = false }
lru = "0.7"
mysql_common = { version = "0.28.0", default-features = false }
socket2 = "0.4"
once_cell = "1.7.2"
pem = "1.0.1"
percent-encoding = "2.1.0"
Expand Down Expand Up @@ -94,3 +93,9 @@ named_pipe = "~0.4"

[target.'cfg(unix)'.dependencies]
libc = "0.2"

[target.'cfg(not(target_os = "wasi"))'.dependencies]
socket2 = "0.4"

[target.'cfg(target_os = "wasi")'.dependencies]
wasmedge_wasi_socket = { git = "https://github.com/second-state/wasmedge_wasi_socket" }
70 changes: 55 additions & 15 deletions src/conn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ use mysql_common::{
packets::SslRequest,
};

#[cfg(not(target_os = "wasi"))]
use std::process;
use std::{
borrow::{Borrow, Cow},
cmp,
Expand All @@ -36,7 +38,6 @@ use std::{
io::{self, Write as _},
mem,
ops::{Deref, DerefMut},
process,
sync::Arc,
};

Expand Down Expand Up @@ -406,33 +407,56 @@ impl Conn {
let tcp_nodelay = opts.get_tcp_nodelay();
let tcp_connect_timeout = opts.get_tcp_connect_timeout();
let bind_address = opts.bind_address().cloned();
let stream = if let Some(socket) = opts.get_socket() {
Stream::connect_socket(&*socket, read_timeout, write_timeout)?
} else {
#[cfg(not(target_os = "wasi"))]
{
let stream = if let Some(socket) = opts.get_socket() {
Stream::connect_socket(&*socket, read_timeout, write_timeout)?
} else {
let port = opts.get_tcp_port();
let ip_or_hostname = match opts.get_host() {
url::Host::Domain(domain) => domain,
url::Host::Ipv4(ip) => ip.to_string(),
url::Host::Ipv6(ip) => ip.to_string(),
};
Stream::connect_tcp(
&*ip_or_hostname,
port,
read_timeout,
write_timeout,
tcp_keepalive_time,
#[cfg(any(target_os = "linux", target_os = "macos",))]
tcp_keepalive_probe_interval_secs,
#[cfg(any(target_os = "linux", target_os = "macos",))]
tcp_keepalive_probe_count,
#[cfg(target_os = "linux")]
tcp_user_timeout,
tcp_nodelay,
tcp_connect_timeout,
bind_address,
)?
};
self.0.stream = Some(MySyncFramed::new(stream));
}
#[cfg(target_os = "wasi")]
{
let port = opts.get_tcp_port();
let ip_or_hostname = match opts.get_host() {
url::Host::Domain(domain) => domain,
url::Host::Ipv4(ip) => ip.to_string(),
url::Host::Ipv6(ip) => ip.to_string(),
};
Stream::connect_tcp(
let stream = Stream::connect_tcp(
&*ip_or_hostname,
port,
read_timeout,
write_timeout,
tcp_keepalive_time,
#[cfg(any(target_os = "linux", target_os = "macos",))]
tcp_keepalive_probe_interval_secs,
#[cfg(any(target_os = "linux", target_os = "macos",))]
tcp_keepalive_probe_count,
#[cfg(target_os = "linux")]
tcp_user_timeout,
tcp_nodelay,
tcp_connect_timeout,
bind_address,
)?
};
self.0.stream = Some(MySyncFramed::new(stream));
)?;
self.0.stream = Some(MySyncFramed::new(stream));
}
Ok(())
}

Expand Down Expand Up @@ -650,7 +674,10 @@ impl Conn {
attrs.insert("_client_name".into(), "rust-mysql-simple".into());
attrs.insert("_client_version".into(), env!("CARGO_PKG_VERSION").into());
attrs.insert("_os".into(), env!("CARGO_CFG_TARGET_OS").into());
#[cfg(not(target_os = "wasi"))]
attrs.insert("_pid".into(), process::id().to_string());
#[cfg(target_os = "wasi")]
attrs.insert("_pid".into(), "66666".into());
attrs.insert("_platform".into(), env!("CARGO_CFG_TARGET_ARCH").into());
attrs.insert("program_name".into(), program_name);

Expand Down Expand Up @@ -1389,6 +1416,7 @@ mod test {
assert_eq!(db_name, DB_NAME);
}

#[cfg(not(target_os = "wasi"))]
#[test]
fn should_connect_by_hostname() {
let opts = OptsBuilder::from_opts(get_opts()).ip_or_hostname(Some("localhost"));
Expand Down Expand Up @@ -1475,6 +1503,7 @@ mod test {
);
}

#[cfg(not(target_os = "wasi"))]
#[test]
fn should_parse_large_text_result() {
let mut conn = Conn::new(get_opts()).unwrap();
Expand Down Expand Up @@ -1538,6 +1567,7 @@ mod test {
assert_eq!(rows, vec![row1, row2]);
}

#[cfg(not(target_os = "wasi"))]
#[test]
fn should_parse_large_binary_result() {
let mut conn = Conn::new(get_opts()).unwrap();
Expand Down Expand Up @@ -1714,6 +1744,7 @@ mod test {
);
}

#[cfg(not(target_os = "wasi"))]
#[test]
fn should_connect_via_socket_for_127_0_0_1() {
let opts = OptsBuilder::from_opts(get_opts());
Expand All @@ -1723,6 +1754,7 @@ mod test {
}
}

#[cfg(not(target_os = "wasi"))]
#[test]
fn should_connect_via_socket_localhost() {
let opts = OptsBuilder::from_opts(get_opts()).ip_or_hostname(Some("localhost"));
Expand All @@ -1735,6 +1767,7 @@ mod test {
/// QueryResult::drop hangs on connectivity errors (see [blackbeam/rust-mysql-simple#306][1]).
///
/// [1]: https://github.com/blackbeam/rust-mysql-simple/issues/306
#[cfg(not(target_os = "wasi"))]
#[test]
fn issue_306() {
let (tx, rx) = channel::<()>();
Expand Down Expand Up @@ -1884,6 +1917,7 @@ mod test {
.unwrap();
}

#[cfg(not(target_os = "wasi"))]
#[test]
fn issue_285() {
let (tx, rx) = sync_channel::<()>(0);
Expand Down Expand Up @@ -1962,6 +1996,7 @@ mod test {
}
}

#[cfg(not(target_os = "wasi"))]
#[test]
fn should_handle_tcp_connect_timeout() {
use crate::error::{DriverError::ConnectTimeout, Error::DriverError};
Expand Down Expand Up @@ -1999,6 +2034,7 @@ mod test {
assert_eq!(result.affected_rows(), 1);
}

#[cfg(not(target_os = "wasi"))]
#[test]
fn should_bind_before_connect() {
let port = 28000 + (rand::random::<u16>() % 2000);
Expand All @@ -2017,6 +2053,7 @@ mod test {
);
}

#[cfg(not(target_os = "wasi"))]
#[test]
fn should_bind_before_connect_with_timeout() {
let port = 30000 + (rand::random::<u16>() % 2000);
Expand Down Expand Up @@ -2177,8 +2214,10 @@ mod test {
);
}
}

#[cfg(not(target_os = "wasi"))]
let pid = process::id().to_string();
#[cfg(target_os = "wasi")]
let pid = "66666".to_string();
let progname = std::env::args_os()
.next()
.unwrap()
Expand Down Expand Up @@ -2211,6 +2250,7 @@ mod test {
}
}

#[cfg(not(target_os = "wasi"))]
#[test]
fn should_read_binlog() -> crate::Result<()> {
use std::{
Expand Down
1 change: 1 addition & 0 deletions src/conn/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ impl Queryable for PooledConn {
}
}

#[cfg(not(target_os = "wasi"))]
#[cfg(test)]
#[allow(non_snake_case)]
mod test {
Expand Down
20 changes: 15 additions & 5 deletions src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ use io_enum::*;
#[cfg(windows)]
use named_pipe as np;

#[cfg(not(target_os = "wasi"))]
use std::net::{self, SocketAddr};
#[cfg(unix)]
use std::os::{
unix,
unix::io::{AsRawFd, RawFd},
};
use std::{
fmt, io,
net::{self, SocketAddr},
time::Duration,
};
use std::{fmt, io, time::Duration};

#[cfg(target_os = "wasi")]
use wasmedge_wasi_socket::{self, SocketAddr};

use crate::error::{
DriverError::{ConnectTimeout, CouldNotConnect},
Expand Down Expand Up @@ -141,13 +142,19 @@ impl Stream {
}
}

#[cfg(not(target_os = "wasi"))]
pub fn is_socket(&self) -> bool {
match self {
Stream::SocketStream(_) => true,
_ => false,
}
}

#[cfg(target_os = "wasi")]
pub fn is_socket(&self) -> bool {
false
}

#[cfg(all(not(feature = "native-tls"), not(feature = "rustls")))]
pub fn make_secure(self, _host: url::Host, _ssl_opts: crate::SslOpts) -> MyResult<Stream> {
panic!(
Expand All @@ -173,7 +180,10 @@ pub enum TcpStream {
Secure(BufStream<native_tls::TlsStream<net::TcpStream>>),
#[cfg(feature = "rustls")]
Secure(BufStream<rustls::StreamOwned<rustls::ClientConnection, net::TcpStream>>),
#[cfg(not(target_os = "wasi"))]
Insecure(BufStream<net::TcpStream>),
#[cfg(target_os = "wasi")]
Insecure(BufStream<wasmedge_wasi_socket::TcpStream>),
}

#[cfg(unix)]
Expand Down
Loading