Skip to content

Commit

Permalink
Merge pull request #704 from habitat-sh/elliott/sync_db
Browse files Browse the repository at this point in the history
Make all calls to the db from the api direct and synchronous
  • Loading branch information
elliott-davis committed Oct 4, 2018
2 parents 20bb539 + 0fb0bcb commit 23aa094
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 244 deletions.
24 changes: 10 additions & 14 deletions components/builder-api/src/server/db.rs
@@ -1,4 +1,3 @@
use actix_web::actix::{Actor, Addr, SyncArbiter, SyncContext};
use actix_web::{error, Error};
use db::config::DataStoreCfg;
use diesel::pg::PgConnection;
Expand All @@ -7,22 +6,19 @@ use diesel::r2d2::{ConnectionManager, Pool, PooledConnection};
type PgPool = Pool<ConnectionManager<PgConnection>>;
type PgPooledConnection = PooledConnection<ConnectionManager<PgConnection>>;

pub struct DbExecutor(pub PgPool);
#[derive(Clone)]
pub struct DbPool(pub PgPool);

impl Actor for DbExecutor {
type Context = SyncContext<Self>;
pub fn init(config: DataStoreCfg) -> DbPool {
DbPool(
Pool::builder()
.max_size(config.pool_size)
.build(ConnectionManager::<PgConnection>::new(config.to_string()))
.expect("Failed to create pool."),
)
}

pub fn init(config: DataStoreCfg) -> Addr<DbExecutor> {
let manager = ConnectionManager::<PgConnection>::new(config.to_string());
let pool = Pool::builder()
.max_size(config.pool_size)
.build(manager)
.expect("Failed to create pool.");
SyncArbiter::start(4, move || DbExecutor(pool.clone()))
}

impl DbExecutor {
impl DbPool {
pub fn get_conn(&self) -> Result<PgPooledConnection, Error> {
self.0.get().map_err(|e| error::ErrorInternalServerError(e))
}
Expand Down
52 changes: 0 additions & 52 deletions components/builder-api/src/server/handlers/account.rs

This file was deleted.

43 changes: 0 additions & 43 deletions components/builder-api/src/server/handlers/channel.rs

This file was deleted.

2 changes: 0 additions & 2 deletions components/builder-api/src/server/handlers/mod.rs

This file was deleted.

12 changes: 5 additions & 7 deletions components/builder-api/src/server/mod.rs
Expand Up @@ -16,7 +16,6 @@ pub mod authorize;
pub mod db;
pub mod error;
pub mod framework;
pub mod handlers;
pub mod helpers;
pub mod models;
pub mod resources;
Expand All @@ -30,7 +29,6 @@ use std::sync::Arc;
use std::thread;

use actix;
use actix_web::actix::Addr;
use actix_web::http::StatusCode;
use actix_web::middleware::Logger;
use actix_web::server::{self, KeepAlive};
Expand All @@ -42,7 +40,7 @@ use hab_net::socket;
use oauth_client::client::OAuth2Client;
use segment_api_client::SegmentClient;

use self::db::{init, DbExecutor};
use self::db::{init, DbPool};
use self::error::Error;
use self::framework::middleware::{Authentication, XRouteClient};

Expand Down Expand Up @@ -81,11 +79,11 @@ pub struct AppState {
segment: SegmentClient,
upstream: UpstreamClient,
memcache: RefCell<MemcacheClient>,
db: Addr<DbExecutor>,
db: DbPool,
}

impl AppState {
pub fn new(config: &Config, db: Addr<DbExecutor>) -> AppState {
pub fn new(config: &Config, db: DbPool) -> AppState {
AppState {
config: config.clone(),
packages: S3Handler::new(config.s3.clone()),
Expand Down Expand Up @@ -154,10 +152,10 @@ pub fn run(config: Config) -> Result<()> {

// TED TODO: When originsrv gets removed we need to do the migrations here

let addr = init(config.datastore.clone());
let db_pool = init(config.datastore.clone());

server::new(move || {
let app_state = AppState::new(&config, addr.clone());
let app_state = AppState::new(&config, db_pool.clone());

App::with_state(app_state)
.middleware(Logger::default().exclude("/v1/status"))
Expand Down
21 changes: 0 additions & 21 deletions components/builder-api/src/server/models/account.rs
@@ -1,4 +1,3 @@
use actix_web::{actix::Message, Error};
use diesel;
use diesel::pg::PgConnection;
use diesel::result::QueryResult;
Expand Down Expand Up @@ -38,26 +37,6 @@ pub struct FindOrCreateAccount {
email: String,
}

impl Message for GetAccount {
type Result = Result<Account, Error>;
}

impl Message for GetAccountById {
type Result = Result<Account, Error>;
}

impl Message for CreateAccount {
type Result = Result<Account, Error>;
}

impl Message for UpdateAccount {
type Result = Result<(), Error>;
}

impl Message for FindOrCreateAccount {
type Result = Result<Account, Error>;
}

impl Account {
pub fn get(account: GetAccount, conn: &PgConnection) -> QueryResult<Account> {
diesel::sql_query("SELECT * FROM get_account_by_name_v1($1)")
Expand Down
19 changes: 1 addition & 18 deletions components/builder-api/src/server/models/channel.rs
@@ -1,5 +1,4 @@
use super::db_id_format;
use actix_web::{actix::Message, Error};
use chrono::NaiveDateTime;
use diesel;
use diesel::pg::PgConnection;
Expand All @@ -8,7 +7,7 @@ use diesel::sql_types::{BigInt, Bool, Text};
use diesel::RunQueryDsl;
use server::schema::channel::*;

#[derive(Debug, Serialize, QueryableByName)]
#[derive(Debug, Serialize, Deserialize, QueryableByName)]
#[table_name = "origin_channels"]
pub struct Channel {
#[serde(with = "db_id_format")]
Expand Down Expand Up @@ -43,22 +42,6 @@ pub struct DeleteChannel {
pub channel: String,
}

impl Message for CreateChannel {
type Result = Result<Channel, Error>;
}

impl Message for ListChannels {
type Result = Result<Vec<Channel>, Error>;
}

impl Message for GetChannel {
type Result = Result<Channel, Error>;
}

impl Message for DeleteChannel {
type Result = Result<(), Error>;
}

impl Channel {
pub fn list(channel: ListChannels, conn: &PgConnection) -> QueryResult<Vec<Channel>> {
diesel::sql_query("select * from get_origin_channels_for_origin_v3($1, $2)")
Expand Down

0 comments on commit 23aa094

Please sign in to comment.