Skip to content

Commit 4fc8c26

Browse files
committed
simplify req timeout (it is faster too)
1 parent 3b5064a commit 4fc8c26

File tree

3 files changed

+40
-49
lines changed

3 files changed

+40
-49
lines changed

src/lambda/lambda.go

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -726,63 +726,56 @@ func (linst *LambdaInstance) Task() {
726726

727727
// below here, we're guaranteed (1) sb != nil, (2) proxy != nil, (3) sb is unpaused
728728

729+
client := http.Client{
730+
Transport: proxy.Transport,
731+
Timeout: time.Second * time.Duration(common.Conf.Limits.Max_runtime_default),
732+
}
733+
729734
// serve until we incoming queue is empty
730735
t = common.T0("LambdaInstance-ServeRequests")
731736
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")
735740

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)
737748

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
741750
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)
743752
} 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)
766758
}
767759
}
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+
}
768770
}
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
784771
}
785772

773+
// notify instance that we're done
774+
t2.T1()
775+
v := int(t2.Milliseconds)
776+
req.execMs = v
777+
f.doneChan <- req
778+
786779
// check whether we should shutdown (non-blocking)
787780
select {
788781
case killed := <-linst.killChan:

src/main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -547,9 +547,8 @@ func cleanup(ctx *cli.Context) error {
547547
fmt.Printf("could not unmount %s: %s\n", dirName, err.Error())
548548
}
549549

550-
fmt.Printf("ATTEMPT to cleanup OL dir at %s\n", olPath)
551-
if err := os.RemoveAll(olPath); err != nil {
552-
fmt.Printf("could not remove all at %s: %s\n", olPath, err.Error())
550+
if err := os.Remove(filepath.Join(olPath, "worker", "worker.pid")); err != nil {
551+
fmt.Printf("could not remove worker.pid: %s\n", err.Error())
553552
}
554553

555554
return nil

src/sandbox/sock.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ func (container *SOCKContainer) HTTPProxy() (p *httputil.ReverseProxy, err error
7373
return net.Dial("unix", sockPath)
7474
}
7575

76-
// TODO: use ResponseHeaderTimeout and/or IdleConnTimeout
7776
tr := &http.Transport{Dial: dial}
7877
u, err := url.Parse("http://sock-container")
7978
if err != nil {

0 commit comments

Comments
 (0)