Posts about Actix
I am writing here in desperation after 5 weeks of intense suffering. I have been trying to learn Rust ( experienced in modern C++, python) and when I did all the code for my earliest experiments (a n-body orbit simulation tool) it works fine so the basics of the language I can handle.
But, I wanted to try to make an API to receive requests, add new objects to the orbiter, persist, and publish predicted orbits . I decided to try using Actix and diesel as it seemed to be the logical choice.
I have tried 7 different tutorials trying to make a simple API to serialize and deserialize data into and from a data base. Not a single one compiles. The one that got the closest for working is https://guimauve.io/articles/50 (even the early backend part fails completely to compile ) but I get errors saying that types do not have the generic parameters used int he tutorial and that serde::Serialzie trait is not fulfilled by a type that is not even in the code the tutorial provides. The other 6 tutorials have more than 40 error messages each. I got the last few weeks into a routine of looking for tutorials following it and finding that each one doe snot compile for a different reason.
It seems Rust libraries change almost overnight into total incompatibility and I am suffering with criptic messages of errors that made me want to go back into writing ASM. (I did wrote an equivalent solution in just 3 days of the whole thing in C++ without any framework except boost, so I cannot fathom why these libraries need to be so convoluted and change so much)
I am really starting to hate Rust, not because of the language but due to the environment being so chaotic. Can please someone point me for a tutorial that does compile and work? (my current rustc version is 1.66.0)
Please? I really do not want to develop a hatred for Rust but my willpower is starting to end.
Hi,
My r/rust learning journey continues with full energy (ok sometimes I am lazy and don't do anything)!
This is probably my biggest learning project so far. Creating a REST API for the famous TODO app using the Actix Web framework.
I used an in-memory "database" for the moment, as I plan to tackle the usage of ORM and Rust in one of my next learnings.
Question: Should I use Diesel.rs with a PostgreSQL database?
https://blog.ediri.io/rust-development-creating-a-rest-api-with-actix-web-for-beginners
I have the following actix
test app:
#[post("/split")]
async fn split(input: String) -> Json<Vec<String>> {
let result: Vec<String> = Regex::new(r" +")
.unwrap()
.split(&input)
.map(|x| String::from(x))
.collect();
Json(result)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new().service(split)
})
.workers(100)
.bind(("127.0.0.1", 8080))
?.run()
.await
}
And I have the functionally equivalent Spring Boot endpoint:
@RestController
public class SplitController {
private final Pattern pattern = Pattern.compile(" +");
@PostMapping(path = "/split")
public List<String> split(@RequestBody String input) {
return List.of(pattern.split(input));
}
}
I have a large text that I am submitted in the benchmark. For Spring Boot, the average response time is 119 ms. For the actix version, the average response time is a whopping 921 ms.
I just started learning Rust a few days ago, so I likely might be doing something horribly wrong.
My first thought was that compiling the regular expression every time could be the problem, but Java does this as well. So, I am clueless as to the speed difference.
What am I doing wrong or what is the deal?
UPDATE:
Based on the great feedback that I got, here is the new code:
static REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r" +").unwrap());
#[post("/split")]
async fn split(input: String) -> HttpResponse {
let result: Vec<&str> = REGEX
.split(&input)
.collect();
HttpResponse::Ok().json(result)
}
I also updated the Spring Boot code to compile the regular expression only once:
@RestController
public class SplitController {
private final Pattern pattern = Pattern.compile(" +");
@PostMapping(path = "/split")
public List<String> split(@RequestBody String input) {
return List.of(pattern.split(" +"));
}
}
The average response time over 100,000 requests:
Spring Boot - 143 ms, 1 GB RAM. I was able to get this app to run stably at 200 MB of RAM, but the average response time was 422 ms. This is with the default embedded Tomcat server. Less RAM and it starts failing.
actix - 58 ms, 80 MB RAM. There could still be some tuning for actix that I don't know about.
Thanks to everyone who helped out this Rust noob!
UPDATE 2:
Special thanks to u/Imaginos_In_Disguise for pointing out a copy-paste error I did in the Spring Boot code that was artificially making it faster because it was not actually processing the payload. After fixing that, the Spring Boot code was WAY, WAY slower.
Hey r/rust!
We've got some exciting news for you! Shuttle just got a whole lot better with the latest release. Here's what's new:
Actix support: we’ve added support for the actix framework, meaning you’ll be able to deploy your actix app in no-time.
Static file support: shuttle now supports serving static files like HTML, CSS, and JavaScript which makes it possible for you to deploy the whole package, rather than just the backend.
Public dependencies: it's now super easy to include public dependencies in your shuttle projects. Just specify them in your Cargo.toml file and we'll take care of the rest.
Better scaffolding: the CLI now has a completely interactive starter flow!
And more: we've also included several other improvements and bug fixes.
The full release notes can be found here: https://github.com/shuttle-hq/shuttle/releases/tag/v0.8.0
We can't wait to see what you build with these new features. Let us know what you think in the comments!
Is it normal that memory usage keeps going up with each request and doesn't go down? Will it eventually plateau? I built a web server with Rust and Actix. The CPU usage stays at less than 1% but the memory usage climbs and doesn't seem to plateau. It never goes back down even days after it has been hit. Is this normal behavior or is there some sort of issue? Any idea what the issue might be? I could be worried about nothing and Actix could just be warming up to a steady memory usage state. I am new to Rust and Actix. Any guidance would help a ton. Thank you!
https://i.redd.it/4nxe080axqq91.png