Skip to content

Conversation

@fabianfett
Copy link
Contributor

Add ControlPlaneResponseDecoder to decode incoming HTTP responses.

Motivation:

We want to save allocations wherever possible. A big driver of allocations is the NIOHTTP1 HTTPResponseDecoder. The reason for this is: It can not be optimized for the lambda use case and allocates header string values that are not needed.

Modifications:

  • Add ControlPlaneResponseDecoder to decode only what we need from an HTTP response
  • Add LambdaRuntimeError which shall be the single error type in the future

Result:

Faster control plane response decoding.

@fabianfett fabianfett requested a review from tomerd January 3, 2022 09:05
return .decoded(head)

case .contentLength(let length):
head.contentLength = length // TODO: This can crash
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

case 202:
return .accepted
case 400 ..< 600:
preconditionFailure("TODO: implement")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

switch head.statusCode {
case 200:
guard let body = body else {
preconditionFailure("TODO: implement")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

return status
}

private mutating func decodeCRLFTerminatedLine(from buffer: inout ByteBuffer) throws -> DecodeResult<ByteBuffer> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does nio offer any utils to parse line by line, I thought there was some Decoder for that?

}

private mutating func decodeHeaderLine(from buffer: inout ByteBuffer) throws -> HeaderLineContent {
guard let colonIndex = buffer.readableBytesView.firstIndex(of: UInt8(ascii: ":")) else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool trick

@discardableResult
mutating func decodeOptionalWhiteSpaceBeforeFieldValue(from buffer: inout ByteBuffer) throws -> Int {
let startIndex = buffer.readerIndex
guard let index = buffer.readableBytesView.firstIndex(where: { $0 != UInt8(ascii: " ") && $0 != UInt8(ascii: "\t") }) else {
Copy link
Contributor

@tomerd tomerd Jan 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe worth defining consts for the UInt8 here and in other spots around the code

Copy link
Contributor

@tomerd tomerd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome. looks like some leftover TODOs?

case .contentLength(let length):
head.contentLength = length // TODO: This can crash

case .contentType:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we dont need to record that?

@tomerd
Copy link
Contributor

tomerd commented Jan 22, 2024

closing in inactive PRs, feel free to re-open if still relevant

@tomerd tomerd closed this Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants