Skip to content

Commit

Permalink
Adopt NIO singletons
Browse files Browse the repository at this point in the history
  • Loading branch information
FranzBusch committed Jul 27, 2023
1 parent 7328908 commit a734501
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 36 deletions.
32 changes: 14 additions & 18 deletions Sources/NIOTCPEchoClient/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,13 @@ struct Client {
/// The port to connect to.
private let port: Int
/// The client's event loop group.
private let eventLoopGroup: any EventLoopGroup
private let eventLoopGroup: MultiThreadedEventLoopGroup

static func main() async throws {
// We are creating this event loop group at the top level and it will live until
// the process exits. This means we also don't have to shut it down.
//
// Note that we start this group with 1 thread. In general, most NIO programs
// should use 1 thread as a default unless they're planning to be a network
// proxy or something that is expecting to be dominated by packet parsing. Most
// servers aren't, and NIO is very fast, so 1 NIO thread is quite capable of
// saturating the average small to medium sized machine.
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)

let client = Client(
host: "localhost",
port: 8765,
eventLoopGroup: eventLoopGroup
eventLoopGroup: .singleton
)
try await client.run()

Expand Down Expand Up @@ -73,12 +63,18 @@ struct Client {
}
.connect(
host: self.host,
port: self.port,
channelConfiguration: .init(
inboundType: String.self,
outboundType: String.self
)
)
port: self.port
) { channel in
channel.eventLoop.makeCompletedFuture {
try NIOAsyncChannel(
synchronouslyWrapping: channel,
configuration: .init(
inboundType: String.self,
outboundType: String.self
)
)
}
}

print("Connection(\(number)): Writing request")
try await channel.outboundWriter.write("Hello on connection \(number)")
Expand Down
32 changes: 14 additions & 18 deletions Sources/NIOTCPEchoServer/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,13 @@ struct Server {
/// The server's port.
private let port: Int
/// The server's event loop group.
private let eventLoopGroup: any EventLoopGroup
private let eventLoopGroup: MultiThreadedEventLoopGroup

static func main() async throws {
// We are creating this event loop group at the top level and it will live until
// the process exits. This means we also don't have to shut it down.
//
// Note that we start this group with 1 thread. In general, most NIO programs
// should use 1 thread as a default unless they're planning to be a network
// proxy or something that is expecting to be dominated by packet parsing. Most
// servers aren't, and NIO is very fast, so 1 NIO thread is quite capable of
// saturating the average small to medium sized machine.
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)

let server = Server(
host: "localhost",
port: 8765,
eventLoopGroup: eventLoopGroup
eventLoopGroup: .singleton
)
try await server.run()
}
Expand All @@ -57,12 +47,18 @@ struct Server {
}
.bind(
host: self.host,
port: self.port,
childChannelConfiguration: .init(
inboundType: String.self,
outboundType: String.self
)
)
port: self.port
) { channel in
channel.eventLoop.makeCompletedFuture {
try NIOAsyncChannel(
synchronouslyWrapping: channel,
configuration: .init(
inboundType: String.self,
outboundType: String.self
)
)
}
}

// We are handling each incoming connection in a separate child task. It is important
// to use a discarding task group here which automatically discards finished child tasks.
Expand Down

0 comments on commit a734501

Please sign in to comment.