Skip to content

Commit

Permalink
Merge branch 'master' into contributing-guide
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioBenitez authored Aug 17, 2024
2 parents 7d8bd8c + ef1cfa0 commit ede7447
Show file tree
Hide file tree
Showing 26 changed files with 203 additions and 140 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,14 @@ jobs:
- name: Install Native Dependencies (Windows)
if: matrix.platform.name == 'Windows'
run: |
curl -fsLS -o vcpkg.7z https://blob.rocket.rs/vcpkg-2019-07-05.7z
curl -fsLS -o vcpkg.7z https://blob.rocket.rs/vcpkg-2024-08-16.7z
7z x vcpkg.7z -y -bb0
xcopy .\vcpkg $env:VCPKG_INSTALLATION_ROOT /s /e /h /y /q
vcpkg integrate install
echo "VCPKGRS_DYNAMIC=1" >> "$env:GITHUB_ENV"
echo "VCPKG_ROOT=$env:VCPKG_INSTALLATION_ROOT" >> "$env:GITHUB_ENV"
echo "$env:VCPKG_INSTALLATION_ROOT\installed\x64-windows\lib" >> "$env:GITHUB_PATH"
echo "MYSQLCLIENT_VERSION=8.0.39" >> "$env:GITHUB_ENV"
- name: Install NASM (Windows)
if: matrix.platform.name == 'Windows'
Expand Down
24 changes: 9 additions & 15 deletions contrib/db_pools/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ sqlx_postgres = ["sqlx", "sqlx/postgres", "log"]
sqlx_sqlite = ["sqlx", "sqlx/sqlite", "log"]
sqlx_macros = ["sqlx/macros"]
# diesel features
diesel_postgres = ["diesel-async/postgres", "diesel-async/deadpool", "diesel", "deadpool_09"]
diesel_mysql = ["diesel-async/mysql", "diesel-async/deadpool", "diesel", "deadpool_09"]
diesel_postgres = ["diesel-async/postgres", "diesel-async/deadpool", "deadpool", "diesel"]
diesel_mysql = ["diesel-async/mysql", "diesel-async/deadpool", "deadpool", "diesel"]
# implicit features: mongodb

[dependencies.rocket]
Expand All @@ -39,27 +39,20 @@ default-features = false
path = "../codegen"
version = "0.1.0"

[dependencies.deadpool_09]
package = "deadpool"
version = "0.9.5"
[dependencies.deadpool]
version = "0.12.1"
default-features = false
features = ["rt_tokio_1", "managed"]
optional = true

[dependencies.deadpool-postgres]
version = "0.13.2"
version = "0.14"
default-features = false
features = ["rt_tokio_1"]
optional = true

[dependencies.deadpool]
version = "0.12.1"
default-features = false
features = ["rt_tokio_1", "managed"]
optional = true

[dependencies.deadpool-redis]
version = "0.15"
version = "0.16"
default-features = false
features = ["rt_tokio_1"]
optional = true
Expand All @@ -71,8 +64,9 @@ features = ["compat-3-0-0", "rustls-tls"]
optional = true

[dependencies.diesel-async]
version = "0.4.1"
version = "0.5.0"
default-features = false
features = ["async-connection-wrapper"]
optional = true

[dependencies.diesel]
Expand All @@ -81,7 +75,7 @@ default-features = false
optional = true

[dependencies.sqlx]
version = "0.7"
version = "0.8"
default-features = false
features = ["runtime-tokio-rustls"]
optional = true
Expand Down
3 changes: 3 additions & 0 deletions contrib/db_pools/lib/src/diesel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ pub use diesel_async::pg;
#[doc(inline)]
pub use diesel_async::pooled_connection::deadpool::Pool;

#[doc(inline)]
pub use diesel_async::async_connection_wrapper::AsyncConnectionWrapper;

