r/rust•
Programmurr

actix-web v2.0 released

Features use of std futures and async-await across the actix ecosystem.

I spent hours replacing complicated chained futures with simple, intuitive async-await and can testify that this new paradigm is a game changer. No more Either. No more folding. Far fewer clones. Lots of borrowing.

Much, much better experience and hours of experience using combinators that I may never touch again.

Posted Dec 25, 2019, 6:55 PM UTC

37934
somebodddy•

I understand only actix-web moved to async-await? Any plans to move actix itself to it?

31
jess-sch•

I'm really not sure how actix is supposed to move over until we have async fn (and therefore GATs) in traits.

The boxing cost of the async-trait workaround crate would most likely be way too high, an Actor model framework would cross trait boundaries way too often for that to reasonably work.

23
somebodddy•

If you want to use futures in actix you already need to box them. impl Trait doesn't work for methods for the same reason async doesn't.

12
Matthias247•

The boxing cost of the async-trait workaround crate would most likely be way too high, an Actor model framework would cross trait boundaries way too often for that to reasonably work.

Can you explain what you think is too high, and why it is the case? Actor systems in general are far from fast. They often involve heap allocation and boxing of message objects, enqueuing them into a remote actors mailbox, scheduling the actor, executing the code there (with dynamic/table driven dispatch on a message type) and then going all the way back to deliver a response if necessary. Actix might or might not need all of those steps (I'm not familiar with it). But even if it would be super efficient for an actor framework, it would be doubtful that the cost of one or a couple more trait object calls is important. Keep in mind that trait objects and dynamic dispatch in general are still super efficient on modern CPUs. The main cost that is sometimes referred to is that trait objects prevent certain optimizations (e.g. inlining) - which may or may not be important in a given piece of code.

4
mamcx•

So how to deal with databases in actix-web? Wait until all is async is not feasible for me.

3
hwchen•

Use web::block. I think this example should show it.

7
Apothum•

I’ve been using that for my current project. I am about to try https://github.com/mehcode/actix-diesel?files=1 this to see if it makes it any better.

1
insanitybit•

Slower than a sync method?

2
Matthias247•

That's hard to answer, because everything is a sync method - There is no such thing as a natural async method 😀 Async methods work by calling multiple synchronous methods.

Based on the assumption that you mean thread-blocking synchronous methods the performance will still depend on the actual use-case. It might or might not be faster than a e.g. using an asynchronous message passing system.

3
jess-sch•

executing a sync function is just as fast as executing an async function. The problem async solves is that it allows us to do other things while waiting for certain I/O syscalls to complete - in the slow portions, we're not really running the function, we're waiting for the operating system to wake up our thread.

The added overhead of Box<dyn Future> is in the creation and disposal of the future, not the execution.

2
insanitybit•

I wasn't trying to imply that async was somehow a 'faster' method. I'm also aware of what Box<dyn Future> implies performance-wise; the RFC covers it well https://github.com/rust-lang/rfcs/pull/1603

I mean, for Actix, a web service library, I expect async to be a significant speedup. For a method that would be async (because it makes sense) but the choice is dynamic dispatch or sync, I expect dynamic dispatch to be the right call (again, with the assumption being that the underlying method is capable of async, especially for a web framework).

Of course it's use case dependent.

1
mytempacc3•

Yep. "most likely be way too high" is a strong claim.

0
orangepantsman•

IIUC, the actix 0.9 is on futures 0.3, which makes it compatible.

13
jess-sch•

I don't think that's enough though, is it? The traits you have to implement for Actors use regular sync functions, because async fn isn't supported yet in traits.

9
orangepantsman•

So I was specifically thinking of the future returned by sending a message to an actor. And that's an await compatible 0.3 future now.

So it works for my use case, which is having an actor be in charge of a data structure, and communicating with it via messages. I was abke to upgrade to actix web 2, and use tbe new await syntax when interacting with actors (I have no need for it within actor implementations)

2
somebodddy•

Regular futures have a limitation - they don't allow you to reborrow the actor. actix worked around this by introducing ActorFuture, which have not moved to async-await yet.

async-await has the same problem of keeping the actor borrowed across .await points. Solving this - whatever the solution would be - is going to require some design. Have the maintainers decided already on how they want to do this?

8
GreatSt•
23
__ah•

Also to see some of the changes using the new syntax and futures: https://github.com/actix/actix-web/compare/1.0...master

4
kaikalii•

Thank you for your great work! Having used actix-web 1.x quite a bit, trait-compliant future chaining was always the most complicated part for me. Anything to simplify it is greatly appreciated.

11
ProgrammurrOP•

I don't know if Nikolay reads reddit anymore so you may want to contact him elsewhere

7
saiumesh535•

I’ve used actix for couple of hobby projects. While it’s great both in terms of usability and performance except it’s middlewares. Middleware implementation in actix so verbose and confusing, hope they make it better.

10
ProgrammurrOP•

This is as good as it will get with the Service-based architecture, actix-net. Confusion comes from not understanding how the architecture works and not understanding Rust concepts. I still don't understand all of the possible permutations that exist and learn them as needed. Pinning hasn't made this easier in v2.

I think the community needs more tutorials and guides. Now that the architecture is stable, it is a good time for people to take up that task.

4
Shnatsel•

Oh, so actix-web does depend on actix-service after all? Then they inherit the unsound custom Cell implementation and the impact of that bug is much bigger than I thought.

2†
ProgrammurrOP•

I am not interested in defending Nikolay. He decides what is best for his work. For my work, there remains no better option in the Rust ecosystem.

I remain skeptical of the usual hyperbolic claims, but can be easily convinced with a qualified, coherent explanation with evidence of the contrary. Yet, I am not the one who needs convincing, as Nikolay is the author with his own criteria that would compel him to sacrifice performance for safety. Performance remains the key reason for the unsafe blocks. As per the safety dance charter, changing std Rust so as to eliminate the need for unsafe seems like the best course of action, but this work is beyond my comprehension. :-[ Yet, it seems to be not beyond yours!

5
Shnatsel•

I am completely disinterested in the people aspect of this, I want the code fixed. Actix-service didn't seem to be in the dependency tree for actix-web, so I kind of deprioritized this particular issue because actix-net didn't have any high-profile users.

Could you clarify if actix-service is used by actix-web or not? If so I'll bump it on my priority list.

2†
zxgx•

What are folks using for async database with actix web 2.0x?

Tokio-diesel seems to work well, but I'm sure there are other good alternatives!

8
ProgrammurrOP•

if you're using postgres, tokio-postgres for native async calls, or else use actix-web's web::block facility

2
moh4abk•

Migrated two micro services in 1 hr. very easy to get into. The updated examples were a real helper.

4
ProgrammurrOP•

Congrats! It didn't feel good replacing async work that took weeks of effort, but that effort was a sunk cost. Refactoring is for the best..

1
AndreVallestero•

It's always impressive to see how quickly actix has matured compared to it's alternatives. It's consistently the fastest across multiple benchmarks and it's only been in development for a few years. I'm looking forward to seeing how much more it will grow in the future.

1
ProgrammurrOP•

Probably won't evolve that much more..

1