Skip to content

Commit f29c6fd

Browse files
committed
Handle timeout within svc
1 parent 1c6fe28 commit f29c6fd

File tree

2 files changed

+19
-31
lines changed

2 files changed

+19
-31
lines changed

internal/server/interop.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"errors"
77
"fmt"
88
"net/http"
9-
"time"
109

1110
"github.com/aws/aws-sdk-go/aws"
1211
"github.com/localstack/lambda-runtime-init/internal/localstack"
@@ -32,35 +31,6 @@ func NewInteropServer(ls *localstack.LocalStackClient) *LocalStackInteropsServer
3231
}
3332
}
3433

35-
func (c *LocalStackInteropsServer) Init(initRequest *interop.Init, timeoutMs int64) error {
36-
// This allows us to properly timeout when an INIT request -- which is unimplemented in the upstream.
37-
38-
initStart := metering.Monotime()
39-
40-
initDone := make(chan error, 1)
41-
go func() {
42-
initDone <- c.Server.Init(initRequest, timeoutMs)
43-
}()
44-
45-
var err error
46-
select {
47-
case err = <-initDone:
48-
case <-time.After(time.Duration(timeoutMs) * time.Millisecond):
49-
if _, resetErr := c.Server.Reset("timeout", 2000); resetErr != nil {
50-
log.WithError(resetErr).Error("Failed to reset after init timeout")
51-
}
52-
err = errors.New("timeout")
53-
}
54-
55-
initDuration := float64(metering.Monotime()-initStart) / float64(time.Millisecond)
56-
57-
if err != nil {
58-
log.WithError(err).WithField("duration", initDuration).Error("Init failed")
59-
}
60-
61-
return err
62-
}
63-
6434
func (c *LocalStackInteropsServer) Execute(ctx context.Context, responseWriter http.ResponseWriter, invoke *interop.Invoke) error {
6535
ctx, cancel := context.WithTimeout(context.Background(), c.Server.GetInvokeTimeout())
6636
defer cancel()

internal/server/service.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/base64"
66
"encoding/json"
7+
"errors"
78
"fmt"
89
"math"
910
"os"
@@ -24,6 +25,8 @@ import (
2425
"go.amzn.com/lambda/rapidcore/env"
2526
)
2627

28+
var errTimeout = errors.New("timeout")
29+
2730
type LocalStackService struct {
2831
sandbox *LocalStackInteropsServer
2932
adapter *localstack.LocalStackClient
@@ -129,7 +132,22 @@ func (ls *LocalStackService) Initialize(bs interop.Bootstrap) error {
129132
}
130133

131134
initStart := metering.Monotime()
132-
err = ls.sandbox.Init(initRequest, initRequest.InitTimeoutMs)
135+
136+
initDone := make(chan error, 1)
137+
go func() {
138+
initDone <- ls.sandbox.Init(initRequest, initRequest.InvokeTimeoutMs)
139+
}()
140+
141+
select {
142+
case err = <-initDone:
143+
case <-time.After(initTimeout):
144+
_, resetFailure := ls.sandbox.Reset("timeout", 2000)
145+
if resetFailure != nil {
146+
log.WithError(resetFailure).Error("Failed to reset after init timeout")
147+
}
148+
err = errTimeout
149+
}
150+
133151
ls.initDuration = float64(metering.Monotime()-initStart) / float64(time.Millisecond)
134152

135153
if err != nil {

0 commit comments

Comments
 (0)