LCOV - code coverage report

Current view
top level - /src - _worker_monitor.dart
Test
lcov.info
Date
2022-04-02
Legend
Lines
hit
not hit
Branches
taken
not taken
# not executed
HitTotalCoverage
Lines3434100.0%
Functions00-
Branches00-
Each row represents a line of source code
LineBranchHitsSource code
1import 'cancellation_token.dart';
2import 'worker_exception.dart';
3import 'worker_pool.dart';
4import 'worker_request.dart';
5import 'worker_service.dart';
6import '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].
22class _CancellationTokenReference extends CancellationToken {
232 _CancellationTokenReference._noToken()
24 : hasRef = false,
251 super(0);
26
271 _CancellationTokenReference(CancellationToken token)
28 : hasRef = true,
293 super(token.id, token.message);
30
31 final bool hasRef;
32
33 int refCount = 0;
34
351 @override
362 CancelledException? get exception => _exception;
37 CancelledException? _exception;
38
391 void _cancel() {
401 if (hasRef) {
413 _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].
50class WorkerMonitor {
511 WorkerMonitor(this._terminate);
52
534 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
611 _CancellationTokenReference _getTokenRef(CancellationToken? token) =>
62 (token == null)
631 ? noTokenRef
642 : _cancelTokens.putIfAbsent(
653 token.id, () => _CancellationTokenReference(token));
66
671 _CancellationTokenReference begin(WorkerRequest request) {
682 _executing++;
692 final tokenRef = _getTokenRef(request.cancelToken);
701 if (tokenRef.hasRef) {
712 tokenRef.refCount++;
721 request.overrideCancelToken(tokenRef);
73 }
74 return tokenRef;
75 }
76
773 void cancel(CancellationToken token) => _getTokenRef(token)._cancel();
78
791 void done(_CancellationTokenReference tokenRef) {
801 if (tokenRef.hasRef) {
812 tokenRef.refCount--;
823 if (tokenRef.refCount == 0 && !tokenRef.cancelled) {
83 // track only cancelled tokens
843 _cancelTokens.remove(tokenRef.id);
85 }
86 }
872 _executing--;
883 if (_terminationRequested && _executing == 0) {
892 _terminate();
90 }
91 }
92
931 void terminate() {
942 if (_executing == 0) {
952 _terminate();
96 } else {
971 _terminationRequested = true;
98 }
99 }
100}
Choose Features