File: QueueBasedMessageHandler.swift

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (248 lines) | stat: -rw-r--r-- 10,545 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import Foundation
import LanguageServerProtocolJSONRPC
import SKLogging

#if compiler(>=6)
package import LanguageServerProtocol
package import SwiftExtensions
#else
import LanguageServerProtocol
import SwiftExtensions
#endif

/// Side structure in which `QueueBasedMessageHandler` can keep track of active requests etc.
///
/// All of these could be requirements on `QueueBasedMessageHandler` but having them in a separate type means that
/// types conforming to `QueueBasedMessageHandler` only have to have a single member and it also ensures that these
/// fields are not accessible outside of the implementation of `QueueBasedMessageHandler`.
package actor QueueBasedMessageHandlerHelper {
  /// The category in which signposts for message handling should be logged.
  fileprivate let signpostLoggingCategory: String

  /// Whether a new logging scope should be created when handling a notification / request.
  private let createLoggingScope: Bool

  /// The queue on which we start and stop keeping track of cancellation.
  ///
  /// Having a queue for this ensures that we started keeping track of a
  /// request's task before handling any cancellation request for it.
  private let cancellationMessageHandlingQueue = AsyncQueue<Serial>()

  /// Notifications don't have an ID. This represents the next ID we can use to identify a notification.
  private let notificationIDForLogging = AtomicUInt32(initialValue: 1)

  /// The requests that we are currently handling.
  ///
  /// Used to cancel the tasks if the client requests cancellation.
  private var inProgressRequestsByID: [RequestID: Task<(), Never>] = [:]

  /// Up to 10 request IDs that have recently finished.
  ///
  /// This is only used so we don't log an error when receiving a `CancelRequestNotification` for a request that has
  /// just returned a response.
  private var recentlyFinishedRequests: [RequestID] = []

  package init(signpostLoggingCategory: String, createLoggingScope: Bool) {
    self.signpostLoggingCategory = signpostLoggingCategory
    self.createLoggingScope = createLoggingScope
  }

  /// Cancel the request with the given ID.
  ///
  /// Cancellation is performed automatically when a `$/cancelRequest` notification is received. This can be called to
  /// implicitly cancel requests based on some criteria.
  package nonisolated func cancelRequest(id: RequestID) {
    // Since the request is very cheap to execute and stops other requests
    // from performing more work, we execute it with a high priority.
    cancellationMessageHandlingQueue.async(priority: .high) {
      if let task = await self.inProgressRequestsByID[id] {
        task.cancel()
        return
      }
      if await !self.recentlyFinishedRequests.contains(id) {
        logger.error(
          "Cannot cancel request \(id, privacy: .public) because it hasn't been scheduled for execution yet"
        )
      }
    }
  }

  fileprivate nonisolated func setInProgressRequest(id: RequestID, request: some RequestType, task: Task<(), Never>?) {
    self.cancellationMessageHandlingQueue.async(priority: .background) {
      await self.setInProgressRequestImpl(id: id, request: request, task: task)
    }
  }

  private func setInProgressRequestImpl(id: RequestID, request: some RequestType, task: Task<(), Never>?) {
    self.inProgressRequestsByID[id] = task
    if task == nil {
      self.recentlyFinishedRequests.append(id)
      while self.recentlyFinishedRequests.count > 10 {
        self.recentlyFinishedRequests.removeFirst()
      }
    }
  }

  fileprivate nonisolated func withNotificationLoggingScopeIfNecessary(_ body: () -> Void) {
    guard createLoggingScope else {
      body()
      return
    }
    // Only use the last two digits of the notification ID for the logging scope to avoid creating too many scopes.
    // See comment in `withLoggingScope`.
    // The last 2 digits should be sufficient to differentiate between multiple concurrently running notifications.
    let notificationID = notificationIDForLogging.fetchAndIncrement()
    withLoggingScope("notification-\(notificationID % 100)") {
      body()
    }
  }

  fileprivate nonisolated func withRequestLoggingScopeIfNecessary(
    id: RequestID,
    _ body: @Sendable () async -> Void
  ) async {
    guard createLoggingScope else {
      await body()
      return
    }
    // Only use the last two digits of the request ID for the logging scope to avoid creating too many scopes.
    // See comment in `withLoggingScope`.
    // The last 2 digits should be sufficient to differentiate between multiple concurrently running requests.
    await withLoggingScope("request-\(id.numericValue % 100)") {
      await body()
    }
  }
}

package protocol QueueBasedMessageHandlerDependencyTracker: DependencyTracker {
  init(_ notification: some NotificationType)
  init(_ request: some RequestType)
}

