1
0
mirror of https://github.com/actix/examples synced 2025-06-28 09:50:36 +02:00

upgrade to 2.0 alpha.3

This commit is contained in:
Nikolay Kim
2019-12-07 23:59:24 +06:00
parent e7f7175b7b
commit 3127352797
100 changed files with 1075 additions and 1107 deletions

View File

@ -1,18 +1,18 @@
[package]
name = "async_db"
version = "0.1.0"
version = "2.0.0"
authors = ["Darin Gordon <dkcdkg@gmail.com>"]
edition = "2018"
workspace = ".."
[dependencies]
actix-rt = "0.2.2"
actix-web = "1.0.0"
actix-rt = "1.0.0-alpha.3"
actix-web = "2.0.0-alpha.3"
dotenv = "0.10"
env_logger = "0.5"
env_logger = "0.6"
failure = "0.1.1"
futures = "0.1"
futures = "0.3.1"
num_cpus = "1.10.0"
r2d2 = "0.8.2"
r2d2_sqlite = "0.8.0"

View File

@ -1,6 +1,6 @@
use actix_web::{web, Error as AWError};
use failure::Error;
use futures::Future;
use futures::{Future, TryFutureExt};
use r2d2;
use r2d2_sqlite;
use rusqlite::NO_PARAMS;
@ -26,7 +26,7 @@ pub enum Queries {
pub fn execute(
pool: &Pool,
query: Queries,
) -> impl Future<Item = Vec<WeatherAgg>, Error = AWError> {
) -> impl Future<Output = Result<Vec<WeatherAgg>, AWError>> {
let pool = pool.clone();
web::block(move || match query {
Queries::GetTopTenHottestYears => get_hottest_years(pool.get()?),
@ -34,7 +34,7 @@ pub fn execute(
Queries::GetTopTenHottestMonths => get_hottest_months(pool.get()?),
Queries::GetTopTenColdestMonths => get_coldest_months(pool.get()?),
})
.from_err()
.map_err(AWError::from)
}
fn get_hottest_years(conn: Connection) -> Result<Vec<WeatherAgg>, Error> {

View File

@ -14,64 +14,42 @@ This project illustrates two examples:
use std::io;
use actix_web::{middleware, web, App, Error as AWError, HttpResponse, HttpServer};
use futures::future::{join_all, ok as fut_ok, Future};
use r2d2_sqlite;
use r2d2_sqlite::SqliteConnectionManager;
use futures::future::join_all;
use r2d2_sqlite::{self, SqliteConnectionManager};
mod db;
use db::{Pool, Queries, WeatherAgg};
use db::{Pool, Queries};
/// Version 1: Calls 4 queries in sequential order, as an asynchronous handler
fn asyncio_weather(
db: web::Data<Pool>,
) -> impl Future<Item = HttpResponse, Error = AWError> {
let mut result: Vec<Vec<WeatherAgg>> = vec![];
async fn asyncio_weather(db: web::Data<Pool>) -> Result<HttpResponse, AWError> {
let result = vec![
db::execute(&db, Queries::GetTopTenHottestYears).await?,
db::execute(&db, Queries::GetTopTenColdestYears).await?,
db::execute(&db, Queries::GetTopTenHottestMonths).await?,
db::execute(&db, Queries::GetTopTenColdestMonths).await?,
];
db::execute(&db, Queries::GetTopTenHottestYears)
.from_err()
.and_then(move |res| {
result.push(res);
db::execute(&db, Queries::GetTopTenColdestYears)
.from_err()
.and_then(move |res| {
result.push(res);
db::execute(&db, Queries::GetTopTenHottestMonths)
.from_err()
.and_then(move |res| {
result.push(res);
db::execute(&db, Queries::GetTopTenColdestMonths)
.from_err()
.and_then(move |res| {
result.push(res);
fut_ok(result)
})
})
})
})
.and_then(|res| Ok(HttpResponse::Ok().json(res)))
Ok(HttpResponse::Ok().json(result))
}
/// Version 2: Calls 4 queries in parallel, as an asynchronous handler
/// Returning Error types turn into None values in the response
fn parallel_weather(
db: web::Data<Pool>,
) -> impl Future<Item = HttpResponse, Error = AWError> {
async fn parallel_weather(db: web::Data<Pool>) -> Result<HttpResponse, AWError> {
let fut_result = vec![
Box::new(db::execute(&db, Queries::GetTopTenHottestYears)),
Box::new(db::execute(&db, Queries::GetTopTenColdestYears)),
Box::new(db::execute(&db, Queries::GetTopTenHottestMonths)),
Box::new(db::execute(&db, Queries::GetTopTenColdestMonths)),
Box::pin(db::execute(&db, Queries::GetTopTenHottestYears)),
Box::pin(db::execute(&db, Queries::GetTopTenColdestYears)),
Box::pin(db::execute(&db, Queries::GetTopTenHottestMonths)),
Box::pin(db::execute(&db, Queries::GetTopTenColdestMonths)),
];
let result: Result<Vec<_>, _> = join_all(fut_result).await.into_iter().collect();
join_all(fut_result)
.map_err(AWError::from)
.map(|result| HttpResponse::Ok().json(result))
Ok(HttpResponse::Ok().json(result.map_err(AWError::from)?))
}
fn main() -> io::Result<()> {
#[actix_rt::main]
async fn main() -> io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init();
let sys = actix_rt::System::new("parallel_db_example");
// Start N db executor actors (N = number of cores avail)
let manager = SqliteConnectionManager::file("weather.db");
@ -80,20 +58,18 @@ fn main() -> io::Result<()> {
// Start http server
HttpServer::new(move || {
App::new()
// store db pool as Data object
.data(pool.clone())
.wrap(middleware::Logger::default())
.service(
web::resource("/asyncio_weather")
.route(web::get().to_async(asyncio_weather)),
web::resource("/asyncio_weather").route(web::get().to(asyncio_weather)),
)
.service(
web::resource("/parallel_weather")
.route(web::get().to_async(parallel_weather)),
.route(web::get().to(parallel_weather)),
)
})
.bind("127.0.0.1:8080")?
.start();
println!("Started http server: 127.0.0.1:8080");
sys.run()
.start()
.await
}