2019-06-12 08:07:39 +00:00
|
|
|
// Copyright (C) 2017 Author: Arun Raghavan <arun@arunraghavan.net>
|
|
|
|
//
|
2022-01-15 18:40:12 +00:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public License, v2.0.
|
|
|
|
// If a copy of the MPL was not distributed with this file, You can obtain one at
|
|
|
|
// <https://mozilla.org/MPL/2.0/>.
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
2019-06-12 08:07:39 +00:00
|
|
|
|
2022-05-14 05:01:35 +00:00
|
|
|
use aws_config::meta::region::RegionProviderChain;
|
2022-10-31 09:00:55 +00:00
|
|
|
use aws_sdk_s3::{config::timeout::TimeoutConfig, Credentials, Region};
|
2022-05-14 05:01:35 +00:00
|
|
|
use aws_types::sdk_config::SdkConfig;
|
|
|
|
|
2022-12-17 10:08:50 +00:00
|
|
|
use aws_smithy_http::byte_stream::{error::Error, ByteStream};
|
2022-05-14 05:01:35 +00:00
|
|
|
|
|
|
|
use bytes::{buf::BufMut, Bytes, BytesMut};
|
|
|
|
use futures::stream::TryStreamExt;
|
|
|
|
use futures::{future, Future};
|
2020-11-22 15:43:59 +00:00
|
|
|
use once_cell::sync::Lazy;
|
2019-06-12 08:07:39 +00:00
|
|
|
use std::sync::Mutex;
|
2022-02-23 18:10:30 +00:00
|
|
|
use std::time::Duration;
|
2019-06-12 08:07:39 +00:00
|
|
|
use tokio::runtime;
|
|
|
|
|
2022-05-14 05:01:35 +00:00
|
|
|
const DEFAULT_S3_REGION: &str = "us-west-2";
|
2022-02-23 18:10:30 +00:00
|
|
|
|
2020-11-22 15:43:59 +00:00
|
|
|
static RUNTIME: Lazy<runtime::Runtime> = Lazy::new(|| {
|
2021-01-09 10:14:31 +00:00
|
|
|
runtime::Builder::new_multi_thread()
|
2020-04-20 01:28:30 +00:00
|
|
|
.enable_all()
|
2021-01-09 10:14:31 +00:00
|
|
|
.worker_threads(2)
|
2022-05-26 09:52:42 +00:00
|
|
|
.thread_name("gst-aws-runtime")
|
2020-04-20 01:28:30 +00:00
|
|
|
.build()
|
2020-11-22 15:43:59 +00:00
|
|
|
.unwrap()
|
|
|
|
});
|
2020-04-20 01:28:30 +00:00
|
|
|
|
2022-03-18 09:01:21 +00:00
|
|
|
#[derive(Debug)]
|
2020-04-20 01:28:30 +00:00
|
|
|
pub enum WaitError<E> {
|
|
|
|
Cancelled,
|
|
|
|
FutureError(E),
|
|
|
|
}
|
|
|
|
|
2022-05-14 05:01:35 +00:00
|
|
|
pub fn wait<F, T, E>(
|
|
|
|
canceller: &Mutex<Option<future::AbortHandle>>,
|
2022-02-23 18:10:30 +00:00
|
|
|
future: F,
|
2022-05-14 05:01:35 +00:00
|
|
|
) -> Result<T, WaitError<E>>
|
2022-02-23 18:10:30 +00:00
|
|
|
where
|
2022-05-14 05:01:35 +00:00
|
|
|
F: Send + Future<Output = Result<T, E>>,
|
|
|
|
F::Output: Send,
|
|
|
|
T: Send,
|
|
|
|
E: Send,
|
2022-02-23 18:10:30 +00:00
|
|
|
{
|
2022-05-14 05:01:35 +00:00
|
|
|
let mut canceller_guard = canceller.lock().unwrap();
|
|
|
|
let (abort_handle, abort_registration) = future::AbortHandle::new_pair();
|
|
|
|
|
|
|
|
canceller_guard.replace(abort_handle);
|
|
|
|
drop(canceller_guard);
|
|
|
|
|
|
|
|
let abortable_future = future::Abortable::new(future, abort_registration);
|
|
|
|
|
|
|
|
// FIXME: add a timeout as well
|
|
|
|
|
|
|
|
let res = {
|
|
|
|
let _enter = RUNTIME.enter();
|
|
|
|
futures::executor::block_on(async {
|
|
|
|
match abortable_future.await {
|
|
|
|
// Future resolved successfully
|
|
|
|
Ok(Ok(res)) => Ok(res),
|
|
|
|
// Future resolved with an error
|
|
|
|
Ok(Err(err)) => Err(WaitError::FutureError(err)),
|
|
|
|
// Canceller called before future resolved
|
|
|
|
Err(future::Aborted) => Err(WaitError::Cancelled),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Clear out the canceller */
|
|
|
|
canceller_guard = canceller.lock().unwrap();
|
|
|
|
*canceller_guard = None;
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn wait_stream(
|
|
|
|
canceller: &Mutex<Option<future::AbortHandle>>,
|
|
|
|
stream: &mut ByteStream,
|
|
|
|
) -> Result<Bytes, WaitError<Error>> {
|
|
|
|
wait(canceller, async move {
|
|
|
|
let mut collect = BytesMut::new();
|
|
|
|
|
|
|
|
// Loop over the stream and collect till we're done
|
|
|
|
while let Some(item) = stream.try_next().await? {
|
|
|
|
collect.put(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok::<Bytes, Error>(collect.freeze())
|
2022-02-23 18:10:30 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-14 05:01:35 +00:00
|
|
|
// See setting-timeouts example in aws-sdk-rust.
|
2022-10-31 09:00:55 +00:00
|
|
|
pub fn timeout_config(request_timeout: Duration) -> TimeoutConfig {
|
|
|
|
TimeoutConfig::builder()
|
|
|
|
.operation_attempt_timeout(request_timeout)
|
|
|
|
.build()
|
2022-02-23 18:10:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 05:01:35 +00:00
|
|
|
pub fn wait_config(
|
2022-02-23 18:10:30 +00:00
|
|
|
canceller: &Mutex<Option<future::AbortHandle>>,
|
2022-05-14 05:01:35 +00:00
|
|
|
region: Region,
|
2022-10-31 09:00:55 +00:00
|
|
|
timeout_config: TimeoutConfig,
|
2022-05-14 05:01:35 +00:00
|
|
|
credentials: Option<Credentials>,
|
|
|
|
) -> Result<SdkConfig, WaitError<Error>> {
|
|
|
|
let region_provider = RegionProviderChain::first_try(region)
|
|
|
|
.or_default_provider()
|
|
|
|
.or_else(Region::new(DEFAULT_S3_REGION));
|
|
|
|
let config_future = match credentials {
|
|
|
|
Some(cred) => aws_config::from_env()
|
|
|
|
.timeout_config(timeout_config)
|
|
|
|
.region(region_provider)
|
|
|
|
.credentials_provider(cred)
|
|
|
|
.load(),
|
|
|
|
None => aws_config::from_env()
|
|
|
|
.timeout_config(timeout_config)
|
|
|
|
.region(region_provider)
|
|
|
|
.load(),
|
|
|
|
};
|
|
|
|
|
2022-02-23 18:10:30 +00:00
|
|
|
let mut canceller_guard = canceller.lock().unwrap();
|
|
|
|
let (abort_handle, abort_registration) = future::AbortHandle::new_pair();
|
|
|
|
|
|
|
|
canceller_guard.replace(abort_handle);
|
|
|
|
drop(canceller_guard);
|
|
|
|
|
2022-05-14 05:01:35 +00:00
|
|
|
let abortable_future = future::Abortable::new(config_future, abort_registration);
|
|
|
|
|
2022-02-23 18:10:30 +00:00
|
|
|
let res = {
|
|
|
|
let _enter = RUNTIME.enter();
|
|
|
|
futures::executor::block_on(async {
|
2022-05-14 05:01:35 +00:00
|
|
|
match abortable_future.await {
|
2022-02-23 18:10:30 +00:00
|
|
|
// Future resolved successfully
|
2022-05-14 05:01:35 +00:00
|
|
|
Ok(config) => Ok(config),
|
2022-02-23 18:10:30 +00:00
|
|
|
// Canceller called before future resolved
|
|
|
|
Err(future::Aborted) => Err(WaitError::Cancelled),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Clear out the canceller */
|
|
|
|
canceller_guard = canceller.lock().unwrap();
|
|
|
|
*canceller_guard = None;
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
2022-03-18 10:10:18 +00:00
|
|
|
|
2022-05-14 05:01:35 +00:00
|
|
|
pub fn duration_from_millis(millis: i64) -> Duration {
|
2022-03-18 10:10:18 +00:00
|
|
|
match millis {
|
2022-05-14 05:01:35 +00:00
|
|
|
-1 => Duration::MAX,
|
|
|
|
v => Duration::from_millis(v as u64),
|
2022-03-18 10:10:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn duration_to_millis(dur: Option<Duration>) -> i64 {
|
|
|
|
match dur {
|
2022-05-14 05:01:35 +00:00
|
|
|
None => Duration::MAX.as_millis() as i64,
|
2022-03-18 10:10:18 +00:00
|
|
|
Some(d) => d.as_millis() as i64,
|
|
|
|
}
|
|
|
|
}
|