aboutsummaryrefslogtreecommitdiff
path: root/planetwars-server/src/modules
diff options
context:
space:
mode:
Diffstat (limited to 'planetwars-server/src/modules')
-rw-r--r--planetwars-server/src/modules/bots.rs2
-rw-r--r--planetwars-server/src/modules/client_api.rs6
-rw-r--r--planetwars-server/src/modules/matches.rs11
-rw-r--r--planetwars-server/src/modules/ranking.rs13
-rw-r--r--planetwars-server/src/modules/registry.rs40
5 files changed, 37 insertions, 35 deletions
diff --git a/planetwars-server/src/modules/bots.rs b/planetwars-server/src/modules/bots.rs
index 6a2883c..6893581 100644
--- a/planetwars-server/src/modules/bots.rs
+++ b/planetwars-server/src/modules/bots.rs
@@ -9,7 +9,7 @@ use crate::{db, util::gen_alphanumeric, GlobalConfig};
pub fn save_code_string(
bot_code: &str,
bot_id: Option<i32>,
- conn: &PgConnection,
+ conn: &mut PgConnection,
config: &GlobalConfig,
) -> QueryResult<db::bots::BotVersion> {
let bundle_name = gen_alphanumeric(16);
diff --git a/planetwars-server/src/modules/client_api.rs b/planetwars-server/src/modules/client_api.rs
index 6e5d05a..9c0bbe7 100644
--- a/planetwars-server/src/modules/client_api.rs
+++ b/planetwars-server/src/modules/client_api.rs
@@ -149,19 +149,19 @@ impl pb::client_api_service_server::ClientApiService for ClientApiServer {
req: Request<pb::CreateMatchRequest>,
) -> Result<Response<pb::CreateMatchResponse>, Status> {
// TODO: unify with matchrunner module
- let conn = self.conn_pool.get().await.unwrap();
+ let mut conn = self.conn_pool.get().await.unwrap();
let match_request = req.get_ref();
let (opponent_bot, opponent_bot_version) =
- db::bots::find_bot_with_version_by_name(&match_request.opponent_name, &conn)
+ db::bots::find_bot_with_version_by_name(&match_request.opponent_name, &mut conn)
.map_err(|_| Status::not_found("opponent not found"))?;
let map_name = match match_request.map_name.as_str() {
"" => "hex",
name => name,
};
- let map = db::maps::find_map_by_name(map_name, &conn)
+ let map = db::maps::find_map_by_name(map_name, &mut conn)
.map_err(|_| Status::not_found("map not found"))?;
let player_key = gen_alphanumeric(32);
diff --git a/planetwars-server/src/modules/matches.rs b/planetwars-server/src/modules/matches.rs
index ecc7976..71e8a98 100644
--- a/planetwars-server/src/modules/matches.rs
+++ b/planetwars-server/src/modules/matches.rs
@@ -80,8 +80,8 @@ impl RunMatch {
let match_data = {
// TODO: it would be nice to get an already-open connection here when possible.
// Maybe we need an additional abstraction, bundling a connection and connection pool?
- let db_conn = conn_pool.get().await.expect("could not get a connection");
- self.store_in_database(&db_conn)?
+ let mut db_conn = conn_pool.get().await.expect("could not get a connection");
+ self.store_in_database(&mut db_conn)?
};
let runner_config = self.into_runner_config();
@@ -90,7 +90,7 @@ impl RunMatch {
Ok((match_data, handle))
}
- fn store_in_database(&self, db_conn: &PgConnection) -> QueryResult<MatchData> {
+ fn store_in_database(&self, db_conn: &mut PgConnection) -> QueryResult<MatchData> {
let new_match_data = db::matches::NewMatch {
state: db::matches::MatchState::Playing,
log_path: &self.log_file_name,
@@ -167,7 +167,7 @@ async fn run_match_task(
let outcome = runner::run_match(match_config).await;
// update match state in database
- let conn = connection_pool
+ let mut conn = connection_pool
.get()
.await
.expect("could not get database connection");
@@ -176,7 +176,8 @@ async fn run_match_task(
winner: outcome.winner.map(|w| (w - 1) as i32), // player numbers in matchrunner start at 1
};
- db::matches::save_match_result(match_id, result, &conn).expect("could not save match result");
+ db::matches::save_match_result(match_id, result, &mut conn)
+ .expect("could not save match result");
outcome
}
diff --git a/planetwars-server/src/modules/ranking.rs b/planetwars-server/src/modules/ranking.rs
index 90c4a56..92f0f8a 100644
--- a/planetwars-server/src/modules/ranking.rs
+++ b/planetwars-server/src/modules/ranking.rs
@@ -20,13 +20,14 @@ pub async fn run_ranker(config: Arc<GlobalConfig>, db_pool: DbPool) {
// TODO: make this configurable
// play at most one match every n seconds
let mut interval = tokio::time::interval(Duration::from_secs(RANKER_INTERVAL));
- let db_conn = db_pool
+ let mut db_conn = db_pool
.get()
.await
.expect("could not get database connection");
loop {
interval.tick().await;
- let bots = db::bots::all_active_bots_with_version(&db_conn).expect("could not load bots");
+ let bots =
+ db::bots::all_active_bots_with_version(&mut db_conn).expect("could not load bots");
if bots.len() < 2 {
// not enough bots to play a match
continue;
@@ -37,14 +38,14 @@ pub async fn run_ranker(config: Arc<GlobalConfig>, db_pool: DbPool) {
.cloned()
.collect();
- let maps = db::maps::list_maps(&db_conn).expect("could not load map");
+ let maps = db::maps::list_maps(&mut db_conn).expect("could not load map");
let map = match maps.choose(&mut rand::thread_rng()).cloned() {
None => continue, // no maps available
Some(map) => map,
};
play_ranking_match(config.clone(), map, selected_bots, db_pool.clone()).await;
- recalculate_ratings(&db_conn).expect("could not recalculate ratings");
+ recalculate_ratings(&mut db_conn).expect("could not recalculate ratings");
}
}
@@ -71,7 +72,7 @@ async fn play_ranking_match(
let _outcome = handle.await;
}
-fn recalculate_ratings(db_conn: &PgConnection) -> QueryResult<()> {
+fn recalculate_ratings(db_conn: &mut PgConnection) -> QueryResult<()> {
let start = Instant::now();
let match_stats = fetch_match_stats(db_conn)?;
let ratings = estimate_ratings_from_stats(match_stats);
@@ -91,7 +92,7 @@ struct MatchStats {
num_matches: usize,
}
-fn fetch_match_stats(db_conn: &PgConnection) -> QueryResult<HashMap<(i32, i32), MatchStats>> {
+fn fetch_match_stats(db_conn: &mut PgConnection) -> QueryResult<HashMap<(i32, i32), MatchStats>> {
let matches = db::matches::list_matches(RANKER_NUM_MATCHES, db_conn)?;
let mut match_stats = HashMap::<(i32, i32), MatchStats>::new();
diff --git a/planetwars-server/src/modules/registry.rs b/planetwars-server/src/modules/registry.rs
index 4a79d59..5e1e05b 100644
--- a/planetwars-server/src/modules/registry.rs
+++ b/planetwars-server/src/modules/registry.rs
@@ -112,8 +112,8 @@ where
Err(RegistryAuthError::InvalidCredentials)
}
} else {
- let db_conn = DatabaseConnection::from_request(req).await.unwrap();
- let user = authenticate_user(&credentials, &db_conn)
+ let mut db_conn = DatabaseConnection::from_request(req).await.unwrap();
+ let user = authenticate_user(&credentials, &mut db_conn)
.ok_or(RegistryAuthError::InvalidCredentials)?;
Ok(RegistryAuth::User(user))
@@ -159,12 +159,12 @@ pub struct RegistryError {
}
async fn check_blob_exists(
- db_conn: DatabaseConnection,
+ mut db_conn: DatabaseConnection,
auth: RegistryAuth,
Path((repository_name, raw_digest)): Path<(String, String)>,
Extension(config): Extension<Arc<GlobalConfig>>,
) -> Result<impl IntoResponse, StatusCode> {
- check_access(&repository_name, &auth, &db_conn)?;
+ check_access(&repository_name, &auth, &mut db_conn)?;
let digest = raw_digest.strip_prefix("sha256:").unwrap();
let blob_path = PathBuf::from(&config.registry_directory)
@@ -179,12 +179,12 @@ async fn check_blob_exists(
}
async fn get_blob(
- db_conn: DatabaseConnection,
+ mut db_conn: DatabaseConnection,
auth: RegistryAuth,
Path((repository_name, raw_digest)): Path<(String, String)>,
Extension(config): Extension<Arc<GlobalConfig>>,
) -> Result<impl IntoResponse, StatusCode> {
- check_access(&repository_name, &auth, &db_conn)?;
+ check_access(&repository_name, &auth, &mut db_conn)?;
let digest = raw_digest.strip_prefix("sha256:").unwrap();
let blob_path = PathBuf::from(&config.registry_directory)
@@ -200,12 +200,12 @@ async fn get_blob(
}
async fn create_upload(
- db_conn: DatabaseConnection,
+ mut db_conn: DatabaseConnection,
auth: RegistryAuth,
Path(repository_name): Path<String>,
Extension(config): Extension<Arc<GlobalConfig>>,
) -> Result<impl IntoResponse, StatusCode> {
- check_access(&repository_name, &auth, &db_conn)?;
+ check_access(&repository_name, &auth, &mut db_conn)?;
let uuid = gen_alphanumeric(16);
tokio::fs::File::create(
@@ -229,13 +229,13 @@ async fn create_upload(
}
async fn patch_upload(
- db_conn: DatabaseConnection,
+ mut db_conn: DatabaseConnection,
auth: RegistryAuth,
Path((repository_name, uuid)): Path<(String, String)>,
mut stream: BodyStream,
Extension(config): Extension<Arc<GlobalConfig>>,
) -> Result<impl IntoResponse, StatusCode> {
- check_access(&repository_name, &auth, &db_conn)?;
+ check_access(&repository_name, &auth, &mut db_conn)?;
// TODO: support content range header in request
let upload_path = PathBuf::from(&config.registry_directory)
@@ -275,14 +275,14 @@ struct UploadParams {
}
async fn put_upload(
- db_conn: DatabaseConnection,
+ mut db_conn: DatabaseConnection,
auth: RegistryAuth,
Path((repository_name, uuid)): Path<(String, String)>,
Query(params): Query<UploadParams>,
mut stream: BodyStream,
Extension(config): Extension<Arc<GlobalConfig>>,
) -> Result<impl IntoResponse, StatusCode> {
- check_access(&repository_name, &auth, &db_conn)?;
+ check_access(&repository_name, &auth, &mut db_conn)?;
let upload_path = PathBuf::from(&config.registry_directory)
.join("uploads")
@@ -332,12 +332,12 @@ async fn put_upload(
}
async fn get_manifest(
- db_conn: DatabaseConnection,
+ mut db_conn: DatabaseConnection,
auth: RegistryAuth,
Path((repository_name, reference)): Path<(String, String)>,
Extension(config): Extension<Arc<GlobalConfig>>,
) -> Result<impl IntoResponse, StatusCode> {
- check_access(&repository_name, &auth, &db_conn)?;
+ check_access(&repository_name, &auth, &mut db_conn)?;
let manifest_path = PathBuf::from(&config.registry_directory)
.join("manifests")
@@ -357,13 +357,13 @@ async fn get_manifest(
}
async fn put_manifest(
- db_conn: DatabaseConnection,
+ mut db_conn: DatabaseConnection,
auth: RegistryAuth,
Path((repository_name, reference)): Path<(String, String)>,
mut stream: BodyStream,
Extension(config): Extension<Arc<GlobalConfig>>,
) -> Result<impl IntoResponse, StatusCode> {
- let bot = check_access(&repository_name, &auth, &db_conn)?;
+ let bot = check_access(&repository_name, &auth, &mut db_conn)?;
let repository_dir = PathBuf::from(&config.registry_directory)
.join("manifests")
@@ -399,9 +399,9 @@ async fn put_manifest(
code_bundle_path: None,
container_digest: Some(&content_digest),
};
- let version =
- db::bots::create_bot_version(&new_version, &db_conn).expect("could not save bot version");
- db::bots::set_active_version(bot.id, Some(version.id), &db_conn)
+ let version = db::bots::create_bot_version(&new_version, &mut db_conn)
+ .expect("could not save bot version");
+ db::bots::set_active_version(bot.id, Some(version.id), &mut db_conn)
.expect("could not update bot version");
Ok(Response::builder()
@@ -421,7 +421,7 @@ async fn put_manifest(
fn check_access(
repository_name: &str,
auth: &RegistryAuth,
- db_conn: &DatabaseConnection,
+ db_conn: &mut DatabaseConnection,
) -> Result<db::bots::Bot, StatusCode> {
use diesel::OptionalExtension;