@@ -24,7 +24,7 @@ var MAX_RETRIES = 5 // int
2424var RETRY_WAIT_MIN = 100 // ms
2525var RETRY_WAIT_MAX = 300 // ms
2626
27- // http timeout for Upload/Download/Remove operations
27+ // default http timeout for Upload/Download/Remove operations
2828var UPLOAD_HTTP_TIMEOUT = 60 // seconds
2929var DOWNLOAD_HTTP_TIMEOUT = 300 // seconds
3030var REMOVE_HTTP_TIMEOUT = 300 // seconds
@@ -122,7 +122,7 @@ func makeURL(storeType, scope, key string) (*url.URL, error) {
122122 return url .Parse (fullpath )
123123}
124124
125- func get (storeType , scope , key string ) error {
125+ func get (storeType , scope , key string , timeout int ) error {
126126 if skipCache (storeType , scope , "get" ) {
127127 return nil
128128 }
@@ -136,7 +136,7 @@ func get(storeType, scope, key string) error {
136136 if err != nil {
137137 return err
138138 }
139- store := sdstore .NewStore (sdToken , MAX_RETRIES , DOWNLOAD_HTTP_TIMEOUT , RETRY_WAIT_MIN , RETRY_WAIT_MAX )
139+ store := sdstore .NewStore (sdToken , MAX_RETRIES , timeout , RETRY_WAIT_MIN , RETRY_WAIT_MAX )
140140
141141 var toExtract bool
142142
@@ -152,7 +152,7 @@ func get(storeType, scope, key string) error {
152152 }
153153}
154154
155- func set (storeType , scope , filePath string ) error {
155+ func set (storeType , scope , filePath string , timeout int ) error {
156156 if skipCache (storeType , scope , "set" ) {
157157 return nil
158158 }
@@ -166,7 +166,7 @@ func set(storeType, scope, filePath string) error {
166166 if err != nil {
167167 return err
168168 }
169- store := sdstore .NewStore (sdToken , MAX_RETRIES , UPLOAD_HTTP_TIMEOUT , RETRY_WAIT_MIN , RETRY_WAIT_MAX )
169+ store := sdstore .NewStore (sdToken , MAX_RETRIES , timeout , RETRY_WAIT_MIN , RETRY_WAIT_MAX )
170170
171171 var toCompress bool
172172
@@ -181,7 +181,7 @@ func set(storeType, scope, filePath string) error {
181181
182182}
183183
184- func remove (storeType , scope , key string ) error {
184+ func remove (storeType , scope , key string , timeout int ) error {
185185 if skipCache (storeType , scope , "remove" ) {
186186 return nil
187187 }
@@ -190,7 +190,7 @@ func remove(storeType, scope, key string) error {
190190 return sdstore .Cache2Disk ("remove" , scope , key , CacheMaxSizeInMB )
191191 } else {
192192 sdToken := os .Getenv ("SD_TOKEN" )
193- store := sdstore .NewStore (sdToken , MAX_RETRIES , REMOVE_HTTP_TIMEOUT , RETRY_WAIT_MIN , RETRY_WAIT_MAX )
193+ store := sdstore .NewStore (sdToken , MAX_RETRIES , timeout , RETRY_WAIT_MIN , RETRY_WAIT_MAX )
194194
195195 if storeType == "cache" {
196196 md5URL , err := makeURL (storeType , scope , fmt .Sprintf ("%s%s" , filepath .Clean (key ), "_md5.json" ))
@@ -225,6 +225,22 @@ func remove(storeType, scope, key string) error {
225225 }
226226}
227227
228+ func getTimeout (flagTimeout string , envValue string , defaultTimeout int ) (int , error ) {
229+
230+ if flagTimeout != "" {
231+ flagTimeoutInt , err := strconv .Atoi (flagTimeout )
232+ return flagTimeoutInt , err
233+ }
234+
235+ envTimeout := os .Getenv (envValue )
236+ if envTimeout != "" {
237+ envTimeoutInt , err := strconv .Atoi (envTimeout )
238+ return envTimeoutInt , err
239+ }
240+
241+ return defaultTimeout , nil
242+ }
243+
228244func main () {
229245 defer finalRecover ()
230246
@@ -247,6 +263,11 @@ func main() {
247263 Usage : "Type of the command. For example: cache, artifacts, steps" ,
248264 Value : "stable" ,
249265 },
266+ cli.StringFlag {
267+ Name : "timeout" ,
268+ Usage : "Specifies the timeout in seconds." ,
269+ Value : "" ,
270+ },
250271 }
251272
252273 app .Commands = []cli.Command {
@@ -259,8 +280,12 @@ func main() {
259280 }
260281 scope := strings .ToLower (c .String ("scope" ))
261282 storeType := strings .ToLower (c .String ("type" ))
283+ timeout , err := getTimeout (c .String ("timeout" ), "SD_STORE_CLI_DOWNLOAD_HTTP_TIMEOUT" , DOWNLOAD_HTTP_TIMEOUT )
284+ if err != nil {
285+ failureExit (err )
286+ }
262287 key := c .Args ().Get (0 )
263- err : = get (storeType , scope , key )
288+ err = get (storeType , scope , key , timeout )
264289 if err != nil {
265290 failureExit (err )
266291 }
@@ -278,8 +303,12 @@ func main() {
278303 }
279304 scope := strings .ToLower (c .String ("scope" ))
280305 storeType := strings .ToLower (c .String ("type" ))
306+ timeout , err := getTimeout (c .String ("timeout" ), "SD_STORE_CLI_UPLOAD_HTTP_TIMEOUT" , UPLOAD_HTTP_TIMEOUT )
307+ if err != nil {
308+ failureExit (err )
309+ }
281310 key := c .Args ().Get (0 )
282- err : = set (storeType , scope , key )
311+ err = set (storeType , scope , key , timeout )
283312 if err != nil {
284313 failureExit (err )
285314 }
@@ -297,8 +326,12 @@ func main() {
297326 }
298327 scope := strings .ToLower (c .String ("scope" ))
299328 storeType := strings .ToLower (c .String ("type" ))
329+ timeout , err := getTimeout (c .String ("timeout" ), "SD_STORE_CLI_REMOVE_HTTP_TIMEOUT" , REMOVE_HTTP_TIMEOUT )
330+ if err != nil {
331+ failureExit (err )
332+ }
300333 key := c .Args ().Get (0 )
301- err : = remove (storeType , scope , key )
334+ err = remove (storeType , scope , key , timeout )
302335 if err != nil {
303336 failureExit (err )
304337 }
0 commit comments