Skip to content

Commit b34ae06

Browse files
authored
Dispatch messages on the background (#54)
1 parent 1f39d7b commit b34ae06

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) 2025 Spotify AB.
2+
//
3+
// Licensed to the Apache Software Foundation (ASF) under one
4+
// or more contributor license agreements. See the NOTICE file
5+
// distributed with this work for additional information
6+
// regarding copyright ownership. The ASF licenses this file
7+
// to you under the Apache License, Version 2.0 (the
8+
// "License"); you may not use this file except in compliance
9+
// with the License. You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing,
14+
// software distributed under the License is distributed on an
15+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
// KIND, either express or implied. See the License for the
17+
// specific language governing permissions and limitations
18+
// under the License.
19+
20+
import BuildServerProtocol
21+
import Foundation
22+
import LanguageServerProtocol
23+
24+
/// Base object that forwards BSP messages to a separate queue.
25+
/// This exists simply to make sure we can continue to receive messages
26+
/// as we process them. Otherwise, each request would require its own queue.
27+
final class AsyncMessageHandler: MessageHandler {
28+
29+
private let queue = DispatchQueue(label: "AsyncMessageHandler", qos: .userInitiated)
30+
private let messageHandler: MessageHandler
31+
32+
init(wrapping handler: MessageHandler) {
33+
self.messageHandler = handler
34+
}
35+
36+
func handle<Notification: NotificationType>(_ notification: Notification) {
37+
queue.async { [weak self] in
38+
self?.messageHandler.handle(notification)
39+
}
40+
}
41+
42+
func handle<Request: RequestType>(
43+
_ request: Request,
44+
id: RequestID,
45+
reply: @escaping @Sendable (LSPResult<Request.Response>) -> Void
46+
) {
47+
queue.async { [weak self] in
48+
self?.messageHandler.handle(request, id: id, reply: reply)
49+
}
50+
}
51+
}

Sources/SourceKitBazelBSP/Server/SourceKitBazelBSPServer.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ package final class SourceKitBazelBSPServer {
115115
outFD: outputHandle
116116
)
117117
let handler = Self.makeBSPMessageHandler(baseConfig: baseConfig, connection: connection)
118-
self.init(connection: connection, handler: handler)
118+
self.init(
119+
connection: connection,
120+
handler: AsyncMessageHandler(wrapping: handler)
121+
)
119122
}
120123

121124
package init(connection: LSPConnection, handler: MessageHandler) {

0 commit comments

Comments
 (0)