Skip to content

Commit e410413

Browse files
committed
simpler/faster mock
motivation: faster mock server for perf testing changes: no logger, always keep-alive
1 parent 7d1b309 commit e410413

File tree

1 file changed

+7
-31
lines changed

1 file changed

+7
-31
lines changed

Sources/MockServer/main.swift

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,44 +13,35 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
import Foundation
16-
import Logging
1716
import NIO
1817
import NIOHTTP1
1918

2019
internal struct MockServer {
21-
private let logger: Logger
2220
private let group: EventLoopGroup
2321
private let host: String
2422
private let port: Int
2523
private let mode: Mode
26-
private let keepAlive: Bool
2724

2825
public init() {
29-
var logger = Logger(label: "MockServer")
30-
logger.logLevel = env("LOG_LEVEL").flatMap(Logger.Level.init) ?? .info
31-
self.logger = logger
3226
self.group = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
3327
self.host = env("HOST") ?? "127.0.0.1"
3428
self.port = env("PORT").flatMap(Int.init) ?? 7000
3529
self.mode = env("MODE").flatMap(Mode.init) ?? .string
36-
self.keepAlive = env("KEEP_ALIVE").flatMap(Bool.init) ?? true
3730
}
3831

3932
func start() throws {
4033
let bootstrap = ServerBootstrap(group: group)
4134
.serverChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
4235
.childChannelInitializer { channel in
4336
channel.pipeline.configureHTTPServerPipeline(withErrorHandling: true).flatMap { _ in
44-
channel.pipeline.addHandler(HTTPHandler(logger: self.logger,
45-
keepAlive: self.keepAlive,
46-
mode: self.mode))
37+
channel.pipeline.addHandler(HTTPHandler(mode: self.mode))
4738
}
4839
}
4940
try bootstrap.bind(host: self.host, port: self.port).flatMap { channel -> EventLoopFuture<Void> in
5041
guard let localAddress = channel.localAddress else {
5142
return channel.eventLoop.makeFailedFuture(ServerError.cantBind)
5243
}
53-
self.logger.info("\(self) started and listening on \(localAddress)")
44+
print("\(self) started and listening on \(localAddress)")
5445
return channel.eventLoop.makeSucceededFuture(())
5546
}.wait()
5647
}
@@ -60,15 +51,11 @@ internal final class HTTPHandler: ChannelInboundHandler {
6051
public typealias InboundIn = HTTPServerRequestPart
6152
public typealias OutboundOut = HTTPServerResponsePart
6253

63-
private let logger: Logger
6454
private let mode: Mode
65-
private let keepAlive: Bool
6655

6756
private var pending = CircularBuffer<(head: HTTPRequestHead, body: ByteBuffer?)>()
6857

69-
public init(logger: Logger, keepAlive: Bool, mode: Mode) {
70-
self.logger = logger
71-
self.keepAlive = keepAlive
58+
public init(mode: Mode) {
7259
self.mode = mode
7360
}
7461

@@ -93,8 +80,6 @@ internal final class HTTPHandler: ChannelInboundHandler {
9380
}
9481

9582
func processRequest(context: ChannelHandlerContext, request: (head: HTTPRequestHead, body: ByteBuffer?)) {
96-
self.logger.debug("\(self) processing \(request.head.uri)")
97-
9883
var responseStatus: HTTPResponseStatus
9984
var responseBody: String?
10085
var responseHeaders: [(String, String)]?
@@ -125,33 +110,24 @@ internal final class HTTPHandler: ChannelInboundHandler {
125110

126111
func writeResponse(context: ChannelHandlerContext, status: HTTPResponseStatus, headers: [(String, String)]? = nil, body: String? = nil) {
127112
var headers = HTTPHeaders(headers ?? [])
128-
headers.add(name: "Content-Length", value: "\(body?.utf8.count ?? 0)")
129-
if !self.keepAlive {
130-
// We only need to add a "Connection" header if we really want to close the connection
131-
headers.add(name: "Connection", value: "close")
132-
}
113+
headers.add(name: "content-length", value: "\(body?.utf8.count ?? 0)")
133114
let head = HTTPResponseHead(version: HTTPVersion(major: 1, minor: 1), status: status, headers: headers)
134115

135116
context.write(wrapOutboundOut(.head(head))).whenFailure { error in
136-
self.logger.error("\(self) write error \(error)")
117+
print("\(self) write error \(error)")
137118
}
138119

139120
if let b = body {
140121
var buffer = context.channel.allocator.buffer(capacity: b.utf8.count)
141122
buffer.writeString(b)
142123
context.write(wrapOutboundOut(.body(.byteBuffer(buffer)))).whenFailure { error in
143-
self.logger.error("\(self) write error \(error)")
124+
print("\(self) write error \(error)")
144125
}
145126
}
146127

147128
context.writeAndFlush(wrapOutboundOut(.end(nil))).whenComplete { result in
148129
if case .failure(let error) = result {
149-
self.logger.error("\(self) write error \(error)")
150-
}
151-
if !self.keepAlive {
152-
context.close().whenFailure { error in
153-
self.logger.error("\(self) close error \(error)")
154-
}
130+
print("\(self) write error \(error)")
155131
}
156132
}
157133
}

0 commit comments

Comments
 (0)