Skip to content

Minor cleanups from re-reading code #1159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions compiler/base/orchestrator/src/coordinator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ where
use versions_error::*;

let [stable, beta, nightly] =
[Channel::Stable, Channel::Beta, Channel::Nightly].map(|c| async move {
[Channel::Stable, Channel::Beta, Channel::Nightly].map(async |c| {
let c = self.select_channel(c).await?;
c.versions().await.map_err(VersionsChannelError::from)
});
Expand Down Expand Up @@ -1144,11 +1144,9 @@ where
let token = mem::take(token);
token.cancel();

let channels = [stable, beta, nightly].map(|c| async {
match c.take() {
Some(c) => c.shutdown().await,
_ => Ok(()),
}
let channels = [stable, beta, nightly].map(async |c| match c.take() {
Some(c) => c.shutdown().await,
_ => Ok(()),
});

let [stable, beta, nightly] = channels;
Expand Down Expand Up @@ -2987,7 +2985,7 @@ mod tests {
}
}

const MAX_CONCURRENT_TESTS: LazyLock<usize> = LazyLock::new(|| {
static MAX_CONCURRENT_TESTS: LazyLock<usize> = LazyLock::new(|| {
env::var("TESTS_MAX_CONCURRENCY")
.ok()
.and_then(|v| v.parse().ok())
Expand Down Expand Up @@ -3081,7 +3079,7 @@ mod tests {
(Mode::Release, "[optimized]"),
];

let tests = params.into_iter().map(|(mode, expected)| async move {
let tests = params.into_iter().map(async |(mode, expected)| {
let coordinator = new_coordinator();

let request = ExecuteRequest {
Expand Down Expand Up @@ -3116,8 +3114,10 @@ mod tests {
];

let tests = params.into_iter().flat_map(|(code, works_in)| {
Edition::ALL.into_iter().zip(works_in).map(
move |(edition, expected_to_work)| async move {
Edition::ALL
.into_iter()
.zip(works_in)
.map(async |(edition, expected_to_work)| {
let coordinator = new_coordinator();

let request = ExecuteRequest {
Expand All @@ -3137,8 +3137,7 @@ mod tests {
coordinator.shutdown().await?;

Ok::<_, Error>(())
},
)
})
});

try_join_all(tests).with_timeout().await?;
Expand All @@ -3157,7 +3156,7 @@ mod tests {
),
];

let tests = params.into_iter().map(|(crate_type, expected)| async move {
let tests = params.into_iter().map(async |(crate_type, expected)| {
let coordinator = new_coordinator();

let request = ExecuteRequest {
Expand Down Expand Up @@ -3190,7 +3189,7 @@ mod tests {

let params = [(false, "Running `"), (true, "Running unittests")];

let tests = params.into_iter().map(|(tests, expected)| async move {
let tests = params.into_iter().map(async |(tests, expected)| {
let coordinator = new_coordinator();

let request = ExecuteRequest {
Expand Down Expand Up @@ -3223,7 +3222,7 @@ mod tests {
(true, "stack backtrace:"),
];

let tests = params.into_iter().map(|(backtrace, expected)| async move {
let tests = params.into_iter().map(async |(backtrace, expected)| {
let coordinator = new_coordinator();

let request = ExecuteRequest {
Expand Down Expand Up @@ -3539,7 +3538,7 @@ mod tests {
.await
.unwrap();

assert!(response.success, "stderr: {}", stderr);
assert!(response.success, "stderr: {stderr}");
assert_contains!(stderr, "Compiling");
assert_contains!(stderr, "Finished");

Expand Down
2 changes: 1 addition & 1 deletion compiler/base/orchestrator/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1172,7 +1172,7 @@ mod test {
let next_result = this.0.pop_front().expect("FixedAsyncRead ran out of input");

if let Ok(v) = &next_result {
buf.put_slice(&v);
buf.put_slice(v);
}

Poll::Ready(next_result.map(drop))
Expand Down
2 changes: 1 addition & 1 deletion ui/src/gist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl From<octocrab::models::gists::Gist> for Gist {
0 | 1 => files.into_iter().map(|(_, content)| content).collect(),
_ => files
.into_iter()
.map(|(name, content)| format!("// {}\n{}\n\n", name, content))
.map(|(name, content)| format!("// {name}\n{content}\n\n"))
.collect(),
};

Expand Down
82 changes: 38 additions & 44 deletions ui/src/server_axum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ use axum_extra::{
headers::{authorization::Bearer, Authorization, CacheControl, ETag, IfNoneMatch},
TypedHeader,
};
use futures::{future::BoxFuture, FutureExt, TryFutureExt};
use futures::{FutureExt, TryFutureExt};
use orchestrator::coordinator::{self, CoordinatorFactory, DockerBackend, TRACKED_CONTAINERS};
use snafu::prelude::*;
use std::{
convert::TryInto,
future::Future,
mem, path,
str::FromStr,
sync::{Arc, LazyLock},
Expand Down Expand Up @@ -134,7 +133,8 @@ pub(crate) async fn serve(config: Config) {
let x_request_id = HeaderName::from_static("x-request-id");

// Basic access logging
app = app.layer(
app = app.layer({
let x_request_id = x_request_id.clone();
TraceLayer::new_for_http().make_span_with(move |req: &Request<_>| {
const REQUEST_ID: &str = "request_id";

Expand All @@ -152,17 +152,15 @@ pub(crate) async fn serve(config: Config) {
}

span
}),
);

let x_request_id = HeaderName::from_static("x-request-id");
})
});

// propagate `x-request-id` headers from request to response
app = app.layer(PropagateRequestIdLayer::new(x_request_id.clone()));

app = app.layer(SetRequestIdLayer::new(
x_request_id.clone(),
MakeRequestUuid::default(),
MakeRequestUuid,
));

let server_socket_addr = config.server_socket_addr();
Expand Down Expand Up @@ -208,16 +206,15 @@ async fn rewrite_help_as_index(
next.run(req).await
}

async fn attempt_record_request<T, RFut, RT, RE>(
async fn attempt_record_request<R, T, E>(
db: Handle,
req: T,
f: impl FnOnce(T) -> RFut,
) -> Result<RT, RE>
req: R,
f: impl AsyncFnOnce(R) -> Result<T, E>,
) -> Result<T, E>
where
T: HasEndpoint + serde::Serialize,
RFut: Future<Output = Result<RT, RE>>,
R: HasEndpoint + serde::Serialize,
{
let category = format!("http.{}", <&str>::from(T::ENDPOINT));
let category = format!("http.{}", <&str>::from(R::ENDPOINT));
let payload = serde_json::to_string(&req).unwrap_or_else(|_| String::from("<invalid JSON>"));
let guard = db.start_with_guard(category, payload).await;

Expand All @@ -233,9 +230,9 @@ async fn evaluate(
Extension(db): Extension<Handle>,
Json(req): Json<api::EvaluateRequest>,
) -> Result<Json<api::EvaluateResponse>> {
attempt_record_request(db, req, |req| async {
with_coordinator(&factory.0, req, |c, req| {
c.execute(req).context(EvaluateSnafu).boxed()
attempt_record_request(db, req, async |req| {
with_coordinator(&factory.0, req, async |c, req| {
c.execute(req).context(EvaluateSnafu).await
})
.await
.map(Json)
Expand All @@ -248,9 +245,9 @@ async fn compile(
Extension(db): Extension<Handle>,
Json(req): Json<api::CompileRequest>,
) -> Result<Json<api::CompileResponse>> {
attempt_record_request(db, req, |req| async {
with_coordinator(&factory.0, req, |c, req| {
c.compile(req).context(CompileSnafu).boxed()
attempt_record_request(db, req, async |req| {
with_coordinator(&factory.0, req, async |c, req| {
c.compile(req).context(CompileSnafu).await
})
.await
.map(Json)
Expand All @@ -263,9 +260,9 @@ async fn execute(
Extension(db): Extension<Handle>,
Json(req): Json<api::ExecuteRequest>,
) -> Result<Json<api::ExecuteResponse>> {
attempt_record_request(db, req, |req| async {
with_coordinator(&factory.0, req, |c, req| {
c.execute(req).context(ExecuteSnafu).boxed()
attempt_record_request(db, req, async |req| {
with_coordinator(&factory.0, req, async |c, req| {
c.execute(req).context(ExecuteSnafu).await
})
.await
.map(Json)
Expand All @@ -278,9 +275,9 @@ async fn format(
Extension(db): Extension<Handle>,
Json(req): Json<api::FormatRequest>,
) -> Result<Json<api::FormatResponse>> {
attempt_record_request(db, req, |req| async {
with_coordinator(&factory.0, req, |c, req| {
c.format(req).context(FormatSnafu).boxed()
attempt_record_request(db, req, async |req| {
with_coordinator(&factory.0, req, async |c, req| {
c.format(req).context(FormatSnafu).await
})
.await
.map(Json)
Expand All @@ -293,9 +290,9 @@ async fn clippy(
Extension(db): Extension<Handle>,
Json(req): Json<api::ClippyRequest>,
) -> Result<Json<api::ClippyResponse>> {
attempt_record_request(db, req, |req| async {
with_coordinator(&factory.0, req, |c, req| {
c.clippy(req).context(ClippySnafu).boxed()
attempt_record_request(db, req, async |req| {
with_coordinator(&factory.0, req, async |c, req| {
c.clippy(req).context(ClippySnafu).await
})
.await
.map(Json)
Expand All @@ -308,9 +305,9 @@ async fn miri(
Extension(db): Extension<Handle>,
Json(req): Json<api::MiriRequest>,
) -> Result<Json<api::MiriResponse>> {
attempt_record_request(db, req, |req| async {
with_coordinator(&factory.0, req, |c, req| {
c.miri(req).context(MiriSnafu).boxed()
attempt_record_request(db, req, async |req| {
with_coordinator(&factory.0, req, async |c, req| {
c.miri(req).context(MiriSnafu).await
})
.await
.map(Json)
Expand All @@ -323,9 +320,9 @@ async fn macro_expansion(
Extension(db): Extension<Handle>,
Json(req): Json<api::MacroExpansionRequest>,
) -> Result<Json<api::MacroExpansionResponse>> {
attempt_record_request(db, req, |req| async {
with_coordinator(&factory.0, req, |c, req| {
c.macro_expansion(req).context(MacroExpansionSnafu).boxed()
attempt_record_request(db, req, async |req| {
with_coordinator(&factory.0, req, async |c, req| {
c.macro_expansion(req).context(MacroExpansionSnafu).await
})
.await
.map(Json)
Expand Down Expand Up @@ -433,10 +430,10 @@ impl Outcome {
}
}

async fn with_coordinator<WebReq, WebResp, Req, Resp, F>(
async fn with_coordinator<WebReq, WebResp, Req, Resp>(
factory: &CoordinatorFactory,
req: WebReq,
f: F,
f: impl AsyncFnOnce(&coordinator::Coordinator<DockerBackend>, Req) -> Result<Resp>,
) -> Result<WebResp>
where
WebReq: TryInto<Req>,
Expand All @@ -445,8 +442,6 @@ where
Req: HasLabelsCore,
Resp: Into<WebResp>,
Resp: IsSuccess,
for<'f> F:
FnOnce(&'f coordinator::Coordinator<DockerBackend>, Req) -> BoxFuture<'f, Result<Resp>>,
{
let coordinator = factory.build();

Expand Down Expand Up @@ -525,9 +520,8 @@ where
.with_max_age(SANDBOX_CACHE_TIME_TO_LIVE)
.with_public();

let use_fresh = if_none_match.map_or(true, |if_none_match| {
if_none_match.0.precondition_passes(&etag)
});
let use_fresh =
if_none_match.is_none_or(|if_none_match| if_none_match.0.precondition_passes(&etag));

let etag = TypedHeader(etag);
let cache_control = TypedHeader(cache_control);
Expand Down Expand Up @@ -617,7 +611,7 @@ async fn nowebsocket(Json(req): Json<NoWebSocketRequest>) {
}

static WS_ERRORS: LazyLock<std::sync::Mutex<std::collections::HashMap<String, usize>>> =
LazyLock::new(|| Default::default());
LazyLock::new(Default::default);

fn record_websocket_error(error: String) {
*WS_ERRORS
Expand Down
Loading