Search code examples
scalagatling

Retry failed request with pause in gatling


I'm trying to implement retry logic so that if my request fails due to some technical problem (timeout, connection reset etc.) or error code 4xx|5xx, script tries to re-submit it couple of times with some pause

My code looks like this

scenario("my_simulation")
  .repeat(2) {
    tryMax(5, "retryLoopIndex") {
      //pause(session => computePause(session("retryLoopIndex").as[Int]))
      println(LocalDateTime.now() + """Before sleep""")
      pause(5.seconds)
      println(LocalDateTime.now() + """After sleep""")

      exec(http("get_eps_internal")
        .get("/500")
        .headers(headers_0)
        .requestTimeout(GlobalRequestTimeout)
        .check(status.is(200))
      )
    }
  }

I have 2 problems here:

  1. it seems like pause "happens" only once when scenario gets initialized
  2. it pauses for couple milliseconds, not 5 seconds as I would expect

From the logs

2022-02-04T08:23:06.404 Before sleep
2022-02-04T08:23:06.408 After sleep

Ho can I add pause before each retry?


Solution

  • The way you are calling the println() is not correct. In fact, println() called once and before starting the load test. If you want to call during the test you need to do this:

    .exec { session =>
            println(LocalDateTime.now() + """Before sleep""")
            session
          }
    

    Another point is that you call actions without a "dot" and it isn't a right way

    ... {
     pause(5.seconds)
     exec(http("get_eps_internal")
    ... }
    

    You need do this:

    ... {
     pause(5.seconds)
     .exec(http("get_eps_internal")
    ... }