| 1 | Title: Limiters |
| 2 | |
| 3 | # Overview |
| 4 | |
| 5 | [[email protected]] bounds the number of asynchronous operations that may run |
| 6 | at once. Use it when a workload can produce more parallel work than a service, |
| 7 | subsystem, or machine should handle at the same time. |
| 8 | |
| 9 | Typical uses include indexing files, downloading URLs, generating thumbnails, |
| 10 | parsing documents, or querying a service with a known concurrency budget. |
| 11 | |
| 12 | ## Running Work |
| 13 | |
| 14 | The safest API is [[email protected]]. It acquires a permit, spawns a |
| 15 | fiber, and releases the permit when that fiber completes. |
| 16 | |
| 17 | ```c |
| 18 | static DexFuture * |
| 19 | load_one_file (gpointer user_data) |
| 20 | { |
| 21 | GFile *file = user_data; |
| 22 | |
| 23 | return dex_file_load_contents_bytes (file); |
| 24 | } |
| 25 | |
| 26 | DexLimiter *limiter = dex_limiter_new (8); |
| 27 | DexFuture *future = dex_limiter_run (limiter, |
| 28 | NULL, |
| 29 | 0, |
| 30 | load_one_file, |
| 31 | g_object_ref (file), |
| 32 | g_object_unref); |
| 33 | ``` |
| 34 | |
| 35 | The returned future resolves or rejects with the result of the spawned fiber. |
| 36 | If many calls to [[email protected]] are made, only the configured number |
| 37 | will run at once. Once a fiber has started, dropping the returned future does |
| 38 | not stop the fiber; it is allowed to complete so the permit can be released. |
| 39 | |
| 40 | Use [[email protected]_on_pool] when the limited work should run on a |
| 41 | [[email protected]] instead of a fiber. This is useful for blocking or |
| 42 | foreign APIs that should not run on a scheduler thread. |
| 43 | |
| 44 | ```c |
| 45 | static DexFuture * |
| 46 | run_blocking_thumbnailer (gpointer user_data) |
| 47 | { |
| 48 | ThumbnailJob *job = user_data; |
| 49 | |
| 50 | return thumbnail_job_run_blocking (job); |
| 51 | } |
| 52 | |
| 53 | DexLimiter *limiter = dex_limiter_new (4); |
| 54 | DexThreadPool *pool = dex_thread_pool_new (4); |
| 55 | DexFuture *future = dex_limiter_run_on_pool (limiter, |
| 56 | pool, |
| 57 | run_blocking_thumbnailer, |
| 58 | thumbnail_job_ref (job), |
| 59 | thumbnail_job_unref); |
| 60 | ``` |
| 61 | |
| 62 | The returned future resolves or rejects with the result of the submitted |
| 63 | thread-pool work. Once the work has been submitted, dropping the returned |
| 64 | future does not stop the work; it is allowed to complete so the permit can be |
| 65 | released. |
| 66 | |
| 67 | For stackless async workflows, use [[email protected]_coroutine]. This |
| 68 | acquires a permit and then runs a [[email protected]] from a scheduler. |
| 69 | |
| 70 | ```c |
| 71 | static DexFuture * |
| 72 | load_one_cache (DexCoroutineContext *context, |
| 73 | gpointer user_data) |
| 74 | { |
| 75 | DEX_COROUTINE_BEGIN (context); |
| 76 | |
| 77 | DEX_COROUTINE_END (); |
| 78 | } |
| 79 | |
| 80 | DexLimiter *limiter = dex_limiter_new (4); |
| 81 | DexFuture *future = dex_limiter_run_coroutine (limiter, |
| 82 | NULL, |
| 83 | load_one_cache, |
| 84 | g_object_ref (key), |
| 85 | g_object_unref); |
| 86 | ``` |
| 87 | |
| 88 | `load_one_cache` uses stackless coroutine style and avoids allocating a fiber |
| 89 | stack while still participating in limiter scoping. |
| 90 | |
| 91 | ## Manual Scopes |
| 92 | |
| 93 | Use [[email protected]] and [[email protected]] when a permit |
| 94 | must cover a custom scope that is not a single fiber. |
| 95 | |
| 96 | ```c |
| 97 | g_autoptr(GError) error = NULL; |
| 98 | |
| 99 | if (dex_await (dex_limiter_acquire (limiter), &error)) |
| 100 | { |
| 101 | do_limited_work (); |
| 102 | dex_limiter_release (limiter); |
| 103 | } |
| 104 | ``` |
| 105 | |
| 106 | Release exactly once for every successful acquisition. Prefer |
| 107 | [[email protected]] when possible because it handles release on both |
| 108 | success and failure paths. |
| 109 | |
| 110 | ## Choosing a Limit |
| 111 | |
| 112 | Choose a limit that matches the constrained resource, not the number of items |
| 113 | in the queue. |
| 114 | |
| 115 | - Use a small value for remote services, databases, and APIs with rate limits. |
| 116 | - Use a value near the number of useful worker threads for CPU-heavy work. |
| 117 | - Use a larger value for I/O-heavy local work if the underlying storage or |
| 118 | service benefits from parallelism. |
| 119 | - Keep separate limiters for unrelated resources so one workload does not |
| 120 | consume another workload's budget. |
| 121 | |
| 122 | ## Shutdown |
| 123 | |
| 124 | Call [[email protected]] when no new work should start. Pending and |
| 125 | future acquisitions reject with [[email protected]_CLOSED]. Work that |
| 126 | already holds a permit may continue, but releasing after close does not make new |
| 127 | permits available. |
| 128 | |
| 129 | If you need to wait for graceful shutdown, call |
| 130 | [[email protected]_after_drain], which both closes the limiter and |
| 131 | returns a future that resolves once all queued acquires have settled and all |
| 132 | running holders have released. |