Handling Master Slave database configuration in Rocket


#1

Rocket provides a way to inject database pool using guards
https://rocket.rs/guide/state/#databases

However, I want have a master slave structure. 1 master for write and 4 slaves to read (slaves are selected at random to read)

so I guess my routes function will be like
fn register_user_handler(conn: DbConnMaster, conn1 : DbConnSlave1, conn2 : DbConnSlave2 ...)

Also, I have 5 such Databases with master slave configuration . So around 25 connection pools . Specifying all those connection as guard in handler seems verbose.

So I was wondering if there is a better way in which I can keep those connection pool and use it across my application safely?


Switch master slave connection inside a function from outside
#2

Maybe you can wrap those connections into a struct?

pub struct DbCollection {
    pub conn1: DbConnMaster,
    pub conn2: DbConnSlave,
}

let newDbCollection = DbCollection {
    conn1: DbConnMaster::new(),
    conn2: DbConnSlave::new(),
};

rocket::ignite().manage(newDbCollection);

#[get("/something")]
fn something(db_collection: State<DbCollection>)