Async vs Sync handlers #2884
-
I am implementing api endpoints for my web app, and I am not sure whether to make the handlers async or sync. The endpoints mostly consist of calls to other function that do basic CRUD operations. I have made them async right now purely based on intuition, but I don't actually know what should I opt for as having both async and sync handlers seems to not make a difference. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hi @valkrypton , If you use |
Beta Was this translation helpful? Give feedback.
-
Rocket starts a |
Beta Was this translation helpful? Give feedback.
Rocket starts a
tokio
runtime, and every method is executed on the async runtime. The general advice I would give, is that you should always usetokio
types for doing any kind of IO, especially network/file IO. Handlers only need to be marked as async if you use any of these types, and call.await
inside them. Clippy has a warning forasync
functions that don't use.await
, but it's up to you whether you remove theasync
marker from your functions that don't need it.