Issue with making second HTTP calls

Seeking some advice on how to use network.request on the following scenario. In my app, there is a need to make a 2nd network call to another server in the event that the first server is unavailable.  As such this is what I am currently doing.

​local function networkListener2(event) if event.isError then print(event.response) end end local function networkListener(event) if event.isError then network.request(url\_alternative, "GET", networkListener2) else -- do something here end end network.request(url\_main, "GET", networkListener)

With the above codes, I often get the following error though it did manage to get through at times.

failed to connect to /www.xxx.yyy.zzz (port 80) after 30000ms: connect failed: ENETUNREACH (Network is unreachable)

Any advice would be appreciated.

Are you receiving that error after the default 30 sec timeout, or is that being thrown at random? 

If it’s the former, you could simply modify the timeout limit to be longer or shorter, depending on your use case.

FYI there is also an option in network.request() to handle URL redirects with 302 responses. Not sure if that would be of use to you or not.

https://docs.coronalabs.com/api/library/network/request.html

I suspect it is after the 30 seconds timeout.  I wasn’t able to replicate this issue on my own personal devices.  i saw the these errors because they were captured in my Google Analytics dashboard.  I also tried to extend to timeout to 45 seconds but still it does not seem to solve the issue.  i cannot use re-direct because if the server goes down, that will not help too.

Corona should handle redirects. What happens when you make the 2nd server the primary?

Hi,

One possibility is checking the first URL with something like this https://forums.coronalabs.com/topic/61908-check-out-network-status and then make decisions based on that result.

This approach would allow you to skip the nesting network calls. You’ll end up making one network request based on the first status.

Cheers.

Thanks for your suggestion. Unfortunately, part of the reason why I am attempting something like that is also due to some DNS issues.  I have captured network error logs that the client has failed to resolve the hostname to the correct IP address.  As such, I am attempting to send those clients facing such issues straight to the actual IP address. However, in doing so, I ended up with the following errors for most of the calls.

failed to connect to /www.xxx.yyy.zzz (port 80) after 30000ms: connect failed: ENETUNREACH (Network is unreachable)

Hi,

I threw together this mod last night, not sure if it will help, but it seems to work with my limited testing.

http://develephant.github.io/mod_slurp/

It may or may not be applicable in your case.

Cheers.

That looks really cool… I will give it a try.  Thanks for your help!

Hi Develephant

There is a syntax error in your mod.  Line 56.

self.network\_id = network.request( url, listener, self.options )

It should be

self.network\_id = network.request( url, "GET", listener, self.options )

Or should the method name be one of the keys in the init table.

Another thing to add.  For done function of the mod, probably need to check with the id is available before doing a cancellation.  Without that, it will cause an error when the first url is successful since breather is not required.

function SlurpQueue:\_done() if self.breather\_id then timer.cancel( self.breather\_id ) end if self.timer\_id then timer.cancel( self.timer\_id ) end self:\_debug("Done") end

Hi,

Thanks for the feedback, I pushed an update that should hopefully address the issues.

Also, to anyone, this is 100% open source so pull requests are welcome. :slight_smile:

https://github.com/develephant/mod_slurp/

Cheers.

Hi,

I rewrote the module. Should be much more stable. The first version was more of a prototype.

http://develephant.github.io/mod_slurp/

Cheers.

Great.  I will download the latest version and give it a try.  Thanks.

Just one quick feedback. It seems like for the latest version, we no longer can get the error response.  I thought that information can be quite useful.

Hi,

I can pass up that info. Previously it was passing the whole networkRequest ‘userdata’ which was causing crashes in certain cases. I can add it as an additional callback parameter.

I believe the status parameter should hold the numerical error/http status id. I’ll look again.

Thanks for the feedback.

Cheers.

Are you receiving that error after the default 30 sec timeout, or is that being thrown at random? 

If it’s the former, you could simply modify the timeout limit to be longer or shorter, depending on your use case.

FYI there is also an option in network.request() to handle URL redirects with 302 responses. Not sure if that would be of use to you or not.

https://docs.coronalabs.com/api/library/network/request.html

I suspect it is after the 30 seconds timeout.  I wasn’t able to replicate this issue on my own personal devices.  i saw the these errors because they were captured in my Google Analytics dashboard.  I also tried to extend to timeout to 45 seconds but still it does not seem to solve the issue.  i cannot use re-direct because if the server goes down, that will not help too.

Corona should handle redirects. What happens when you make the 2nd server the primary?

Hi,

One possibility is checking the first URL with something like this https://forums.coronalabs.com/topic/61908-check-out-network-status and then make decisions based on that result.

This approach would allow you to skip the nesting network calls. You’ll end up making one network request based on the first status.

Cheers.

Thanks for your suggestion. Unfortunately, part of the reason why I am attempting something like that is also due to some DNS issues.  I have captured network error logs that the client has failed to resolve the hostname to the correct IP address.  As such, I am attempting to send those clients facing such issues straight to the actual IP address. However, in doing so, I ended up with the following errors for most of the calls.

failed to connect to /www.xxx.yyy.zzz (port 80) after 30000ms: connect failed: ENETUNREACH (Network is unreachable)