@@ -726,63 +726,56 @@ func (linst *LambdaInstance) Task() {
726
726
727
727
// below here, we're guaranteed (1) sb != nil, (2) proxy != nil, (3) sb is unpaused
728
728
729
+ client := http.Client {
730
+ Transport : proxy .Transport ,
731
+ Timeout : time .Second * time .Duration (common .Conf .Limits .Max_runtime_default ),
732
+ }
733
+
729
734
// serve until we incoming queue is empty
730
735
t = common .T0 ("LambdaInstance-ServeRequests" )
731
736
for req != nil {
732
- servehttp_complete := make ( chan bool )
733
- go func () {
734
- f . printf ( "Forwarding request to sandbox " )
737
+ f . printf ( "Forwarding request to sandbox" )
738
+
739
+ t2 := common . T0 ( "LambdaInstance-RoundTrip " )
735
740
736
- t2 := common .T0 ("LambdaInstance-RoundTrip" )
741
+ // get response from sandbox
742
+ url := "http://root" + req .r .RequestURI
743
+ httpReq , err := http .NewRequest (req .r .Method , url , req .r .Body )
744
+ if err != nil {
745
+ linst .TrySendError (req , http .StatusInternalServerError , "Could not create NewRequest: " + err .Error (), sb )
746
+ } else {
747
+ resp , err := client .Do (httpReq )
737
748
738
- // get response from sandbox
739
- url := "http://root" + req .r .RequestURI
740
- httpReq , err := http .NewRequest (req .r .Method , url , req .r .Body )
749
+ // copy response out
741
750
if err != nil {
742
- linst .TrySendError (req , http .StatusInternalServerError , "Could not create NewRequest : " + err .Error (), sb )
751
+ linst .TrySendError (req , http .StatusBadGateway , "RoundTrip failed : " + err .Error ()+ " \n " , sb )
743
752
} else {
744
- resp , err := proxy .Transport .RoundTrip (httpReq )
745
-
746
- // copy response out
747
- if err != nil {
748
- linst .TrySendError (req , http .StatusBadGateway , "RoundTrip failed: " + err .Error ()+ "\n " , sb )
749
- } else {
750
- // copy headers
751
- // (adapted from copyHeaders: https://go.dev/src/net/http/httputil/reverseproxy.go)
752
- for k , vv := range resp .Header {
753
- for _ , v := range vv {
754
- req .w .Header ().Add (k , v )
755
- }
756
- }
757
- req .w .WriteHeader (resp .StatusCode )
758
-
759
- // copy body
760
- defer resp .Body .Close ()
761
- if _ , err := io .Copy (req .w , resp .Body ); err != nil {
762
- // already used WriteHeader, so can't use that to surface on error anymore
763
- msg := "reading lambda response failed: " + err .Error ()+ "\n "
764
- f .printf ("error: " + msg )
765
- linst .TrySendError (req , 0 , msg , sb )
753
+ // copy headers
754
+ // (adapted from copyHeaders: https://go.dev/src/net/http/httputil/reverseproxy.go)
755
+ for k , vv := range resp .Header {
756
+ for _ , v := range vv {
757
+ req .w .Header ().Add (k , v )
766
758
}
767
759
}
760
+ req .w .WriteHeader (resp .StatusCode )
761
+
762
+ // copy body
763
+ defer resp .Body .Close ()
764
+ if _ , err := io .Copy (req .w , resp .Body ); err != nil {
765
+ // already used WriteHeader, so can't use that to surface on error anymore
766
+ msg := "reading lambda response failed: " + err .Error ()+ "\n "
767
+ f .printf ("error: " + msg )
768
+ linst .TrySendError (req , 0 , msg , sb )
769
+ }
768
770
}
769
-
770
- // notify instance that we're done
771
- t2 .T1 ()
772
- req .execMs = int (t2 .Milliseconds )
773
- f .doneChan <- req
774
- servehttp_complete <- true
775
- }()
776
- select {
777
- case <- servehttp_complete :
778
- case <- time .After (time .Duration (common .Conf .Limits .Max_runtime_default ) * time .Second ):
779
- // TODO: have per-lambda config
780
- // TODO: send error response (maybe 504 Gateway Timeout)
781
- f .printf ("ServeHTTP timeout, killing sandbox" )
782
- sb .Destroy ("lambda instance experienced HTTP timeout" )
783
- break
784
771
}
785
772
773
+ // notify instance that we're done
774
+ t2 .T1 ()
775
+ v := int (t2 .Milliseconds )
776
+ req .execMs = v
777
+ f .doneChan <- req
778
+
786
779
// check whether we should shutdown (non-blocking)
787
780
select {
788
781
case killed := <- linst .killChan :
0 commit comments