1 | | | /// Concurrency settings governing parallelization of workers in a [WorkerPool]. |
2 | | | class ConcurrencySettings { |
3 | | 4 | const ConcurrencySettings( |
4 | | | {this.minWorkers = 0, this.maxWorkers = 0, this.maxParallel = 1}) |
5 | | 2 | : assert(minWorkers >= 0), |
6 | | 2 | assert(maxWorkers >= 0), |
7 | | 3 | assert(minWorkers <= maxWorkers), |
8 | | 3 | assert(maxParallel >= 1); |
9 | | |
|
10 | | | /// Minimum number of workers in the pool. |
11 | | | final int minWorkers; |
12 | | |
|
13 | | | /// Maximum number of workers in the pool. |
14 | | | /// If this is set to 0, the number of workers is unbounded (as a result, any task posted to the pool will be assigned a worker asap). |
15 | | | final int maxWorkers; |
16 | | |
|
17 | | | /// Maximum number of tasks that can be posted to a worker. |
18 | | | final int maxParallel; |
19 | | |
|
20 | | | /// Maximum number of running tasks. |
21 | | 4 | int get maxConcurrency => maxWorkers * maxParallel; |
22 | | |
|
23 | | | /// Returns the number of workers necessary to handle [pending] tasks, taking into account [minWorkers] and [maxWorkers]. |
24 | | 2 | int max(int pending) { |
25 | | 4 | if (pending < minWorkers) return minWorkers; |
26 | | 5 | if (pending < maxWorkers || maxWorkers == 0) return pending; |
27 | | 2 | return maxWorkers; |
28 | | 1 | } |
29 | | |
|
30 | | | /// Returns the number of workers necessary to have at least [minLive] live workers, taking into account [minWorkers] and [maxWorkers]. |
31 | | 2 | int min(int minLive) { |
32 | | 4 | if (minLive < minWorkers) return minWorkers; |
33 | | 2 | return max(minLive); |
34 | | 1 | } |
35 | | |
|
36 | | | /// 50 tasks per worker with at most 1 worker. |
37 | | | static const oneIoThread = |
38 | | | ConcurrencySettings(minWorkers: 0, maxWorkers: 1, maxParallel: 50); |
39 | | |
|
40 | | | /// 50 tasks per worker with at most 2 workers. |
41 | | | static const twoIoThreads = |
42 | | | ConcurrencySettings(minWorkers: 0, maxWorkers: 2, maxParallel: 50); |
43 | | |
|
44 | | | /// 50 tasks per worker, 1 to 4 workers. |
45 | | | static const fourIoThreads = |
46 | | | ConcurrencySettings(minWorkers: 1, maxWorkers: 4, maxParallel: 50); |
47 | | |
|
48 | | | /// 50 tasks per worker, 1 to 8 workers. |
49 | | | static const eightIoThreads = |
50 | | | ConcurrencySettings(minWorkers: 1, maxWorkers: 8, maxParallel: 50); |
51 | | |
|
52 | | | /// 1 task per worker, 1 to 3 workers. |
53 | | | static const threeCpuThreads = |
54 | | | ConcurrencySettings(minWorkers: 1, maxWorkers: 3, maxParallel: 1); |
55 | | |
|
56 | | | /// 1 tasks per worker, 1 to 7 workers. |
57 | | | static const sevenCpuThreads = |
58 | | | ConcurrencySettings(minWorkers: 1, maxWorkers: 7, maxParallel: 1); |
59 | | | } |