How we migrate our framework into async/await
Wai Pai Lee
Co-author of ObsidianFirstly, congratulation on Rust lang achieving stable async/await syntax! As of the release, async/await is becoming the preferred way to do asynchronous programming instead of using Futures in Rust lang.
In Obsidian Web Framework, we do the same move just like other libraries which enabling async/await syntax in order to provide a better development experience.
*Rust 1.40 is used in this article
In case you don't know async/await in Rust
Why async/await?
Code complexity - You may design the code in the synchronous way.
Readability - Avoid and_then
and then
chaining which making code hard to read.
Lifetime - Lifetime is following synchronous flow within async
function.
In Obsidian, we treat the developer's development experience as the first priority goal. Thus, migration to async/await enabled structure is definitely needed.
Changing futures to async/await
The process is pretty simple for most of the cases. Basically, we just add async to the function and remove then
,and_then
and the dangerous wait
to await
. From the example below, it shows that the code is much cleaner and readable after using async/await.
from
to
Using async/await in trait and closure
In current Rust version, the async
syntax does not support trait function and closure yet. It will be supported in the future. So, we need to do it another way around which is like this.
It seems complicated for a trait but luckily Rust community create a crate to simplify this which is called as async-trait
. By using this crate, we are able to use traits with asyncsyntax!
You may notice that this trait workaround method will result in a heap allocation per-function-call. So why do we use it? In Obsidian, we aim for delivering a better development experience. We believe that enabling await for traits like Middleware
ease the development process. Besides that, we also trust the Rust community will enable the use of async
in trait soon. 😀
For more information on the trade-off, visit Asynchronous Programming in Rust book.
How to test an async API?
Sadly, now test function cannot use async
syntax. Thus, we need to make it synchronous. In our framework, we use async-std
task blocking feature to make it happen.
Lifetime issue
Although async/await seems perfect, it can be hard when dealing with self
or struct outside of async block lifetime. In some cases, the developer might want to call an async implementation within the same struct. In this case, the compiler will not let you go. For instance:
The simplest way will be simply cloning the struct before async block call which is used above. However, the system will have the scalability issue. In Obsidian, the clone method decreased performance by ~25% with the sample of 1 endpoint router and 20 endpoint router. So, we decided to go for another approach by moving out the usage of self
into synchronous part and move its ownership into the async block for the async process.
About Obsidian
Obsidian is a web development framework built in Rust which vision to lower down the learning curve of web development in Rust without losing the advantage of it. Currently, Obsidian is under active development and we expected to release v0.2 in this couple of months.