Skip to content

Commit ff669f6

Browse files
committed
Retry request logic
1 parent 134d29b commit ff669f6

File tree

4 files changed

+45
-14
lines changed

4 files changed

+45
-14
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ module github.com/ihippik/lambda-go
33
go 1.21.1
44

55
require (
6+
github.com/avast/retry-go v3.0.0+incompatible
67
github.com/docker/docker v23.0.3+incompatible
78
github.com/docker/go-connections v0.4.0
9+
github.com/gorilla/mux v1.8.0
810
github.com/ihippik/config v0.1.1
911
github.com/sethvargo/go-envconfig v0.9.0
1012
)
@@ -20,7 +22,6 @@ require (
2022
github.com/getsentry/sentry-go v0.23.0 // indirect
2123
github.com/gogo/protobuf v1.3.2 // indirect
2224
github.com/golang/protobuf v1.5.3 // indirect
23-
github.com/gorilla/mux v1.8.0 // indirect
2425
github.com/ihippik/slog-sentry v0.1.0 // indirect
2526
github.com/klauspost/compress v1.16.7 // indirect
2627
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migc
44
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
55
github.com/Microsoft/hcsshim v0.11.0 h1:7EFNIY4igHEXUdj1zXgAyU3fLc7QfOKHbkldRVTBdiM=
66
github.com/Microsoft/hcsshim v0.11.0/go.mod h1:OEthFdQv/AD2RAdzR6Mm1N1KPCztGKDurW1Z8b8VGMM=
7+
github.com/avast/retry-go v3.0.0+incompatible h1:4SOWQ7Qs+oroOTQOYnAHqelpCO0biHSxpiH9JdtuBj0=
8+
github.com/avast/retry-go v3.0.0+incompatible/go.mod h1:XtSnn+n/sHqQIpZ10K1qAevBhOOCWBLXXy3hyiqqBrY=
79
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
810
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
911
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=

lambda/meta.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ func newMetaData(containerID string, port int) *metaData {
1414
func (m metaData) address() string {
1515
return "http://localhost:" + strconv.Itoa(m.port)
1616
}
17+
18+
func (m metaData) short() string {
19+
return m.containerID[:5]
20+
}

lambda/service.go

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"strings"
1717
"sync"
1818

19+
"github.com/avast/retry-go"
20+
1921
"github.com/ihippik/lambda-go/config"
2022
)
2123

@@ -84,37 +86,59 @@ func (s *Service) Invoke(ctx context.Context, name string, data []byte) ([]byte,
8486
return nil, fmt.Errorf("start container: %w", err)
8587
}
8688

87-
// FIXME: need to wait for container start
89+
respData, err := s.makeRequest(ctx, data, containerMeta)
90+
if err != nil {
91+
return nil, fmt.Errorf("make request: %w", err)
92+
}
93+
94+
if err := s.builder.ContainerStop(ctx, containerMeta.containerID); err != nil {
95+
return nil, fmt.Errorf("stop container: %w", err)
96+
}
97+
98+
return respData, nil
99+
}
100+
101+
// makeRequest makes http request to container with Lambda.
102+
// Using retry pattern for waiting container ready for requests.
103+
func (s *Service) makeRequest(ctx context.Context, data []byte, meta *metaData) ([]byte, error) {
104+
const numAttempts = 3
88105

89106
req, err := http.NewRequestWithContext(
90107
ctx,
91108
http.MethodPost,
92-
containerMeta.address(),
109+
meta.address(),
93110
bytes.NewReader(data),
94111
)
95112
if err != nil {
96113
return nil, fmt.Errorf("post request: %w", err)
97114
}
98115

99-
resp, err := s.client.Do(req)
100-
if err != nil {
101-
return nil, fmt.Errorf("do request: %w", err)
102-
}
116+
var respData []byte
103117

104-
defer resp.Body.Close()
118+
if err = retry.Do(
119+
func() error {
120+
resp, err := s.client.Do(req)
121+
if err != nil {
122+
return fmt.Errorf("do request: %w", err)
123+
}
124+
defer resp.Body.Close()
105125

106-
respData, err := io.ReadAll(resp.Body)
107-
if err != nil {
108-
return nil, fmt.Errorf("read response body: %w", err)
109-
}
126+
respData, err = io.ReadAll(resp.Body)
127+
if err != nil {
128+
return fmt.Errorf("read response body: %w", err)
129+
}
110130

111-
if err := s.builder.ContainerStop(ctx, containerMeta.containerID); err != nil {
112-
return nil, fmt.Errorf("stop container: %w", err)
131+
return nil
132+
},
133+
retry.Attempts(numAttempts),
134+
); err != nil {
135+
return nil, err
113136
}
114137

115138
return respData, nil
116139
}
117140

141+
// decompress decompresses tar.gz archive.
118142
func (s *Service) decompress(dst string, file io.ReadCloser) error {
119143
uncompressedStream, err := gzip.NewReader(file)
120144
if err != nil {

0 commit comments

Comments
 (0)