Skip to content

Commit 014c90d

Browse files
committed
use proprietary DispatchQueues
1 parent 4d32a8c commit 014c90d

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

Sources/Prorsum/Net/TCPServer.swift

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
import Foundation
1010
import Dispatch
1111

12+
private let sourceReadQ = DispatchQueue(label: "prorsum.net.source-read.queue")
13+
14+
private let tcpIncommingRequestHandleQ = DispatchQueue(label: "prorsum.tcp.concurrent-queue", attributes: .concurrent)
15+
1216
public final class TCPServer {
1317
let stream: TCPStream
1418

@@ -45,19 +49,21 @@ public final class TCPServer {
4549
try stream.socket.bind(host: host, port: port)
4650
}
4751

52+
/**
53+
* Kitura HTTPServerのように、acceptはループで行い、read/writeを非同期で
54+
* DispatchWorkItemを用いている模様
55+
*/
4856
public func listen(backlog: Int = 1024) throws {
4957
try stream.socket.listen(backlog: backlog)
5058

51-
watcher = DispatchSource.makeReadSource(fileDescriptor: stream.socket.fd, queue: .main)
59+
watcher = DispatchSource.makeReadSource(fileDescriptor: stream.socket.fd, queue: sourceReadQ)
5260

5361
watcher?.setEventHandler { [unowned self] in
54-
do {
55-
let client = try TCPStream(socket: self.stream.socket.accept())
56-
go {
62+
tcpIncommingRequestHandleQ.async {
63+
do {
64+
let client = try TCPStream(socket: self.stream.socket.accept())
5765
self.handler(client)
58-
}
59-
} catch {
60-
go {
66+
} catch {
6167
self.onError?(error)
6268
}
6369
}

0 commit comments

Comments
 (0)