uniffi-rs icon indicating copy to clipboard operation
uniffi-rs copied to clipboard

Foreign BackgroundQueue type

Open bendk opened this issue 2 years ago • 4 comments

A common pattern we use in application-services is to run SQLite queries using a foreign-managed task queue (Dispatchers.IO on Kotlin, DispatchQueue on Swift, etc). Could UniFFI support this as a first-class type?

This would require:

  • Moving it across the FFI (only supporting Foreign -> Rust is probably fine)
  • Adding way to execute Rust synchronous code using the foreign DispatchQueue in two modes:
    • async mode where you await the result
    • fire-and-forget mode where you just want the code to run and don't care about the result.

bendk avatar Sep 05 '23 16:09 bendk

I think BackgroundQueue is a nicer name than DispatchQueue, since the feature is really about running tasks in a background thread. I feel like there could be a better name though, open to any suggestions.

bendk avatar Nov 03 '23 16:11 bendk

"background thread" implies "low priority thread" to me, which I don't think is the actual intent. The actual use-cases here tend to be more about thread blocking than thread priority. I was going to suggest something like TaskQueue, but depending on the content the Task part of that might itself carry baggage. WorkQueue? But I don't think it matters that much though.

mhammond avatar Nov 03 '23 18:11 mhammond

You're right, it really is about blocking more than anything. tokio separates their threads into "worker threads" and "blocking threads". What about BlockingQueue or BlockingTaskQueue?

bendk avatar Nov 03 '23 19:11 bendk

using the same terminology as tokio is certainly appealing!

mhammond avatar Nov 03 '23 19:11 mhammond