Skip to content

Commit

Permalink
Remove unwrap()s when creating an S3 bucket.
Browse files Browse the repository at this point in the history
  • Loading branch information
nikurt committed Apr 11, 2023
1 parent 7493c6f commit 67c6c0b
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions chain/client/src/sync/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,21 +151,13 @@ impl StateSync {
num_s3_requests_per_shard: u64,
) -> Self {
let inner = if state_sync_from_s3_enabled {
// `unwrap()` here is fine, because the config validation has already
// ensured those fields are present.
let mut bucket = s3::Bucket::new(
s3_bucket,
s3_region.parse::<s3::Region>().unwrap(),
s3::creds::Credentials::anonymous().unwrap(),
)
.unwrap();
// Ensure requests finish in finite amount of time.
bucket.set_request_timeout(Some(timeout));
let bucket = Arc::new(bucket);

let bucket = create_bucket(s3_bucket, s3_region, timeout);
if let Err(err) = bucket {
panic!("Failed to create an S3 bucket: {}", err);
}
StateSyncInner::PartsFromExternal {
chain_id: chain_id.to_string(),
bucket,
bucket: Arc::new(bucket.unwrap()),
requests_remaining: Arc::new(AtomicI64::new(num_s3_requests_per_shard as i64)),
}
} else {
Expand Down Expand Up @@ -1075,6 +1067,23 @@ impl StateSync {
}
}

fn create_bucket(
bucket: &str,
region: &str,
timeout: TimeDuration,
) -> Result<s3::Bucket, near_chain::Error> {
let mut bucket = s3::Bucket::new(
bucket,
region.parse::<s3::Region>().map_err(|err| near_chain::Error::Other(err.to_string()))?,
s3::creds::Credentials::anonymous()
.map_err(|err| near_chain::Error::Other(err.to_string()))?,
)
.map_err(|err| near_chain::Error::Other(err.to_string()))?;
// Ensure requests finish in finite amount of time.
bucket.set_request_timeout(Some(timeout));
Ok(bucket)
}

/// Returns parts that still need to be fetched.
fn parts_to_fetch(
new_shard_sync_download: &mut ShardSyncDownload,
Expand Down

0 comments on commit 67c6c0b

Please sign in to comment.