@@ -16,6 +16,8 @@ import (
16
16
"strings"
17
17
"sync"
18
18
19
+ "github.com/avast/retry-go"
20
+
19
21
"github.com/ihippik/lambda-go/config"
20
22
)
21
23
@@ -84,37 +86,59 @@ func (s *Service) Invoke(ctx context.Context, name string, data []byte) ([]byte,
84
86
return nil , fmt .Errorf ("start container: %w" , err )
85
87
}
86
88
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
88
105
89
106
req , err := http .NewRequestWithContext (
90
107
ctx ,
91
108
http .MethodPost ,
92
- containerMeta .address (),
109
+ meta .address (),
93
110
bytes .NewReader (data ),
94
111
)
95
112
if err != nil {
96
113
return nil , fmt .Errorf ("post request: %w" , err )
97
114
}
98
115
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
103
117
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 ()
105
125
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
+ }
110
130
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
113
136
}
114
137
115
138
return respData , nil
116
139
}
117
140
141
+ // decompress decompresses tar.gz archive.
118
142
func (s * Service ) decompress (dst string , file io.ReadCloser ) error {
119
143
uncompressedStream , err := gzip .NewReader (file )
120
144
if err != nil {
0 commit comments