Skip to content

Commit

Permalink
Don't run unnecessary consumeBody if body is empty byteBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Mar 6, 2021
1 parent 9fc3eed commit 790b763
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions Sources/Hummingbird/Router/RouterMethods.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,18 @@ extension HBRouterMethods {
switch body {
case .collate:
return HBCallbackResponder { request in
request.body.consumeBody(on: request.eventLoop).flatMapThrowing { buffer in
request.body = .byteBuffer(buffer)
return try closure(request).response(from: request).apply(patch: request.optionalResponse)
if case .byteBuffer(_) = request.body {
do {
let response = try closure(request).response(from: request).apply(patch: request.optionalResponse)
return request.success(response)
} catch {
return request.failure(error)
}
} else {
return request.body.consumeBody(on: request.eventLoop).flatMapThrowing { buffer in
request.body = .byteBuffer(buffer)
return try closure(request).response(from: request).apply(patch: request.optionalResponse)
}
}
}
case .stream:
Expand All @@ -166,11 +175,17 @@ extension HBRouterMethods {
switch body {
case .collate:
return HBCallbackResponder { request in
request.body.consumeBody(on: request.eventLoop).flatMap { buffer in
request.body = .byteBuffer(buffer)
if case .byteBuffer(_) = request.body {
return closure(request).responseFuture(from: request)
.map { $0.apply(patch: request.optionalResponse) }
.hop(to: request.eventLoop)
} else {
return request.body.consumeBody(on: request.eventLoop).flatMap { buffer in
request.body = .byteBuffer(buffer)
return closure(request).responseFuture(from: request)
.map { $0.apply(patch: request.optionalResponse) }
.hop(to: request.eventLoop)
}
}
}
case .stream:
Expand Down

0 comments on commit 790b763

Please sign in to comment.