#[doc(inline)]
#[cfg(feature = "diesel_mysql")]
pub use diesel_async::AsyncMysqlConnection;
Expand Down
6 changes: 3 additions & 3 deletions contrib/db_pools/lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@
//! Drivers have a varying degree of support for graceful shutdown, affected by
//! the `Type::init()` fairing on Rocket shutdown.
//!
//! ## `deadpool` (v0.13)
//! ## `deadpool` (v0.12)
//!
//! | Database | Feature | [`Pool`] Type | [`Connection`] Deref |
//! |----------|-----------------------------|-----------------------------|--------------------------------------|
//! | Postgres | `deadpool_postgres` (v0.13) | [`deadpool_postgres::Pool`] | [`deadpool_postgres::ClientWrapper`] |
//! | Redis | `deadpool_redis` (v0.15) | [`deadpool_redis::Pool`] | [`deadpool_redis::Connection`] |
//! | Postgres | `deadpool_postgres` (v0.14) | [`deadpool_postgres::Pool`] | [`deadpool_postgres::ClientWrapper`] |
//! | Redis | `deadpool_redis` (v0.16) | [`deadpool_redis::Pool`] | [`deadpool_redis::Connection`] |
//!
//! On shutdown, new connections are denied. Shutdown _does not_ wait for
//! connections to be returned.
Expand Down
53 changes: 6 additions & 47 deletions contrib/db_pools/lib/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ mod deadpool_postgres {
use deadpool::{Runtime, managed::{Manager, Pool, PoolError, Object}};
use super::{Duration, Error, Config, Figment};

#[cfg(feature = "diesel")]
use diesel_async::pooled_connection::AsyncDieselConnectionManager;

pub trait DeadManager: Manager + Sized + Send + Sync + 'static {
fn new(config: &Config) -> Result<Self, Self::Error>;
}
Expand All @@ -175,50 +178,6 @@ mod deadpool_postgres {
}
}

#[rocket::async_trait]
impl<M: DeadManager, C: From<Object<M>>> crate::Pool for Pool<M, C>
where M::Type: Send, C: Send + Sync + 'static, M::Error: std::error::Error
{
type Error = Error<PoolError<M::Error>>;

type Connection = C;

async fn init(figment: &Figment) -> Result<Self, Self::Error> {
let config: Config = figment.extract()?;
let manager = M::new(&config).map_err(|e| Error::Init(e.into()))?;

Pool::builder(manager)
.max_size(config.max_connections)
.wait_timeout(Some(Duration::from_secs(config.connect_timeout)))
.create_timeout(Some(Duration::from_secs(config.connect_timeout)))
.recycle_timeout(config.idle_timeout.map(Duration::from_secs))
.runtime(Runtime::Tokio1)
.build()
.map_err(|_| Error::Init(PoolError::NoRuntimeSpecified))
}

async fn get(&self) -> Result<Self::Connection, Self::Error> {
self.get().await.map_err(Error::Get)
}

async fn close(&self) {
<Pool<M, C>>::close(self)
}
}
}

// TODO: Remove when new release of diesel-async with deadpool 0.10 is out.
#[cfg(all(feature = "deadpool_09", any(feature = "diesel_postgres", feature = "diesel_mysql")))]
mod deadpool_old {
use deadpool_09::{managed::{Manager, Pool, PoolError, Object, BuildError}, Runtime};
use diesel_async::pooled_connection::AsyncDieselConnectionManager;

use super::{Duration, Error, Config, Figment};

pub trait DeadManager: Manager + Sized + Send + Sync + 'static {
fn new(config: &Config) -> Result<Self, Self::Error>;
}

