|
| 1 | +--- |
| 2 | +title: Coroutine Pool Overview |
| 3 | +date: 2025-01-18 10:00:00 |
| 4 | +author: loongs-zhang |
| 5 | +--- |
| 6 | + |
| 7 | +# Coroutine Pool Overview |
| 8 | + |
| 9 | +## Usage |
| 10 | + |
| 11 | +```rust |
| 12 | +use open_coroutine_core::co_pool::CoroutinePool; |
| 13 | + |
| 14 | +fn main() -> std::io::Result<()> { |
| 15 | + let mut pool = CoroutinePool::default(); |
| 16 | + assert!(pool.is_empty()); |
| 17 | + pool.submit_task( |
| 18 | + Some(String::from(task_name)), |
| 19 | + |_| { |
| 20 | + println!("Hello, world!"); |
| 21 | + Some(2) |
| 22 | + }, |
| 23 | + None, |
| 24 | + None, |
| 25 | + )?; |
| 26 | + assert!(!pool.is_empty()); |
| 27 | + pool.try_schedule_task() |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +## Why coroutine pool? |
| 32 | + |
| 33 | +Pooling the coroutines can bring several significant advantages: |
| 34 | + |
| 35 | +1. Resource management: The coroutine pool can manage the creation, destruction, and reuse of coroutines. By using a |
| 36 | + coroutine pool, a certain number of coroutines can be created in advance and stored in the pool for use when needed. |
| 37 | + This can avoid frequent creation and destruction of coroutines, reduce unnecessary resource waste, and improve system |
| 38 | + performance. |
| 39 | + |
| 40 | +2. Avoid coroutine hunger: When using a coroutine pool, coroutines will be continuously provided with tasks, avoiding |
| 41 | + the situation where coroutines are idle after completing tasks. |
| 42 | + |
| 43 | +3. Concurrency control: By setting the parameters of the coroutine pool, the number of concurrent coroutines can be |
| 44 | + limited to avoid overloading the system due to too many coroutines. |
| 45 | + |
| 46 | +4. Improve code maintainability: Using coroutine pools can separate task execution from coroutine management, making the |
| 47 | + code clearer and more maintainable. The execution logic of a task can be focused on the task itself, while the |
| 48 | + creation and management of coroutines are handled by the coroutine pool. |
| 49 | + |
| 50 | +## How it works |
| 51 | + |
| 52 | +In open-coroutine-core, the coroutine pool is lazy, which means if you don't call `try_timeout_schedule_task`, tasks |
| 53 | +will not be executed. Please refer to the sequence diagram below for details: |
| 54 | + |
| 55 | +```mermaid |
| 56 | +sequenceDiagram |
| 57 | + Actor Schedule Thread |
| 58 | + participant CoroutinePool |
| 59 | + participant WorkerCoroutine |
| 60 | + participant Task |
| 61 | + participant CoroutineCreator |
| 62 | +
|
| 63 | + Schedule Thread ->>+ CoroutinePool: CoroutinePool::try_timeout_schedule_task |
| 64 | + alt the coroutine pool is stopped |
| 65 | + CoroutinePool ->>+ Schedule Thread: return error |
| 66 | + end |
| 67 | + alt the task queue in the coroutine pool is empty |
| 68 | + CoroutinePool ->>+ Schedule Thread: return success |
| 69 | + end |
| 70 | + alt create worker coroutines |
| 71 | + CoroutinePool ->>+ WorkerCoroutine: create worker coroutines only if the coroutine pool has not reached its maximum pool size |
| 72 | + end |
| 73 | + CoroutinePool ->>+ WorkerCoroutine: schedule the worker coroutines |
| 74 | + alt run tasks |
| 75 | + WorkerCoroutine ->>+ Task: try poll a task |
| 76 | + alt poll success |
| 77 | + Task ->>+ Task: run the task |
| 78 | + alt in execution |
| 79 | + Task ->>+ WorkerCoroutine: be preempted or enter syscall |
| 80 | + WorkerCoroutine ->>+ WorkerCoroutine: The coroutine state changes to Suspend/Syscall |
| 81 | + WorkerCoroutine ->>+ CoroutineCreator: Listener::on_state_changed |
| 82 | + CoroutineCreator ->>+ WorkerCoroutine: create worker coroutines only if the coroutine pool has not reached its maximum pool size |
| 83 | + end |
| 84 | + alt run success |
| 85 | + Task ->>+ WorkerCoroutine: Task exited normally |
| 86 | + end |
| 87 | + alt run fail |
| 88 | + Task ->>+ WorkerCoroutine: Task exited abnormally |
| 89 | + WorkerCoroutine ->>+ WorkerCoroutine: The coroutine state changes to Error |
| 90 | + WorkerCoroutine ->>+ CoroutineCreator: Listener::on_state_changed |
| 91 | + CoroutineCreator ->>+ CoroutineCreator: reduce the current coroutine count |
| 92 | + CoroutineCreator ->>+ WorkerCoroutine: recreate worker coroutine only if the coroutine pool has not reached its maximum pool size |
| 93 | + end |
| 94 | + end |
| 95 | + alt poll fail |
| 96 | + Task ->>+ WorkerCoroutine: increase count and yield to the next coroutine |
| 97 | + WorkerCoroutine ->>+ WorkerCoroutine: block for a while if the count has reached the current size of coroutine pool |
| 98 | + end |
| 99 | + WorkerCoroutine ->>+ WorkerCoroutine: try poll the next task |
| 100 | + end |
| 101 | + alt recycle coroutines |
| 102 | + WorkerCoroutine ->>+ WorkerCoroutine: the schedule has exceeded the timeout time |
| 103 | + WorkerCoroutine ->>+ CoroutinePool: has the coroutine pool exceeded the minimum pool size? |
| 104 | + CoroutinePool ->>+ WorkerCoroutine: yes |
| 105 | + WorkerCoroutine ->>+ WorkerCoroutine: exit |
| 106 | + end |
| 107 | + WorkerCoroutine ->>+ CoroutinePool: return if timeout or schedule fail |
| 108 | + CoroutinePool ->>+ Schedule Thread: This schedule has ended |
| 109 | + Schedule Thread ->>+ Schedule Thread: ...... |
| 110 | +``` |
0 commit comments