Skip to content

Commit

Permalink
fix: hanging server (#806)
Browse files Browse the repository at this point in the history
Since the update to actix-rt@2.3 (f413996), the web server hasn't been
started, when we executed `cargo run -p rust-code-analysis-web`.

It froze once `actix_rt::System::new().run()?;` was called,
effectively preventing the execution of the code below.

By looking at the actix-rt examples and changing the code to be more
like the examples, I was able to fix this.
The webserver now behaves as expected.
  • Loading branch information
tpraxl authored Mar 21, 2022
1 parent 97e576b commit 08c61f4
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 65 deletions.
5 changes: 3 additions & 2 deletions rust-code-analysis-web/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use clap::{crate_version, App, Arg};

use web::server;

fn main() {
#[actix_web::main]
async fn main() {
let matches = App::new("rust-code-analysis-web")
.version(crate_version!())
.author(&*env!("CARGO_PKG_AUTHORS").replace(':', "\n"))
Expand Down Expand Up @@ -51,7 +52,7 @@ fn main() {
eprintln!("Invalid port number");
return;
};
if let Err(e) = server::run(host.to_string(), port, num_jobs) {
if let Err(e) = server::run(host.to_string(), port, num_jobs).await {
eprintln!("Cannot run the server at {}:{}: {}", host, port, e);
}
}
121 changes: 58 additions & 63 deletions rust-code-analysis-web/src/web/server.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use actix_rt::Runtime;
use actix_web::{
dev::Body,
guard, http,
Expand Down Expand Up @@ -220,71 +219,67 @@ fn ping() -> HttpResponse {
HttpResponse::Ok().body(Body::Empty)
}

pub fn run(host: String, port: u16, n_threads: usize) -> std::io::Result<()> {
actix_rt::System::new().run()?;
let rt = Runtime::new()?;
pub async fn run(host: String, port: u16, n_threads: usize) -> std::io::Result<()> {
let max_size = 1024 * 1024 * 4;

rt.block_on(async move {
HttpServer::new(move || {
App::new()
.service(
web::resource("/ast")
.guard(guard::Header("content-type", "application/json"))
.app_data(web::Json::<AstPayload>::configure(|cfg| {
cfg.limit(max_size)
}))
.route(web::post().to(ast_parser)),
)
.service(
web::resource("/comment")
.guard(guard::Header("content-type", "application/json"))
.app_data(web::Json::<WebCommentPayload>::configure(|cfg| {
cfg.limit(max_size)
}))
.route(web::post().to(comment_removal_json)),
)
.service(
web::resource("/comment")
.guard(guard::Header("content-type", "application/octet-stream"))
.data(web::PayloadConfig::default().limit(max_size))
.route(web::post().to(comment_removal_plain)),
)
.service(
web::resource("/metrics")
.guard(guard::Header("content-type", "application/json"))
.app_data(web::Json::<WebMetricsPayload>::configure(|cfg| {
cfg.limit(max_size)
}))
.route(web::post().to(metrics_json)),
)
.service(
web::resource("/metrics")
.guard(guard::Header("content-type", "application/octet-stream"))
.data(web::PayloadConfig::default().limit(max_size))
.route(web::post().to(metrics_plain)),
)
.service(
web::resource("/function")
.guard(guard::Header("content-type", "application/json"))
.app_data(web::Json::<WebFunctionPayload>::configure(|cfg| {
cfg.limit(max_size)
}))
.route(web::post().to(function_json)),
)
.service(
web::resource("/function")
.guard(guard::Header("content-type", "application/octet-stream"))
.data(web::PayloadConfig::default().limit(max_size))
.route(web::post().to(function_plain)),
)
.service(web::resource("/ping").route(web::get().to(ping)))
})
.workers(n_threads)
.bind((host.as_str(), port))?
.run()
.await
HttpServer::new(move || {
App::new()
.service(
web::resource("/ast")
.guard(guard::Header("content-type", "application/json"))
.app_data(web::Json::<AstPayload>::configure(|cfg| {
cfg.limit(max_size)
}))
.route(web::post().to(ast_parser)),
)
.service(
web::resource("/comment")
.guard(guard::Header("content-type", "application/json"))
.app_data(web::Json::<WebCommentPayload>::configure(|cfg| {
cfg.limit(max_size)
}))
.route(web::post().to(comment_removal_json)),
)
.service(
web::resource("/comment")
.guard(guard::Header("content-type", "application/octet-stream"))
.data(web::PayloadConfig::default().limit(max_size))
.route(web::post().to(comment_removal_plain)),
)
.service(
web::resource("/metrics")
.guard(guard::Header("content-type", "application/json"))
.app_data(web::Json::<WebMetricsPayload>::configure(|cfg| {
cfg.limit(max_size)
}))
.route(web::post().to(metrics_json)),
)
.service(
web::resource("/metrics")
.guard(guard::Header("content-type", "application/octet-stream"))
.data(web::PayloadConfig::default().limit(max_size))
.route(web::post().to(metrics_plain)),
)
.service(
web::resource("/function")
.guard(guard::Header("content-type", "application/json"))
.app_data(web::Json::<WebFunctionPayload>::configure(|cfg| {
cfg.limit(max_size)
}))
.route(web::post().to(function_json)),
)
.service(
web::resource("/function")
.guard(guard::Header("content-type", "application/octet-stream"))
.data(web::PayloadConfig::default().limit(max_size))
.route(web::post().to(function_plain)),
)
.service(web::resource("/ping").route(web::get().to(ping)))
})
.workers(n_threads)
.bind((host.as_str(), port))?
.run()
.await
}

// curl --header "Content-Type: application/json" --request POST --data '{"id": "1234", "file_name": "prova.cpp", "code": "int x = 1;", "comment": true, "span": true}' http://127.0.0.1:8081/ast
Expand Down

0 comments on commit 08c61f4

Please sign in to comment.