#[cfg(feature = "diesel_postgres")]
impl DeadManager for AsyncDieselConnectionManager<diesel_async::AsyncPgConnection> {
fn new(config: &Config) -> Result<Self, Self::Error> {
Expand All @@ -237,13 +196,13 @@ mod deadpool_old {
impl<M: DeadManager, C: From<Object<M>>> crate::Pool for Pool<M, C>
where M::Type: Send, C: Send + Sync + 'static, M::Error: std::error::Error
{
type Error = Error<BuildError<M::Error>, PoolError<M::Error>>;
type Error = Error<PoolError<M::Error>>;

type Connection = C;

async fn init(figment: &Figment) -> Result<Self, Self::Error> {
let config: Config = figment.extract()?;
let manager = M::new(&config).map_err(|e| Error::Init(BuildError::Backend(e)))?;
let manager = M::new(&config).map_err(|e| Error::Init(e.into()))?;

Pool::builder(manager)
.max_size(config.max_connections)
Expand All @@ -252,7 +211,7 @@ mod deadpool_old {
.recycle_timeout(config.idle_timeout.map(Duration::from_secs))
.runtime(Runtime::Tokio1)
.build()
.map_err(Error::Init)
.map_err(|_| Error::Init(PoolError::NoRuntimeSpecified))
}

async fn get(&self) -> Result<Self::Connection, Self::Error> {
Expand Down
9 changes: 4 additions & 5 deletions contrib/sync_db_pools/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ diesel_postgres_pool = ["diesel/postgres", "diesel/r2d2"]
diesel_mysql_pool = ["diesel/mysql", "diesel/r2d2"]
sqlite_pool = ["rusqlite", "r2d2_sqlite"]
postgres_pool = ["postgres", "r2d2_postgres"]
memcache_pool = ["memcache", "r2d2-memcache"]
memcache_pool = ["memcache"]

[dependencies]
r2d2 = "0.8"
Expand All @@ -31,11 +31,10 @@ diesel = { version = "2.0.0", default-features = false, optional = true }
postgres = { version = "0.19", optional = true }
r2d2_postgres = { version = "0.18", optional = true }

rusqlite = { version = "0.29.0", optional = true }
r2d2_sqlite = { version = "0.22.0", optional = true }
rusqlite = { version = "0.31.0", optional = true }
r2d2_sqlite = { version = "0.24.0", optional = true }

memcache = { version = "0.15.2", optional = true }
r2d2-memcache = { version = "0.6", optional = true }
memcache = { version = "0.17.2", optional = true }

[dependencies.rocket_sync_db_pools_codegen]
version = "0.1.0"
Expand Down
9 changes: 4 additions & 5 deletions contrib/sync_db_pools/lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,8 @@
//! | Postgres | [Diesel] | `2` | [`diesel::PgConnection`] | `diesel_postgres_pool` |
//! | MySQL | [Diesel] | `2` | [`diesel::MysqlConnection`] | `diesel_mysql_pool` |
//! | Postgres | [Rust-Postgres] | `0.19` | [`postgres::Client`] | `postgres_pool` |
//! | Sqlite | [`Rusqlite`] | `0.27` | [`rusqlite::Connection`] | `sqlite_pool` |
//! | Memcache | [`memcache`] | `0.15` | [`memcache::Client`] | `memcache_pool` |
//! | Sqlite | [`Rusqlite`] | `0.31` | [`rusqlite::Connection`] | `sqlite_pool` |
//! | Memcache | [`memcache`] | `0.17` | [`memcache::Client`] | `memcache_pool` |
//!
//! [Diesel]: https://diesel.rs
//! [`diesel::SqliteConnection`]: https://docs.rs/diesel/2/diesel/sqlite/struct.SqliteConnection.html
Expand All @@ -327,10 +327,10 @@
//! [Rust-Postgres]: https://github.com/sfackler/rust-postgres
//! [`postgres::Client`]: https://docs.rs/postgres/0.19/postgres/struct.Client.html
//! [`Rusqlite`]: https://github.com/jgallagher/rusqlite
//! [`rusqlite::Connection`]: https://docs.rs/rusqlite/0.27/rusqlite/struct.Connection.html
//! [`rusqlite::Connection`]: https://docs.rs/rusqlite/0.31/rusqlite/struct.Connection.html
//! [`diesel::PgConnection`]: http://docs.diesel.rs/diesel/pg/struct.PgConnection.html
//! [`memcache`]: https://github.com/aisk/rust-memcache
//! [`memcache::Client`]: https://docs.rs/memcache/0.15/memcache/struct.Client.html
//! [`memcache::Client`]: https://docs.rs/memcache/0.17/memcache/struct.Client.html
//!
//! The above table lists all the supported database adapters in this library.
//! In order to use particular `Poolable` type that's included in this library,
Expand Down Expand Up @@ -372,7 +372,6 @@ pub use diesel;
#[cfg(feature = "sqlite_pool")] pub use r2d2_sqlite;

#[cfg(feature = "memcache_pool")] pub use memcache;
#[cfg(feature = "memcache_pool")] pub use r2d2_memcache;

pub use r2d2;

Expand Down
70 changes: 52 additions & 18 deletions contrib/sync_db_pools/lib/src/poolable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ use crate::{Config, Error};
///
/// Implementations of `Poolable` are provided for the following types:
///
/// * `diesel::MysqlConnection`
/// * `diesel::PgConnection`
/// * `diesel::SqliteConnection`
/// * `postgres::Connection`
/// * `rusqlite::Connection`
/// * [`diesel::MysqlConnection`](diesel::MysqlConnection)
/// * [`diesel::PgConnection`](diesel::PgConnection)
/// * [`diesel::SqliteConnection`](diesel::SqliteConnection)
/// * [`postgres::Client`](postgres::Client)
/// * [`rusqlite::Connection`](rusqlite::Connection)
/// * [`memcache::Client`](memcache::Client)
///
/// # Implementation Guide
///
Expand Down Expand Up @@ -130,7 +131,7 @@ impl Poolable for diesel::SqliteConnection {
fn on_acquire(&self, conn: &mut SqliteConnection) -> Result<(), Error> {
conn.batch_execute("\
PRAGMA journal_mode = WAL;\
PRAGMA busy_timeout = 1000;\
PRAGMA busy_timeout = 5000;\
PRAGMA foreign_keys = ON;\
").map_err(Error::QueryError)?;

Expand Down Expand Up @@ -263,19 +264,52 @@ impl Poolable for rusqlite::Connection {
}

#[cfg(feature = "memcache_pool")]
impl Poolable for memcache::Client {
type Manager = r2d2_memcache::MemcacheConnectionManager;
// Unused, but we might want it in the future without a breaking change.
type Error = memcache::MemcacheError;
mod memcache_pool {
use memcache::{Client, Connectable, MemcacheError};

fn pool(db_name: &str, rocket: &Rocket<Build>) -> PoolResult<Self> {
let config = Config::from(db_name, rocket)?;
let manager = r2d2_memcache::MemcacheConnectionManager::new(&*config.url);
let pool = r2d2::Pool::builder()
.max_size(config.pool_size)
.connection_timeout(Duration::from_secs(config.timeout as u64))
.build(manager)?;
use super::*;

Ok(pool)
#[derive(Debug)]
pub struct ConnectionManager {
urls: Vec<String>,
}

impl ConnectionManager {
pub fn new<C: Connectable>(target: C) -> Self {
Self { urls: target.get_urls(), }
}
}

impl r2d2::ManageConnection for ConnectionManager {
type Connection = Client;
type Error = MemcacheError;

fn connect(&self) -> Result<Client, MemcacheError> {
Client::connect(self.urls.clone())
}

fn is_valid(&self, connection: &mut Client) -> Result<(), MemcacheError> {
connection.version().map(|_| ())
}

fn has_broken(&self, _connection: &mut Client) -> bool {
false
}
}

impl super::Poolable for memcache::Client {
type Manager = ConnectionManager;
type Error = MemcacheError;

fn pool(db_name: &str, rocket: &Rocket<Build>) -> PoolResult<Self> {
let config = Config::from(db_name, rocket)?;
let manager = ConnectionManager::new(&*config.url);
let pool = r2d2::Pool::builder()
.max_size(config.pool_size)
.connection_timeout(Duration::from_secs(config.timeout as u64))
.build(manager)?;

Ok(pool)
}
}
}
10 changes: 10 additions & 0 deletions contrib/sync_db_pools/lib/tests/databases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ mod databases_tests {
struct PrimaryDb(diesel::PgConnection);
}

#[cfg(feature = "memcache_pool")]
mod memcache_pool_tests {
#![allow(dead_code)]

use rocket_sync_db_pools::database;

#[database("test_db")]
struct MemcacheDb(memcache::Client);
}

#[cfg(test)]
#[cfg(all(feature = "sqlite_pool"))]
mod rusqlite_integration_test {
Expand Down
2 changes: 1 addition & 1 deletion contrib/ws/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ default = ["tungstenite"]
tungstenite = ["tokio-tungstenite"]

[dependencies]
tokio-tungstenite = { version = "0.21", optional = true }
tokio-tungstenite = { version = "0.23", optional = true }

[dependencies.rocket]
version = "0.6.0-dev"
Expand Down
14 changes: 14 additions & 0 deletions core/http/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,12 @@ pub struct ParseMethodError;

impl std::error::Error for ParseMethodError { }

impl From<std::convert::Infallible> for ParseMethodError {
fn from(infallible: std::convert::Infallible) -> Self {
match infallible {}
}
}

impl fmt::Display for ParseMethodError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("invalid HTTP method")
Expand All @@ -331,6 +337,14 @@ impl FromStr for Method {
}
}

impl TryFrom<&str> for Method {
type Error = ParseMethodError;

fn try_from(s: &str) -> Result<Self, Self::Error> {
Self::try_from(s.as_bytes())
}
}

impl AsRef<str> for Method {
fn as_ref(&self) -> &str {
self.as_str()
Expand Down
Loading

0 comments on commit ede7447

Please sign in to comment.