@@ -22,6 +22,62 @@ This crate follows the [Unified API] guidelines and is centered around
2222the concepts of [ ` Client ` ] , [ ` Hub ` ] and [ ` Scope ` ] , as well as the extension
2323points via the [ ` Integration ` ] , [ ` Transport ` ] and [ ` TransportFactory ` ] traits.
2424
25+ ## Parallelism, Concurrency and Async
26+
27+ The main concurrency primitive is the [ ` Hub ` ] . In general, all concurrent
28+ code, no matter if multithreaded parallelism or futures concurrency, needs
29+ to run with its own copy of a [ ` Hub ` ] . Even though the [ ` Hub ` ] is internally
30+ synchronized, using it concurrently may lead to unexpected results up to
31+ panics.
32+
33+ For threads or tasks that are running concurrently or outlive the current
34+ execution context, a new [ ` Hub ` ] needs to be created and bound for the computation.
35+
36+ ``` rust
37+ use rayon :: prelude :: * ;
38+ use sentry :: {Hub , SentryFutureExt };
39+ use std :: sync :: Arc ;
40+
41+ // Parallel multithreaded code:
42+ let outer_hub = Hub :: current ();
43+ let results : Vec <_ > = [1_u32 , 2 , 3 ]
44+ . into_par_iter ()
45+ . map (| num | {
46+ let thread_hub = Arc :: new (Hub :: new_from_top (& outer_hub ));
47+ Hub :: run (thread_hub , || num * num )
48+ })
49+ . collect ();
50+
51+ assert_eq! (& results , & [1 , 4 , 9 ]);
52+
53+ // Concurrent futures code:
54+ let futures = [1_u32 , 2 , 3 ]
55+ . into_iter ()
56+ . map (| num | async move { num * num }. bind_hub (Hub :: new_from_top (Hub :: current ())));
57+ let results = futures :: future :: join_all (futures ). await ;
58+
59+ assert_eq! (& results , & [1 , 4 , 9 ]);
60+ ```
61+
62+ For tasks that are not concurrent and do not outlive the current execution
63+ context, no * new* [ ` Hub ` ] needs to be created, but the current [ ` Hub ` ] has
64+ to be bound.
65+
66+ ``` rust
67+ use sentry :: {Hub , SentryFutureExt };
68+
69+ // Spawned thread that is being joined:
70+ let hub = Hub :: current ();
71+ let result = std :: thread :: spawn (|| Hub :: run (hub , || 1_u32 )). join ();
72+
73+ assert_eq! (result . unwrap (), 1 );
74+
75+ // Spawned future that is being awaited:
76+ let result = tokio :: spawn (async { 1_u32 }. bind_hub (Hub :: current ())). await ;
77+
78+ assert_eq! (result . unwrap (), 1 );
79+ ```
80+
2581## Minimal API
2682
2783By default, this crate comes with a so-called "minimal" mode. This mode will
@@ -46,13 +102,7 @@ functionality.
46102[ Sentry ] : https://sentry.io/
47103[ `sentry` ] : https://crates.io/crates/sentry
48104[ Unified API ] : https://develop.sentry.dev/sdk/unified-api/
49- [ `Client` ] : https://docs.rs/sentry-core/0.27.0/sentry_core/struct.Client.html
50- [ `Hub` ] : https://docs.rs/sentry-core/0.27.0/sentry_core/struct.Hub.html
51- [ `Scope` ] : https://docs.rs/sentry-core/0.27.0/sentry_core/struct.Scope.html
52- [ `Integration` ] : https://docs.rs/sentry-core/0.27.0/sentry_core/trait.Integration.html
53- [ `Transport` ] : https://docs.rs/sentry-core/0.27.0/sentry_core/trait.Transport.html
54- [ `TransportFactory` ] : https://docs.rs/sentry-core/0.27.0/sentry_core/trait.TransportFactory.html
55- [ `test` ] : https://docs.rs/sentry-core/0.27.0/sentry_core/test/index.html
105+ [ `test` ] : https://docs.rs/sentry-core/0.28.0/sentry_core/test/index.html
56106
57107## Resources
58108
0 commit comments