Java script policy: how to sleep in a proxy

karliso
Participant II

I am looking for the best way to sleep in a java script policy. We want to inject random delays in specific use cases to confuse hackers. Is this possible or will it cause issues? I could loop for the delay period but I have concerns about burning CPU cycles and creating performance issue.

0 7 1,139
7 REPLIES 7

You can’t do it in JS. There’s a Java policy that sleeps (https://github.com/DinoChiesa/ApigeeEdge-Java-Delay) and also you can use a python callout to do it.

I’m interested to hear more about your use case. Can you share?

I probably shouldn't share as this is a public forum. I will take a look at your repo.Thanks for posting.

ok, I dropped the URL in the answer above. The code already has a "random" feature - but you may need or want to modify the parameterization so it fits your needs better.

(I'll accept pull requests if you want to share back!)

cool, I will check it out.

I just updated that callout to allow you to specify a min and max delay. Re-pull if you have cloned the repo. Check the readme for the example.

We can do this in JS . Please try below code in Java Script

function delay(time) {
var currentTime = new Date();
var futureTime = new Date();
while(futureTime.valueOf() < currentTime.valueOf() + time) {
futureTime = new Date();
}
}

delay(2000);

yes, you can do that, but it's not going to scale very well. That code performs what is known as a "busy wait", which is an anti-pattern for most programming environments, including the JavaScript callout within Apigee. If you have multiple API requests exercising that code, you will overwhelm the server resources.  Better to do it with the Java callout I provided earlier.