/// A `MessageHandler` that handles all messages on an `AsyncQueue` and tracks dependencies between requests using
/// `DependencyTracker`, ensuring that requests which depend on each other are not executed out-of-order.
package protocol QueueBasedMessageHandler: MessageHandler {
  associatedtype DependencyTracker: QueueBasedMessageHandlerDependencyTracker

  /// The queue on which all messages (notifications, requests, responses) are
  /// handled.
  ///
  /// The queue is blocked until the message has been sufficiently handled to
  /// avoid out-of-order handling of messages. For sourcekitd, this means that
  /// a request has been sent to sourcekitd and for clangd, this means that we
  /// have forwarded the request to clangd.
  ///
  /// The actual semantic handling of the message happens off this queue.
  var messageHandlingQueue: AsyncQueue<DependencyTracker> { get }

  var messageHandlingHelper: QueueBasedMessageHandlerHelper { get }

  /// Called when a notification has been received but before it is being handled in `messageHandlingQueue`.
  ///
  /// Adopters can use this to implicitly cancel requests when a notification is received.
  func didReceive(notification: some NotificationType)

  /// Called when a request has been received but before it is being handled in `messageHandlingQueue`.
  ///
  /// Adopters can use this to implicitly cancel requests when a notification is received.
  func didReceive(request: some RequestType, id: RequestID)

  /// Perform the actual handling of `notification`.
  func handle(notification: some NotificationType) async

  /// Perform the actual handling of `request`.
  func handle<Request: RequestType>(
    request: Request,
    id: RequestID,
    reply: @Sendable @escaping (LSPResult<Request.Response>) -> Void
  ) async
}

extension QueueBasedMessageHandler {
  package func didReceive(notification: some NotificationType) {}
  package func didReceive(request: some RequestType, id: RequestID) {}

  package func handle(_ notification: some NotificationType) {
    messageHandlingHelper.withNotificationLoggingScopeIfNecessary {
      // Request cancellation needs to be able to overtake any other message we
      // are currently handling. Ordering is not important here. We thus don't
      // need to execute it on `messageHandlingQueue`.
      if let notification = notification as? CancelRequestNotification {
        logger.log("Received cancel request notification: \(notification.forLogging)")
        self.messageHandlingHelper.cancelRequest(id: notification.id)
        return
      }
      self.didReceive(notification: notification)

      let signposter = Logger(
        subsystem: LoggingScope.subsystem,
        category: messageHandlingHelper.signpostLoggingCategory
      )
      .makeSignposter()
      let signpostID = signposter.makeSignpostID()
      let state = signposter.beginInterval("Notification", id: signpostID, "\(type(of: notification))")
      messageHandlingQueue.async(metadata: DependencyTracker(notification)) {
        signposter.emitEvent("Start handling", id: signpostID)
        await self.handle(notification: notification)
        signposter.endInterval("Notification", state, "Done")
      }
    }
  }

  package func handle<Request: RequestType>(
    _ request: Request,
    id: RequestID,
    reply: @Sendable @escaping (LSPResult<Request.Response>) -> Void
  ) {
    let signposter = Logger(subsystem: LoggingScope.subsystem, category: messageHandlingHelper.signpostLoggingCategory)
      .makeSignposter()
    let signpostID = signposter.makeSignpostID()
    let state = signposter.beginInterval("Request", id: signpostID, "\(Request.self)")

    self.didReceive(request: request, id: id)

    let task = messageHandlingQueue.async(metadata: DependencyTracker(request)) {
      signposter.emitEvent("Start handling", id: signpostID)
      await self.messageHandlingHelper.withRequestLoggingScopeIfNecessary(id: id) {
        await withTaskCancellationHandler {
          await self.handle(request: request, id: id, reply: reply)
          signposter.endInterval("Request", state, "Done")
        } onCancel: {
          signposter.emitEvent("Cancelled", id: signpostID)
        }
      }
      // We have handled the request and can't cancel it anymore.
      // Stop keeping track of it to free the memory.
      self.messageHandlingHelper.setInProgressRequest(id: id, request: request, task: nil)
    }
    // Keep track of the ID -> Task management with low priority. Once we cancel
    // a request, the cancellation task runs with a high priority and depends on
    // this task, which will elevate this task's priority.
    self.messageHandlingHelper.setInProgressRequest(id: id, request: request, task: task)
  }
}

fileprivate extension RequestID {
  /// Returns a numeric value for this request ID.
  ///
  /// For request IDs that are numbers, this is straightforward. For string-based request IDs, this uses a hash to
  /// convert the string into a number.
  var numericValue: Int {
    switch self {
    case .number(let number): return number
    case .string(let string): return Int(string) ?? abs(string.hashValue)
    }
  }
}