Linux curl keep alive

How do I make cURL use keepalive from the command line?

Use the same » curl -v http://my.server/url1 http://my.server/url2 » invocation against your server and check that you see the same message.

Consider using tcpdump instead of netstat to see how the packets are handled. netstat will only give you a momentary glimpse of what’s happening, whereas with tcpdump you’ll see every single packet involved. Another option is Wireshark.

Solution 2:

If your server allows ‘KeepAlive On’, you can use telnet to keep a persistent connection going like so:

$ while :;do echo -e "GET / HTTP/1.1\nhost: $YOUR_VIRTUAL_HOSTNAME\n\n";sleep 1;done|telnet $YOUR_SERVERS_IP 80 

Solution 3:

One way to test HTTP persistent connection/Keep-Alive is to see if the TCP connection is reused for subsequent connections.

For example. I have a file containing link of http://google.com repeated multiple times.

Running below command will open http://google.com multiple times with the same TCP connection.

And during this time if you netstat you can find that the TCP connection has not changes and the older one is resued (The socket remains the same).

$ sudo netstat -pnt|grep curl tcp 0 0 106.51.85.118:48682 74.125.236.69:80 ESTABLISHED 9732/curl $ sudo netstat -pnt|grep curl tcp 0 0 106.51.85.118:48682 74.125.236.69:80 ESTABLISHED 9732/curl $ sudo netstat -pnt|grep curl tcp 0 0 106.51.85.118:48682 74.125.236.69:80 ESTABLISHED 9732/curl 

But when we ask client to use HTTP 1.0 which dose not support persistent HTTP connection the socket address changes

$ curl -0 -K /tmp/file $ sudo netstat -pnt|grep curl tcp 0 0 106.51.85.118:48817 74.125.236.69:80 ESTABLISHED 9765/curl $ sudo netstat -pnt|grep curl tcp 0 0 106.51.85.118:48827 74.125.236.69:80 ESTABLISHED 9765/curl $ sudo netstat -pnt|grep curl tcp 0 74 106.51.85.118:48838 74.125.236.69:80 ESTABLISHED 9765/curl 

from this we can be sure that the TCP connection is reused.

Читайте также:  Mount shared folder linux guest

Источник

libcurl: how does connection Keep-Alive work?

How do I keep connection alive with libcurl? The usage that I’d like is the following. I want to connect to a server that supports Keep-Alive but which closes the connection after 90 seconds of inactivity. I want to do POST once in a while when some events (outside of this connection) occur, and I want to keep the connection alive in order to reduce latency. It might happen that no event occurs for more than 90 seconds, so I want a way to tell the server I’m not idle. The examples page is excellent, but I don’t see one involving Keep-Alive : https://curl.haxx.se/libcurl/c/example.html I did find CURLOPT_TCP_KEEPINTVL : https://curl.haxx.se/libcurl/c/CURLOPT_TCP_KEEPINTVL.html, but it’s not clear to me how it’s supposed to work. In that example, we see the code:

CURL *curl = curl_easy_init(); if(curl) < curl_easy_setopt(curl, CURLOPT_URL, "http://example.com"); /* enable TCP keep-alive for this transfer */ curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L); /* set keep-alive idle time to 120 seconds */ curl_easy_setopt(curl, CURLOPT_TCP_KEEPIDLE, 120L); /* interval time between keep-alive probes: 60 seconds */ curl_easy_setopt(curl, CURLOPT_TCP_KEEPINTVL, 60L); curl_easy_perform(curl); >

Pass a long. Sets the interval, in seconds, that the operating system will wait between sending keepalive probes. Not all operating systems support this option. (Added in 7.25.0)

Two questions: A) Operationally, how do I use this? From the description, it looks like as long as I keep curl in scope, the connection will remain open. Is this correct? And then should I just keep doing

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postthis); res = curl_easy_perform(curl); 

Источник

How to disable keepalive in curl

I’m trying to figure out how to consistently turn off keepalive across various client machines which are issuing HTTP requests via curl . This is my target server:

- Ubuntu 18.04.2 LTS - 4.15.0-47-generic - HA-Proxy version 1.8.19-1ppa1~bionic 2019/02/12 
- Ubuntu 16.04.3 LTS - 4.4.0-62-generic - curl 7.47.0 (x86_64-pc-linux-gnu) libcurl/7.47.0 GnuTLS/3.4.10 zlib/1.2.8 libidn/1.32 librtmp/2.3 
- Ubuntu 18.04 LTS - 4.15.0-20-generic - curl 7.58.0 (x86_64-pc-linux-gnu) libcurl/7.58.0 OpenSSL/1.1.0g zlib/1.2.11 libidn2/2.0.4 libpsl/0.19.1 (+libidn2/2.0.4) nghttp2/1.30.0 librtmp/2.3 

To turn off keepalive I’ve tried using -H «Connection: close» , —no-keepalive and —keepalive-time 1 and only the first option seems to work but only from client 1. On client 1 (Ubuntu 16) the connection is not left open but from client 2 (Ubuntu 18) the connection is left open until it times out. I confirm that either by looking at the target server’s watch -n 0.1 «netstat -na | fgrep CLIENT_IP_ADDRESS» or by using -vvv on both clients, which on client 1 is always * Closing connection 0 and on client 2 is always * Connection #0 to host www.example.com left intact . The difference between client 1 and 2 are obviously the Ubuntu version and curl version. What is it that causes the the connection to be closed in client 1 but not in client 2? I’ve also found out that if I change my target server to another machine running an old distro with an old apache httpd, whether I send out Connection: close or not doesn’t make any difference and keepalive is always used from either of my 2 clients. So I suppose that the target server configuration also plays some role in that. Edit: To complicate things even further, if from client 1 and 2 I issue requests to the target server via ab then the connection is killed instantly. That is expected because ab uses HTTP/1.0 and not HTTP/1.1 . But then, if I target the old distro with apache then in both cases the connection remains open. Which means that, indeed, the receiving end plays a role as well. Edit 2: I grabbed all /proc/sys/net/ipv4/ settings of both clients via:

for file in /proc/sys/net/ipv4/* do echo "$file $(cat $file)" done 

Источник

Оцените статью
Adblock
detector