1 | | | import 'cancellation_token.dart'; |
2 | | | import 'worker_exception.dart'; |
3 | | | import 'worker_pool.dart'; |
4 | | | import 'worker_request.dart'; |
5 | | | import 'worker_service.dart'; |
6 | | | import 'worker_task.dart'; |
7 | | |
|
8 | | | /// Cancellation token reference. This special cancellation token is managed by the [WorkerMonitor] and is used to |
9 | | | /// mirror' cancellation tokens presented to Squadron by callers of a worker service. When a [WorkerRequest] is |
10 | | | /// handled by the platform worker and that request is associated with a cancellation request, the [WorkerMonitor] |
11 | | | /// will override the request's cancellation token with a [_CancellationTokenReference]. The same cancellation may |
12 | | | /// be used for several service calls, so the [WorkerMonitor] keeps a map of [_CancellationTokenReference] and a |
13 | | | /// reference count that is incremented for each [WorkerRequest] having the same cancellation token and decremented |
14 | | | /// when processing is finished. When the reference count drops to 0 and the cancellation token was not cancelled, |
15 | | | /// the [_CancellationTokenReference] is removed from the map. |
16 | | | /// |
17 | | | /// When a caller cancels a token, a cancellation notification is sent to all workers in the pool and the |
18 | | | /// corresponding [_CancellationTokenReference] will be cancelled. Services executing in the context of a platform |
19 | | | /// worker will be able to inspect the token's status to interrupt processing gracefully. If the token's status is |
20 | | | /// not inspected, processing will continue in platform workers, but will be interrupted on caller-side with a |
21 | | | /// [CancelledException]. |
22 | | | class _CancellationTokenReference extends CancellationToken { |
23 | | 2 | _CancellationTokenReference._noToken() |
24 | | | : hasRef = false, |
25 | | 1 | super(0); |
26 | | |
|
27 | | 1 | _CancellationTokenReference(CancellationToken token) |
28 | | | : hasRef = true, |
29 | | 3 | super(token.id, token.message); |
30 | | |
|
31 | | | final bool hasRef; |
32 | | |
|
33 | | | int refCount = 0; |
34 | | |
|
35 | | 1 | @override |
36 | | 2 | CancelledException? get exception => _exception; |
37 | | | CancelledException? _exception; |
38 | | |
|
39 | | 1 | void _cancel() { |
40 | | 1 | if (hasRef) { |
41 | | 3 | _exception ??= CancelledException(message: message); |
42 | | | } |
43 | | | } |
44 | | | } |
45 | | |
|
46 | | | /// Each platform worker will instantiate a [WorkerMonitor] responsible for handling cancellation requests. Worker |
47 | | | /// tasks in Squadron may be cancelled in two ways: with a [CancellationToken], giving worker services the chance to |
48 | | | /// handle cancellation requests gracefully, or without a [CancellationToken] via [WorkerPool.cancel] or |
49 | | | /// [WorkerTask.cancel]. |
50 | | | class WorkerMonitor { |
51 | | 1 | WorkerMonitor(this._terminate); |
52 | | |
|
53 | | 4 | static final noTokenRef = _CancellationTokenReference._noToken(); |
54 | | |
|
55 | | | final SquadronCallback _terminate; |
56 | | | bool _terminationRequested = false; |
57 | | | int _executing = 0; |
58 | | |
|
59 | | | final _cancelTokens = <int, _CancellationTokenReference>{}; |
60 | | |
|
61 | | 1 | _CancellationTokenReference _getTokenRef(CancellationToken? token) => |
62 | | | (token == null) |
63 | | 1 | ? noTokenRef |
64 | | 2 | : _cancelTokens.putIfAbsent( |
65 | | 3 | token.id, () => _CancellationTokenReference(token)); |
66 | | |
|
67 | | 1 | _CancellationTokenReference begin(WorkerRequest request) { |
68 | | 2 | _executing++; |
69 | | 2 | final tokenRef = _getTokenRef(request.cancelToken); |
70 | | 1 | if (tokenRef.hasRef) { |
71 | | 2 | tokenRef.refCount++; |
72 | | 1 | request.overrideCancelToken(tokenRef); |
73 | | | } |
74 | | | return tokenRef; |
75 | | | } |
76 | | |
|
77 | | 3 | void cancel(CancellationToken token) => _getTokenRef(token)._cancel(); |
78 | | |
|
79 | | 1 | void done(_CancellationTokenReference tokenRef) { |
80 | | 1 | if (tokenRef.hasRef) { |
81 | | 2 | tokenRef.refCount--; |
82 | | 3 | if (tokenRef.refCount == 0 && !tokenRef.cancelled) { |
83 | | | // track only cancelled tokens |
84 | | 3 | _cancelTokens.remove(tokenRef.id); |
85 | | | } |
86 | | | } |
87 | | 2 | _executing--; |
88 | | 3 | if (_terminationRequested && _executing == 0) { |
89 | | 2 | _terminate(); |
90 | | | } |
91 | | | } |
92 | | |
|
93 | | 1 | void terminate() { |
94 | | 2 | if (_executing == 0) { |
95 | | 2 | _terminate(); |
96 | | | } else { |
97 | | 1 | _terminationRequested = true; |
98 | | | } |
99 | | | } |
100 | | | } |