#job #task #sharing #proc-macro

nightly macro mula_proc_macro

Proc macro for Mula, share expensive computation with various requesters

1 unstable release

0.1.0 Oct 13, 2020

#53 in #sharing

25 downloads per month
Used in mula

MIT license

5KB

mula

Mula provides a way of having several requests for a given computation be serviced without duplicating the actual computation.

Imagine you have a REST API with an endpoint that causes an expensive computation. If that endpoint receives more requests while the first one is being computed it would be nice if you could just let those new requests wait in line for the computation to end and give them all the same result.

That is what Mula allows you to do.

Example

The following example will only run the computation closure twice, once for each of the distinct inputs. The two subscriptions for "burro" will both be serviced by the same computation.

use mula::mula;

#[mula]
fn delayed_uppercase(input: &'static str) -> String {
    std::thread::sleep(std::time::Duration::from_secs(2));
    input.to_uppercase()
}

let thread1 = std::thread::spawn(move || {
    let upper = delayed_uppercase("mula");
    assert_eq!(upper, "MULA".to_string());
});

let thread2 = std::thread::spawn(move || {
    let upper = delayed_uppercase("burro");
    assert_eq!(upper, "BURRO".to_string());
});

let thread3 = std::thread::spawn(move || {
    let upper = delayed_uppercase("burro");
    assert_eq!(upper, "BURRO".to_string());
});

thread1.join();
thread2.join();
thread3.join();

License: MIT

Dependencies

~1.5MB
~32K SLoC