forked from proxmox-mirrors/proxmox-backup
examples: drop hyper server/client examples
Those are left over from early development experimenting but as they have nothing to do with PBS itself this is definitively the wrong place. As they are preserved in the git history forever anyway, just delete them here completely. Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
parent
63ece39a17
commit
ceb6690cdd
@ -1,109 +0,0 @@
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
use std::task::{Context, Poll};
|
||||
|
||||
use anyhow::Error;
|
||||
use futures::future::TryFutureExt;
|
||||
use futures::stream::Stream;
|
||||
use tokio::net::TcpStream;
|
||||
|
||||
// Simple H2 client to test H2 download speed using h2server.rs
|
||||
|
||||
struct Process {
|
||||
body: h2::RecvStream,
|
||||
trailers: bool,
|
||||
bytes: usize,
|
||||
}
|
||||
|
||||
impl Future for Process {
|
||||
type Output = Result<usize, Error>;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
||||
let this = self.get_mut();
|
||||
|
||||
loop {
|
||||
if this.trailers {
|
||||
match futures::ready!(this.body.poll_trailers(cx)) {
|
||||
Ok(Some(trailers)) => println!("trailers: {:?}", trailers),
|
||||
Ok(None) => (),
|
||||
Err(err) => return Poll::Ready(Err(Error::from(err))),
|
||||
}
|
||||
|
||||
println!("Received {} bytes", this.bytes);
|
||||
|
||||
return Poll::Ready(Ok(this.bytes));
|
||||
} else {
|
||||
match futures::ready!(Pin::new(&mut this.body).poll_next(cx)) {
|
||||
Some(Ok(chunk)) => {
|
||||
this.body.flow_control().release_capacity(chunk.len())?;
|
||||
this.bytes += chunk.len();
|
||||
// println!("GOT FRAME {}", chunk.len());
|
||||
}
|
||||
Some(Err(err)) => return Poll::Ready(Err(Error::from(err))),
|
||||
None => {
|
||||
this.trailers = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn send_request(
|
||||
mut client: h2::client::SendRequest<bytes::Bytes>,
|
||||
) -> impl Future<Output = Result<usize, Error>> {
|
||||
println!("sending request");
|
||||
|
||||
let request = hyper::http::Request::builder()
|
||||
.uri("http://localhost/")
|
||||
.body(())
|
||||
.unwrap();
|
||||
|
||||
let (response, _stream) = client.send_request(request, true).unwrap();
|
||||
|
||||
response.map_err(Error::from).and_then(|response| Process {
|
||||
body: response.into_body(),
|
||||
trailers: false,
|
||||
bytes: 0,
|
||||
})
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Error> {
|
||||
proxmox_async::runtime::main(run())
|
||||
}
|
||||
|
||||
async fn run() -> Result<(), Error> {
|
||||
let start = std::time::SystemTime::now();
|
||||
|
||||
let conn = TcpStream::connect(std::net::SocketAddr::from(([127, 0, 0, 1], 8008))).await?;
|
||||
conn.set_nodelay(true).unwrap();
|
||||
|
||||
let (client, h2) = h2::client::Builder::new()
|
||||
.initial_connection_window_size(1024 * 1024 * 1024)
|
||||
.initial_window_size(1024 * 1024 * 1024)
|
||||
.max_frame_size(4 * 1024 * 1024)
|
||||
.handshake(conn)
|
||||
.await?;
|
||||
|
||||
tokio::spawn(async move {
|
||||
if let Err(err) = h2.await {
|
||||
println!("GOT ERR={:?}", err);
|
||||
}
|
||||
});
|
||||
|
||||
let mut bytes = 0;
|
||||
for _ in 0..2000 {
|
||||
bytes += send_request(client.clone()).await?;
|
||||
}
|
||||
|
||||
let elapsed = start.elapsed().unwrap();
|
||||
let elapsed = (elapsed.as_secs() as f64) + (elapsed.subsec_millis() as f64) / 1000.0;
|
||||
|
||||
println!(
|
||||
"Downloaded {} bytes, {} MB/s",
|
||||
bytes,
|
||||
(bytes as f64) / (elapsed * 1024.0 * 1024.0)
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@ -1,125 +0,0 @@
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
use std::task::{Context, Poll};
|
||||
|
||||
use anyhow::{format_err, Error};
|
||||
use futures::future::TryFutureExt;
|
||||
use futures::stream::Stream;
|
||||
use tokio::net::TcpStream;
|
||||
|
||||
// Simple H2 client to test H2 download speed using h2s-server.rs
|
||||
|
||||
struct Process {
|
||||
body: h2::RecvStream,
|
||||
trailers: bool,
|
||||
bytes: usize,
|
||||
}
|
||||
|
||||
impl Future for Process {
|
||||
type Output = Result<usize, Error>;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
||||
let this = self.get_mut();
|
||||
|
||||
loop {
|
||||
if this.trailers {
|
||||
match futures::ready!(this.body.poll_trailers(cx)) {
|
||||
Ok(Some(trailers)) => println!("trailers: {:?}", trailers),
|
||||
Ok(None) => (),
|
||||
Err(err) => return Poll::Ready(Err(Error::from(err))),
|
||||
}
|
||||
|
||||
println!("Received {} bytes", this.bytes);
|
||||
|
||||
return Poll::Ready(Ok(this.bytes));
|
||||
} else {
|
||||
match futures::ready!(Pin::new(&mut this.body).poll_next(cx)) {
|
||||
Some(Ok(chunk)) => {
|
||||
this.body.flow_control().release_capacity(chunk.len())?;
|
||||
this.bytes += chunk.len();
|
||||
// println!("GOT FRAME {}", chunk.len());
|
||||
}
|
||||
Some(Err(err)) => return Poll::Ready(Err(Error::from(err))),
|
||||
None => {
|
||||
this.trailers = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn send_request(
|
||||
mut client: h2::client::SendRequest<bytes::Bytes>,
|
||||
) -> impl Future<Output = Result<usize, Error>> {
|
||||
println!("sending request");
|
||||
|
||||
let request = hyper::http::Request::builder()
|
||||
.uri("http://localhost/")
|
||||
.body(())
|
||||
.unwrap();
|
||||
|
||||
let (response, _stream) = client.send_request(request, true).unwrap();
|
||||
|
||||
response.map_err(Error::from).and_then(|response| Process {
|
||||
body: response.into_body(),
|
||||
trailers: false,
|
||||
bytes: 0,
|
||||
})
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Error> {
|
||||
proxmox_async::runtime::main(run())
|
||||
}
|
||||
|
||||
async fn run() -> Result<(), Error> {
|
||||
let start = std::time::SystemTime::now();
|
||||
|
||||
let conn = TcpStream::connect(std::net::SocketAddr::from(([127, 0, 0, 1], 8008))).await?;
|
||||
conn.set_nodelay(true).unwrap();
|
||||
|
||||
use openssl::ssl::{SslConnector, SslMethod};
|
||||
|
||||
let mut ssl_connector_builder = SslConnector::builder(SslMethod::tls()).unwrap();
|
||||
ssl_connector_builder.set_verify(openssl::ssl::SslVerifyMode::NONE);
|
||||
let ssl = ssl_connector_builder
|
||||
.build()
|
||||
.configure()?
|
||||
.into_ssl("localhost")?;
|
||||
|
||||
let conn = tokio_openssl::SslStream::new(ssl, conn)?;
|
||||
let mut conn = Box::pin(conn);
|
||||
conn.as_mut()
|
||||
.connect()
|
||||
.await
|
||||
.map_err(|err| format_err!("connect failed - {}", err))?;
|
||||
|
||||
let (client, h2) = h2::client::Builder::new()
|
||||
.initial_connection_window_size(1024 * 1024 * 1024)
|
||||
.initial_window_size(1024 * 1024 * 1024)
|
||||
.max_frame_size(4 * 1024 * 1024)
|
||||
.handshake(conn)
|
||||
.await?;
|
||||
|
||||
tokio::spawn(async move {
|
||||
if let Err(err) = h2.await {
|
||||
println!("GOT ERR={:?}", err);
|
||||
}
|
||||
});
|
||||
|
||||
let mut bytes = 0;
|
||||
for _ in 0..2000 {
|
||||
bytes += send_request(client.clone()).await?;
|
||||
}
|
||||
|
||||
let elapsed = start.elapsed().unwrap();
|
||||
let elapsed = (elapsed.as_secs() as f64) + (elapsed.subsec_millis() as f64) / 1000.0;
|
||||
|
||||
println!(
|
||||
"Downloaded {} bytes, {} MB/s",
|
||||
bytes,
|
||||
(bytes as f64) / (elapsed * 1024.0 * 1024.0)
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@ -1,84 +0,0 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::{format_err, Error};
|
||||
use bytes::Bytes;
|
||||
use futures::{future, FutureExt, TryFutureExt};
|
||||
use http_body_util::Full;
|
||||
use hyper::{body::Incoming, Request, Response};
|
||||
use hyper_util::rt::{TokioExecutor, TokioIo};
|
||||
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
|
||||
use tokio::net::{TcpListener, TcpStream};
|
||||
|
||||
use pbs_buildcfg::configdir;
|
||||
|
||||
fn main() -> Result<(), Error> {
|
||||
proxmox_async::runtime::main(run())
|
||||
}
|
||||
|
||||
async fn run() -> Result<(), Error> {
|
||||
let key_path = configdir!("/proxy.key");
|
||||
let cert_path = configdir!("/proxy.pem");
|
||||
|
||||
let mut acceptor = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
|
||||
acceptor
|
||||
.set_private_key_file(key_path, SslFiletype::PEM)
|
||||
.map_err(|err| format_err!("unable to read proxy key {} - {}", key_path, err))?;
|
||||
acceptor
|
||||
.set_certificate_chain_file(cert_path)
|
||||
.map_err(|err| format_err!("unable to read proxy cert {} - {}", cert_path, err))?;
|
||||
acceptor.check_private_key().unwrap();
|
||||
|
||||
let acceptor = Arc::new(acceptor.build());
|
||||
|
||||
let listener = TcpListener::bind(std::net::SocketAddr::from(([127, 0, 0, 1], 8008))).await?;
|
||||
|
||||
println!("listening on {:?}", listener.local_addr());
|
||||
|
||||
loop {
|
||||
let (socket, _addr) = listener.accept().await?;
|
||||
tokio::spawn(handle_connection(socket, Arc::clone(&acceptor)).map(|res| {
|
||||
if let Err(err) = res {
|
||||
eprintln!("Error: {}", err);
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_connection(socket: TcpStream, acceptor: Arc<SslAcceptor>) -> Result<(), Error> {
|
||||
socket.set_nodelay(true).unwrap();
|
||||
|
||||
let ssl = openssl::ssl::Ssl::new(acceptor.context())?;
|
||||
let stream = tokio_openssl::SslStream::new(ssl, socket)?;
|
||||
let mut stream = Box::pin(stream);
|
||||
|
||||
stream.as_mut().accept().await?;
|
||||
|
||||
let mut http = hyper::server::conn::http2::Builder::new(TokioExecutor::new());
|
||||
// increase window size: todo - find optiomal size
|
||||
let max_window_size = (1 << 31) - 2;
|
||||
http.initial_stream_window_size(max_window_size);
|
||||
http.initial_connection_window_size(max_window_size);
|
||||
|
||||
let service = hyper::service::service_fn(|_req: Request<Incoming>| {
|
||||
println!("Got request");
|
||||
let buffer = vec![65u8; 4 * 1024 * 1024]; // nonsense [A,A,A,A...]
|
||||
let body = Full::<Bytes>::from(buffer);
|
||||
|
||||
let response = Response::builder()
|
||||
.status(hyper::http::StatusCode::OK)
|
||||
.header(
|
||||
hyper::http::header::CONTENT_TYPE,
|
||||
"application/octet-stream",
|
||||
)
|
||||
.body(body)
|
||||
.unwrap();
|
||||
future::ok::<_, Error>(response)
|
||||
});
|
||||
|
||||
http.serve_connection(TokioIo::new(stream), service)
|
||||
.map_err(Error::from)
|
||||
.await?;
|
||||
|
||||
println!("H2 connection CLOSE !");
|
||||
Ok(())
|
||||
}
|
||||
@ -1,60 +0,0 @@
|
||||
use anyhow::Error;
|
||||
use bytes::Bytes;
|
||||
use futures::*;
|
||||
use http_body_util::Full;
|
||||
use hyper::{body::Incoming, Request, Response};
|
||||
|
||||
use hyper_util::rt::{TokioExecutor, TokioIo};
|
||||
use tokio::net::{TcpListener, TcpStream};
|
||||
|
||||
fn main() -> Result<(), Error> {
|
||||
proxmox_async::runtime::main(run())
|
||||
}
|
||||
|
||||
async fn run() -> Result<(), Error> {
|
||||
let listener = TcpListener::bind(std::net::SocketAddr::from(([127, 0, 0, 1], 8008))).await?;
|
||||
|
||||
println!("listening on {:?}", listener.local_addr());
|
||||
|
||||
loop {
|
||||
let (socket, _addr) = listener.accept().await?;
|
||||
tokio::spawn(handle_connection(socket).map(|res| {
|
||||
if let Err(err) = res {
|
||||
eprintln!("Error: {}", err);
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_connection(socket: TcpStream) -> Result<(), Error> {
|
||||
socket.set_nodelay(true).unwrap();
|
||||
|
||||
let mut http = hyper::server::conn::http2::Builder::new(TokioExecutor::new());
|
||||
// increase window size: todo - find optiomal size
|
||||
let max_window_size = (1 << 31) - 2;
|
||||
http.initial_stream_window_size(max_window_size);
|
||||
http.initial_connection_window_size(max_window_size);
|
||||
|
||||
let service = hyper::service::service_fn(|_req: Request<Incoming>| {
|
||||
println!("Got request");
|
||||
let buffer = vec![65u8; 4 * 1024 * 1024]; // nonsense [A,A,A,A...]
|
||||
let body = Full::<Bytes>::from(buffer);
|
||||
|
||||
let response = Response::builder()
|
||||
.status(hyper::http::StatusCode::OK)
|
||||
.header(
|
||||
hyper::http::header::CONTENT_TYPE,
|
||||
"application/octet-stream",
|
||||
)
|
||||
.body(body)
|
||||
.unwrap();
|
||||
future::ok::<_, Error>(response)
|
||||
});
|
||||
|
||||
http.serve_connection(TokioIo::new(socket), service)
|
||||
.map_err(Error::from)
|
||||
.await?;
|
||||
|
||||
println!("H2 connection CLOSE !");
|
||||
Ok(())
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user