wso2 / product-microgateway

Choreo Connect is a cloud-native, open-source, and developer-centric API gateway.
https://wso2.com/choreo/choreo-connect/
Apache License 2.0
294 stars 252 forks source link

Error when executing subsequent requests on a given API #987

Closed chanakaudaya closed 4 years ago

chanakaudaya commented 4 years ago

Description:

I'm getting an error when sending back to back requests to the microgateway. The first request works fine. I get the below error when I send the subsequent request.

error: Text '-1' could not be parsed at index 2 {}
    at ballerina/time:parse(<native>)
       ballerina/http:getFreshnessLifetime(caching/http_caching_client.bal:561)
       ballerina/http:isFreshResponse(caching/http_caching_client.bal:576)
       ballerina/http:getCachedResponse(caching/http_caching_client.bal:375)
       ballerina/http:forward(caching/http_caching_client.bal:325)
       ballerina/http:forward(client_endpoint.bal:146)
       src:0.0.0:get_0a7778a1_db73_433e_a031_88f40787f429(MGWapi__V1.bal:123)

Steps to reproduce:

I'm using the APIM 3.0 to develop the API and then import that to MGW 3.0.2 through the CLI. I have generated a token from the devportal and use that for invocation.

Everything goes well with the first request. The second request fails. If I try after a minute, it works again for the first req and failed the next one. Looks like a caching issue I tried enabling response caching on the API through the publisher, still, the issue persists

Affected Product Version:

APIM 3.0 MGW 3.0.2

Environment details (with versions):

praminda commented 4 years ago

Ballerina http client by default enables response cache. Also currently released version of MGW doesn't support changing this behavior by configurations. As a workaround below is something you can try. Add cache: { enabled: false } manually to below line https://github.com/wso2/product-microgateway/blob/030fa555c968e392203c0e1fea98d2dbbbbeab4f/components/micro-gateway-cli/src/main/resources/templates/httpEndpoint.mustache#L3 Resulting file will look like,

http:Client {{qualifiedServiceName}}_{{endpointUrlType}} = new (
{{#if endpoints.0.isEtcdEnabled}}gateway:etcdSetup("{{name}}_{{endpointUrlType}}_endpoint_0","{{name}}_{{endpointUrlType}}_0_etcdKey","{{endpoints.0.endpointUrl}}","{{endpoints.0.etcdKey}}"){{else}}gateway:retrieveConfig("{{name}}_{{endpointUrlType}}_endpoint_0","{{endpoints.0.endpointUrl}}"){{/if}},
config = { cache: {enabled: false}, {{>http2}}{{>basicAuth}}{{>secureSocket}}
});
praminda commented 4 years ago

Upcoming version of MGW will support changing response caching behavior by extension. You can track progress with #929

chanakaudaya commented 4 years ago

I'm not sure whether you're answering the actual problem here. The problem I faced was a fundamental usability issue of the MGW.

This behavior stops me from using MGW. A few things, 1) I'm running MGW with the default config (by default, caching is enabled in MGW) 2) I haven't enabled response caching when creating the API

What is your suggestion for this scenario? Note: I have tried with enabling response caching on the API through the publisher and recreated the MGW, still the same error.

praminda commented 4 years ago

Let me explain my answer, By looking at the log you've attached it looks like this issues comes due to some issue in ballerina http client when response caching is enabled. When calling the backend endpoint of MGW API, we are using a ballerina http client which by default enables response caching. However for now you can't enabled or disable this behavior by extension or by changing config in WSO2 API Publisher. Which means API Publisher configuration on the response caching doesn't take effect in MGW.

So with the assumption this is caused by this default response cache behavior of ballerina http client, I suggested above workaround for you to disable the response cache and check the behavior. If that works for you we can try to confirm if it is already fixed in the latest master after jballerina upgrade.

chanakaudaya commented 4 years ago

I'm sorry, I didn't understand your workaround properly. Is this a configuration change in the product or changing the source code of the product and recompile?

praminda commented 4 years ago

Oops sorry my bad. Forgot to mention the file to update. You can find above file in <TOOLKIT_HOME>/resources/templates/httpEndpoint.mustache.

praminda commented 4 years ago

To make this change available for all endpoint types you can change <TOOLKIT_HOME>/resources/templates/caching.mustache and replace file content with below.

, cache: { enabled: false }
chanakaudaya commented 4 years ago

This suggestion worked. I'm able to get the correct response to subsequent requests.

Resolution

praminda commented 4 years ago

Closing since issue is tracked via #929