grafana/jslib.k6.io

Add a retry helper method to k6-util

simskij opened this issue · 1 comments

https://community.k6.io/t/retry-http-requests/190 has gotten a bit of attention in the forums (230 views in roughly 8 months) which suggest it's something our users would like to have access to.

function httpGet(url, params) {
    var res; 
    for (var retries = 3; retries > 0; retries--) {
        res = http.get(url, params)
        if (res.status != 408 && res.status < 500) {
            return res;
        }
    }
    return res;
}

was posted by @mstoykov as a suggestion on how to implement it yourself as a user. i'm thinking that we could provide a generic retry function in the k6-util lib that takes an http request function as a callback and attempt count as arguments, like

retry(
  () => http.get(url, params),
  retryCount,
);

// we could allow users to override the accepted status code by supplying an optional third argument:

retry(
  () => http.get(url, params), 
  retryCount, 
  [200, 301]
);