What gonna do when CURL CONNECTION_TIMEOUT is the error?

This is a post to remember developers to look other points in the system until committing workarounds to situations other tools are written to handle with.

Recently a CONNECTION_TIMEOUT error happens in a curl execution in a script I have and I realize it never happen with me before. Not like this.

What is a Connection_Timeout?  Its a error about open a connection with a socket resource. It happen in a curl executions and in a development environment using vagrant.

The main problem is it does not happen in production mode and in my “localhost” env cannot happen, right?

Well, dear Watson. it look  an enviroment problem, I think.

There is two possible solutions. The good one and the another 😉

The first try is increase the timout value. It is currently set to 1 sec (and hope god help me never reach this time in production. 1sec is a lot of time waiting for a conection with such critical service).

but, this is the easy one:

$easyOne = curl_init();
curl_setopt($easyOne, CURLOPT_TIMEOUT, 10);

Yeahhhh, there is no problem anymore. But I imagine the phone ringing in my desk: Ivo, the product is down? What did you do man? Its so slow… some times it take more then 10 secs to run).

10 Seconds hummm. Talk me more about this number…. 😉

The second thing I realize is everything before the conection… the resource creation, memory limit , name resolution and bla bla bla.

so, let me see the /etc/resolv.conf

# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND — YOUR CHANGES WILL BE OVERWRITTEN
nameserver 10.0.2.3

Hey, what this 10.0.2.3, think is to replace my dear google nameserver? So, I add 8.8.8.8 as the first one option.

# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND — YOUR CHANGES WILL BE OVERWRITTEN
nameserver 8.8.8.8
nameserver 10.0.2.3

and voilá. Fast socket connection in development mode again. Uffa!

and cheers.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s