FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: Ensure the LambdaRuntimeAPI server is up before launching INIT phase by gregfurman · Pull Request #42 · localstack/lambda-runtime-init · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .go  (3) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
11 changes: 6 additions & 5 deletions cmd/localstack/custom_interop.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"time"

"github.com/go-chi/chi"
log "github.com/sirupsen/logrus"
"go.amzn.com/lambda/core/statejson"
"go.amzn.com/lambda/interop"
"go.amzn.com/lambda/rapidcore"
"go.amzn.com/lambda/rapidcore/standalone"
"io"
"net/http"
"strconv"
"strings"
"time"
)

type CustomInteropServer struct {
Expand Down
22 changes: 19 additions & 3 deletions cmd/localstack/main.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ package main

import (
"context"
log "github.com/sirupsen/logrus"
"go.amzn.com/lambda/interop"
"go.amzn.com/lambda/rapidcore"
"os"
"runtime/debug"
"strconv"
"strings"
"time"

log "github.com/sirupsen/logrus"
"go.amzn.com/lambda/interop"
"go.amzn.com/lambda/rapidcore"
)

type LsOpts struct {
Expand Down Expand Up @@ -188,6 +190,12 @@ func main() {
SetLogsEgressAPI(localStackLogsEgressApi).
SetTracer(tracer)

// Corresponds to the 'AWS_LAMBDA_RUNTIME_API' environment variable.
// We need to ensure the runtime server is up before the INIT phase,
// but this envar is only set after the InitHandler is called.
runtimeAPIAddress := "127.0.0.1:9001"
sandbox.SetRuntimeAPIAddress(runtimeAPIAddress)
Comment thread
gregfurman marked this conversation as resolved.

// xray daemon
endpoint := "http://" + lsOpts.LocalstackIP + ":" + lsOpts.EdgePort
xrayConfig := initConfig(endpoint, xRayLogLevel)
Expand Down Expand Up @@ -225,6 +233,14 @@ func main() {
}
go RunHotReloadingListener(interopServer, lsOpts.HotReloadingPaths, fileWatcherContext, lsOpts.FileWatcherStrategy)

log.Debugf("Awaiting initialization of runtime api at %s.", runtimeAPIAddress)
// Fixes https://github.com/localstack/localstack/issues/12680
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
if err := waitForRuntimeAPI(ctx, runtimeAPIAddress); err != nil {
log.Fatalf("Lambda Runtime API server at %s did not come up in 30s, with error %s", runtimeAPIAddress, err.Error())
}
cancel()

// start runtime init. It is important to start `InitHandler` synchronously because we need to ensure the
// notification channels and status fields are properly initialized before `AwaitInitialized`
log.Debugln("Starting runtime init.")
Expand Down
48 changes: 48 additions & 0 deletions cmd/localstack/runtime.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"context"
"fmt"
"net/http"
"net/url"
"strings"
"time"
)

func waitForRuntimeAPI(ctx context.Context, targetAddress string) error {
if !strings.HasPrefix(targetAddress, "http://") {
targetAddress = fmt.Sprintf("http://%s", targetAddress)
}

healthEndpoint, err := url.JoinPath(targetAddress, "2018-06-01", "ping")
if err != nil {
return err
}

req, err := http.NewRequestWithContext(ctx, http.MethodGet, healthEndpoint, nil)
if err != nil {
return err
}
client := &http.Client{
Timeout: 5 * time.Second,
}

ticker := time.NewTicker(50 * time.Millisecond)
defer ticker.Stop()

for {
resp, err := client.Do(req)
if err == nil {
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
return nil
}
}

select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
}
}
}

Back | FazBrowse Home | New Git URL