@@ -689,6 +689,37 @@ extension LambdaChannelHandler: ChannelInboundHandler {
689689 func channelRead( context: ChannelHandlerContext , data: NIOAny ) {
690690 let response = unwrapInboundIn ( data)
691691
692+ // handle response content
693+
694+ switch self . state {
695+ case . connected( let context, . waitingForNextInvocation( let continuation) ) :
696+ do {
697+ let metadata = try InvocationMetadata ( headers: response. head. headers)
698+ self . state = . connected( context, . waitingForResponse)
699+ continuation. resume ( returning: Invocation ( metadata: metadata, event: response. body ?? ByteBuffer ( ) ) )
700+ } catch {
701+ self . state = . closing
702+
703+ self . delegate. connectionWillClose ( channel: context. channel)
704+ context. close ( promise: nil )
705+ continuation. resume (
706+ throwing: NewLambdaRuntimeError ( code: . invocationMissingMetadata, underlying: error)
707+ )
708+ }
709+
710+ case . connected( let context, . sentResponse( let continuation) ) :
711+ if response. head. status == . accepted {
712+ self . state = . connected( context, . idle)
713+ continuation. resume ( )
714+ } else {
715+ self . state = . connected( context, . idle)
716+ continuation. resume ( throwing: NewLambdaRuntimeError ( code: . unexpectedStatusCodeForRequest) )
717+ }
718+
719+ case . disconnected, . closing, . connected( _, _) :
720+ break
721+ }
722+
692723 // As defined in RFC 7230 Section 6.3:
693724 // HTTP/1.1 defaults to the use of "persistent connections", allowing
694725 // multiple requests and responses to be carried over a single
@@ -719,39 +750,6 @@ extension LambdaChannelHandler: ChannelInboundHandler {
719750 self . state = . closing
720751 self . delegate. connectionWillClose ( channel: context. channel)
721752 context. close ( promise: nil )
722- } else {
723- self . state = . connected( context, . idle)
724- }
725-
726- // handle response content
727-
728- switch self . state {
729- case . connected( let context, . waitingForNextInvocation( let continuation) ) :
730- do {
731- let metadata = try InvocationMetadata ( headers: response. head. headers)
732- self . state = . connected( context, . waitingForResponse)
733- continuation. resume ( returning: Invocation ( metadata: metadata, event: response. body ?? ByteBuffer ( ) ) )
734- } catch {
735- self . state = . closing
736-
737- self . delegate. connectionWillClose ( channel: context. channel)
738- context. close ( promise: nil )
739- continuation. resume (
740- throwing: NewLambdaRuntimeError ( code: . invocationMissingMetadata, underlying: error)
741- )
742- }
743-
744- case . connected( let context, . sentResponse( let continuation) ) :
745- if response. head. status == . accepted {
746- self . state = . connected( context, . idle)
747- continuation. resume ( )
748- } else {
749- self . state = . connected( context, . idle)
750- continuation. resume ( throwing: NewLambdaRuntimeError ( code: . unexpectedStatusCodeForRequest) )
751- }
752-
753- case . disconnected, . closing, . connected( _, _) :
754- break
755753 }
756754 }
757755
0 commit comments