forked from aws/aws-lambda-runtime-interface-emulator
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathruntime.go
More file actions
48 lines (41 loc) · 852 Bytes
/
runtime.go
File metadata and controls
48 lines (41 loc) · 852 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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:
}
}
}