@@ -25,12 +25,36 @@ const (
2525
2626// doRequestWithInfiniteRetries does a request with infinite retries until the context is done.
2727// The parent context should have a deadline or a timeout.
28- func doRequestWithInfiniteRetries (ctx context.Context , method , address string , requestBody []byte , accessToken * string , envdInitRequestTimeout time.Duration , sandboxID , envdVersion string ) (* http.Response , int64 , error ) {
28+ func doRequestWithInfiniteRetries (
29+ ctx context.Context ,
30+ method ,
31+ address string ,
32+ accessToken * string ,
33+ envdInitRequestTimeout time.Duration ,
34+ envVars map [string ]string ,
35+ sandboxID ,
36+ envdVersion ,
37+ hyperloopIP string ,
38+ ) (* http.Response , int64 , error ) {
2939 requestCount := int64 (0 )
3040 for {
41+ now := time .Now ()
42+
43+ jsonBody := & PostInitJSONBody {
44+ EnvVars : & envVars ,
45+ HyperloopIP : & hyperloopIP ,
46+ AccessToken : accessToken ,
47+ Timestamp : & now ,
48+ }
49+
50+ body , err := json .Marshal (jsonBody )
51+ if err != nil {
52+ return nil , requestCount , err
53+ }
54+
3155 requestCount ++
3256 reqCtx , cancel := context .WithTimeout (ctx , envdInitRequestTimeout )
33- request , err := http .NewRequestWithContext (reqCtx , method , address , bytes .NewReader (requestBody ))
57+ request , err := http .NewRequestWithContext (reqCtx , method , address , bytes .NewReader (body ))
3458 if err != nil {
3559 cancel ()
3660 return nil , requestCount , err
@@ -76,20 +100,18 @@ func (s *Sandbox) initEnvd(ctx context.Context) error {
76100
77101 hyperloopIP := s .Slot .HyperloopIPString ()
78102 address := fmt .Sprintf ("http://%s:%d/init" , s .Slot .HostIPString (), consts .DefaultEnvdServerPort )
79- now := time .Now ()
80- jsonBody := & PostInitJSONBody {
81- EnvVars : & s .Config .Envd .Vars ,
82- HyperloopIP : & hyperloopIP ,
83- AccessToken : s .Config .Envd .AccessToken ,
84- Timestamp : & now ,
85- }
86-
87- body , err := json .Marshal (jsonBody )
88- if err != nil {
89- return err
90- }
91103
92- response , count , err := doRequestWithInfiniteRetries (ctx , "POST" , address , body , s .Config .Envd .AccessToken , s .internalConfig .EnvdInitRequestTimeout , s .Runtime .SandboxID , s .Config .Envd .Version )
104+ response , count , err := doRequestWithInfiniteRetries (
105+ ctx ,
106+ http .MethodPost ,
107+ address ,
108+ s .Config .Envd .AccessToken ,
109+ s .internalConfig .EnvdInitRequestTimeout ,
110+ s .Config .Envd .Vars ,
111+ s .Runtime .SandboxID ,
112+ s .Config .Envd .Version ,
113+ hyperloopIP ,
114+ )
93115 if err != nil {
94116 envdInitCalls .Add (ctx , count , metric .WithAttributes (attributesFail ... ))
95117 return fmt .Errorf ("failed to init envd: %w" , err )
0 commit comments