1 | | | part of 'worker_pool.dart'; |
2 | | |
|
3 | | | class _PoolWorker<W extends Worker> { |
4 | | 2 | _PoolWorker(this.worker, this._maxWorkload) : _capacity = _maxWorkload; |
5 | | |
|
6 | | | final W worker; |
7 | | |
|
8 | | | final int _maxWorkload; |
9 | | | int? _lastStart; |
10 | | | int _capacity; |
11 | | |
|
12 | | 3 | int get capacity => _capacity; |
13 | | 7 | bool get isIdle => worker.isStopped || _capacity == _maxWorkload; |
14 | | |
|
15 | | 1 | void _start() { |
16 | | 4 | _lastStart = DateTime.now().millisecondsSinceEpoch; |
17 | | 2 | _capacity--; |
18 | | | } |
19 | | |
|
20 | | 2 | void _finish() { |
21 | | 2 | _capacity++; |
22 | | 4 | if (_capacity == _maxWorkload) { |
23 | | 2 | _lastStart = null; |
24 | | | } |
25 | | 1 | } |
26 | | |
|
27 | | 1 | Future run(WorkerTask task) { |
28 | | 1 | _start(); |
29 | | 5 | return task.run(worker).whenComplete(_finish); |
30 | | | } |
31 | | |
|
32 | | 2 | static int compareCapacityDesc(_PoolWorker a, _PoolWorker b) { |
33 | | 7 | if (a.capacity != b.capacity) return b.capacity.compareTo(a.capacity); |
34 | | 2 | if (a._lastStart == null) return 1; |
35 | | 2 | if (b._lastStart == null) return -1; |
36 | | 4 | return a._lastStart!.compareTo(b._lastStart!); |
37 | | 1 | } |
38 | | |
|
39 | | 4 | static bool isStopped(_PoolWorker w) => w.worker.isStopped; |
40 | | 4 | static WorkerStat getStats(_PoolWorker w) => w.worker.stats; |
41 | | | } |