Skip to content

Commit 1c6fe28

Browse files
committed
feat(init): Handle expiring init
1 parent d0017a7 commit 1c6fe28

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

internal/server/interop.go

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

1011
"github.com/aws/aws-sdk-go/aws"
1112
"github.com/localstack/lambda-runtime-init/internal/localstack"
@@ -31,6 +32,35 @@ func NewInteropServer(ls *localstack.LocalStackClient) *LocalStackInteropsServer
3132
}
3233
}
3334

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+
3464
func (c *LocalStackInteropsServer) Execute(ctx context.Context, responseWriter http.ResponseWriter, invoke *interop.Invoke) error {
3565
ctx, cancel := context.WithTimeout(context.Background(), c.Server.GetInvokeTimeout())
3666
defer cancel()

0 commit comments

Comments
 (0)