# Send a request asynchronously per unit of time

**URL:** <https://community.gatling.io/t/send-a-request-asynchronously-per-unit-of-time/507>\
**Category:** Gatling (Open-Source)\
**Created:** [July 26, 2013, 12:26pm UTC](https://community.gatling.io/t/send-a-request-asynchronously-per-unit-of-time/507 "2013-07-26T12:26:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roy\_van\_der\_Valk](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@Roy\_van\_der\_Valk](https://community.gatling.io/u/Roy_van_der_Valk)\
**Post date:** [July 26, 2013, 12:26pm UTC](https://community.gatling.io/t/send-a-request-asynchronously-per-unit-of-time/507/1 "2013-07-26T12:26:28Z")

</div>

Dear all,

I have been using Gatling the last months and becoming more and more enthusiastic about it!

There’s one problem that is buggering me today. The application I’m testing requires to send a keep-alive request every 10 minutes for the session to keep alive. These keep-alive requests are now send at the moment that the recorder recorded them. However the actions in between can take longer when you generate a large load on the application. The results in the session being timed out at a certain moment.

I want to solve this neatly by making Gatling send this type of request every 10 minutes in a session irrespective of what happens in the scenario.

Any ideas?

Cheers,  
Roy

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [July 26, 2013, 12:44pm UTC](https://community.gatling.io/t/send-a-request-asynchronously-per-unit-of-time/507/2 "2013-07-26T12:44:35Z")

</div>

What’s you Scala level? Which version of Gatling do you use?

---

<div class="post-metadata">

**Author:** ![Roy\_van\_der\_Valk](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@Roy\_van\_der\_Valk](https://community.gatling.io/u/Roy_van_der_Valk)\
**Post date:** [July 26, 2013, 12:55pm UTC](https://community.gatling.io/t/send-a-request-asynchronously-per-unit-of-time/507/3 "2013-07-26T12:55:32Z")

</div>

I’m not afraid of some advanced Scala. I’m not the greatest expert, but I got a lot of geniuses with several years of Scala experience around me who can help me on that part.

I’m using Gatling v1.5.1.

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [July 26, 2013, 1:05pm UTC](https://community.gatling.io/t/send-a-request-asynchronously-per-unit-of-time/507/4 "2013-07-26T13:05:07Z")

</div>

Scala 2.9, so no implicit value classes then, damn...

Here's what you can (no very simple, sorry):

&nbsp;&nbsp;&nbsp;- store into the session the last ping time  
&nbsp;&nbsp;&nbsp;- define a method that would take a request and chain it with a doIf  
&nbsp;&nbsp;&nbsp;block that would test the ping time and if it's more that 10 minutes,  
&nbsp;&nbsp;&nbsp;update it and send the ping request  
&nbsp;&nbsp;&nbsp;- you can then either patch the API to add a new execAndPing method to  
&nbsp;&nbsp;&nbsp;existing DSL elements, or try to override exec method

Clear?

---

<div class="post-metadata">

**Author:** ![Roy\_van\_der\_Valk](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@Roy\_van\_der\_Valk](https://community.gatling.io/u/Roy_van_der_Valk)\
**Post date:** [July 26, 2013, 1:15pm UTC](https://community.gatling.io/t/send-a-request-asynchronously-per-unit-of-time/507/5 "2013-07-26T13:15:20Z")

</div>

Sounds neat and do-able, thank you! I got another question along the same lines, will make another post.

---

<div class="post-metadata">

**Author:** ![Roy\_van\_der\_Valk](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@Roy\_van\_der\_Valk](https://community.gatling.io/u/Roy_van_der_Valk)\
**Post date:** [July 26, 2013, 1:34pm UTC](https://community.gatling.io/t/send-a-request-asynchronously-per-unit-of-time/507/6 "2013-07-26T13:34:13Z")

</div>

I gave it another thought, but now I have to use some kind of threshold (say 1 minute before the session elapses, so at 9 minutes) when I will send a keep-alive. But what if there is an action that takes longer than the threshold?

Hypothetically this could go wrong? Do you think it is doable to have two Akka actors per session? One executing the normal script and the other responsible for the keep-alive?

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [July 26, 2013, 1:42pm UTC](https://community.gatling.io/t/send-a-request-asynchronously-per-unit-of-time/507/7 "2013-07-26T13:42:37Z")

</div>

> I gave it another thought, but now I have to use some kind of threshold  
> (say 1 minute before the session elapses, so at 9 minutes) when I will send  
> a keep-alive. But what if there is an action that takes longer than the  
> threshold?

Cross your fingers it doesn't happen.

> Hypothetically this could go wrong? Do you think it is doable to have two  
> Akka actors per session? One executing the normal script and the other  
> responsible for the keep-alive?
> 
> A user is not an Actor, it's a message.

Ideally, what you'd need is this:

> <https://github.com/gatling/gatling/issues/431>
>
> This could be used for page resources and ajax calls.
> Http-Only cookies wouldn't… be sent.
> This kind of request would be grouped in a scatter-gather manner.

---

<div class="post-metadata">

**Author:** ![Roy\_van\_der\_Valk](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@Roy\_van\_der\_Valk](https://community.gatling.io/u/Roy_van_der_Valk)\
**Post date:** [July 26, 2013, 1:53pm UTC](https://community.gatling.io/t/send-a-request-asynchronously-per-unit-of-time/507/8 "2013-07-26T13:53:32Z")

</div>

Haha yeah maybe crossing my fingers would be the easiest way out! 😉 Indeed that’s precisely what I need. I’m looking forward to the official 2.0 release!

Have a nice weekend twice!
