Execution of failed with error: Javascript runtime exceeded limit of 200ms

We have an OPDK Apigee 18.01 installation. Couple of our policies keep experiencing an intermittent issue of

`Javascript runtime exceeded limit of 200ms`

Below is the sample of one of our proxies

<!-- Sets the 'accessToken.isJwt' variable for later use in validating the token --> 
<Javascript async="false" continueOnError="false" enabled="true" timeLimit="200" name="Javascript.IsJwt"> 
      <DisplayName>Javascript.IsJwt</DisplayName> 
      <ResourceURL>jsc://Javascript.IsJwt.js</ResourceURL> 
</Javascript>

Our `timeLimit` is set for 200ms, below is the javascript code for the policy. Are they suggestion or feedback what could be causing the errors.

var accessToken = context.getVariable('request.header.Authorization');
// Expecting a string with 3 fields separated by 2 dot characters if it is a
// JWT token.
var isJwt = accessToken !== null && characterCount(accessToken, '.') === 2;
context.setVariable("accessToken.isJwt", isJwt);
function characterCount(str, character) {
  var count = 0;
  for (var i = 0; i < str.length; i++) {
    if (str[i] === character) {
      count++;
    }
  }
  return count;
}
0 1 1,192
1 REPLY 1

This is an old question, so I'm confident you've solved or avoided this problem by now. But maybe my answer will help someone else who has a similar question or problem.

Apigee Edge includes 30+ "policies" out of the box to perform standard operations on API requests and responses, like: verify an api key or access token, insert or retrieve an item from a cache, test for threats (like SQL injection), call an external service, or transform JSON to XML or vice versa. The JavaScript policy in Apigee Edge is designed to allow people to add some custom logic into their API Proxy, something not covered by the builtin policies. Often this logic is something small but important: parsing a string, checking the validity of a value, reforming or filtering JSON payloads.

The JS policy includes a timelimit. This is the maximum time Apigee Edge will allow the JS logic to run before pre-empting it and throwing a fault. This is a safety and reliability feature in Apigee Edge. In fact it is possible to built a JS policy that loops endlessly; this would obviously be bad for the particular API request being handled, but also, it can affect other API requests, because looping logic can consume a thread on the Apigee server. Lots of those loops can lead to resource starvation. Every programmer makes mistakes; the time limit on the policy is designed to be a fail safe to prevent that kind of "train wreck" issue.

The default timelimit on the JS policy is 200ms, but you can adjust it with the timeLimit attribute on the policy root.

The logic you showed looks relatively compact and efficient. In normal cases I would expect your code to run very quickly, well under 200ms.

But, when the server on which this logic runs becomes overloaded, because many other requests are being handled at the same time, the JS may run more slowly. This is what you are experiencing, I imagine.

There is no way to avoid that except: reduce the load on the server.

Also, it's good to keep in mind: When you use an evaluation version of Apigee Edge, you are using a shared server. There may be thousands of APIs deployed to a single server, and may be hundreds of people just like you all running requests through their proxies. We try to provision the right amount of resources for evaluation versions, but sometimes those eval servers get overloaded and can be slow. This may be what you are experiencing.

There's no good solution to that except, provision a commercial instance for Apigee Edge.

I hope this is helpful. For any further questions, let me know via a comment below.