Skip to content

Commit 1d1cbf2

Browse files
keertipCommit Queue
authored andcommitted
Move cancellation token creation to MessageScheduler
Change-Id: I1def99245a7f063c5613cb9b82d75b686aff9ce7 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/391203 Commit-Queue: Keerti Parthasarathy <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 4e968ab commit 1d1cbf2

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

pkg/analysis_server/lib/src/lsp/handlers/handlers.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -417,12 +417,12 @@ abstract class ServerStateMessageHandler {
417417
final AnalysisServer server;
418418
final Map<Method, MessageHandler<Object?, Object?, AnalysisServer>>
419419
messageHandlers = {};
420-
final CancelRequestHandler _cancelHandler;
420+
final CancelRequestHandler cancelHandler;
421421
final NotCancelableToken _notCancelableToken = NotCancelableToken();
422422

423423
ServerStateMessageHandler(this.server)
424-
: _cancelHandler = CancelRequestHandler(server) {
425-
registerHandler(_cancelHandler);
424+
: cancelHandler = CancelRequestHandler(server) {
425+
registerHandler(cancelHandler);
426426
}
427427

428428
/// Handle the given [message]. If the [message] is a [RequestMessage], then the
@@ -446,7 +446,7 @@ abstract class ServerStateMessageHandler {
446446
// server), create a new cancellation token that will allow us to cancel
447447
// this request if requested. This saves some processing but the handler
448448
// will need to specifically check the token after `await`s.
449-
cancellationToken ??= _cancelHandler.createToken(message);
449+
cancellationToken ??= cancelHandler.createToken(message);
450450
try {
451451
var result =
452452
await handler.handleMessage(message, messageInfo, cancellationToken);
@@ -458,7 +458,7 @@ abstract class ServerStateMessageHandler {
458458
await Future.delayed(Duration.zero);
459459
return cancellationToken.isCancellationRequested ? cancelled() : result;
460460
} finally {
461-
_cancelHandler.clearToken(message);
461+
cancelHandler.clearToken(message);
462462
}
463463
}
464464

pkg/analysis_server/lib/src/lsp/lsp_analysis_server.dart

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ class LspAnalysisServer extends AnalysisServer {
477477
}
478478

479479
/// Handle a [message] that was read from the communication channel.
480-
void handleMessage(Message message) {
480+
void handleMessage(Message message, {CancelableToken? cancellationToken}) {
481481
var startTime = DateTime.now();
482482
performance.logRequestTiming(message.clientRequestTime);
483483
runZonedGuarded(() async {
@@ -506,7 +506,8 @@ class LspAnalysisServer extends AnalysisServer {
506506
if (message is RequestMessage) {
507507
analyticsManager.startedRequestMessage(
508508
request: message, startTime: startTime);
509-
await _handleRequestMessage(message, messageInfo);
509+
await _handleRequestMessage(message, messageInfo,
510+
cancellationToken: cancellationToken);
510511
} else if (message is NotificationMessage) {
511512
await _handleNotificationMessage(message, messageInfo);
512513
analyticsManager.handledNotificationMessage(
@@ -760,7 +761,14 @@ class LspAnalysisServer extends AnalysisServer {
760761
}
761762

762763
void scheduleMessage(Message message) {
763-
messageScheduler.add(LspMessage(message: message));
764+
CancelableToken? cancellationToken;
765+
// Create a cancellation token that will allow us to cancel
766+
// this request if requested.
767+
if (message is RequestMessage) {
768+
cancellationToken = messageHandler.cancelHandler.createToken(message);
769+
}
770+
messageScheduler.add(
771+
LspMessage(message: message, cancellationToken: cancellationToken));
764772
messageScheduler.notify();
765773
}
766774

@@ -1062,10 +1070,10 @@ class LspAnalysisServer extends AnalysisServer {
10621070
}
10631071

10641072
Future<void> _handleRequestMessage(
1065-
RequestMessage message,
1066-
MessageInfo messageInfo,
1067-
) async {
1068-
var result = await messageHandler.handleMessage(message, messageInfo);
1073+
RequestMessage message, MessageInfo messageInfo,
1074+
{CancelableToken? cancellationToken}) async {
1075+
var result = await messageHandler.handleMessage(message, messageInfo,
1076+
cancellationToken: cancellationToken);
10691077
result.ifError((error) => sendErrorResponse(message, error));
10701078
result.ifResult(
10711079
(result) => sendResponse(ResponseMessage(

pkg/analysis_server/lib/src/server/message_scheduler.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:analysis_server/src/analysis_server.dart';
1111
import 'package:analysis_server/src/legacy_analysis_server.dart';
1212
import 'package:analysis_server/src/lsp/lsp_analysis_server.dart';
1313
import 'package:analyzer/src/util/performance/operation_performance.dart';
14+
import 'package:analyzer/src/utilities/cancellation.dart';
1415
import 'package:meta/meta.dart';
1516

1617
/// Represents a message from DTD (Dart Tooling Daemon).
@@ -35,8 +36,9 @@ final class LegacyMessage extends MessageObject {
3536
/// Represents a message in the LSP protocol format.
3637
final class LspMessage extends MessageObject {
3738
final lsp.Message message;
39+
CancelableToken? cancellationToken;
3840

39-
LspMessage({required this.message});
41+
LspMessage({required this.message, this.cancellationToken});
4042
}
4143

4244
/// Represents a message from a client, can be an IDE, DTD etc.
@@ -75,7 +77,8 @@ final class MessageScheduler {
7577
switch (message) {
7678
case LspMessage():
7779
var lspMessage = message.message;
78-
(server as LspAnalysisServer).handleMessage(lspMessage);
80+
(server as LspAnalysisServer).handleMessage(lspMessage,
81+
cancellationToken: message.cancellationToken);
7982
case LegacyMessage():
8083
var request = message.request;
8184
(server as LegacyAnalysisServer).handleRequest(request);

0 commit comments

Comments
 (0)