curl_easy_upkeep.md (2368B)
1 --- 2 c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 3 SPDX-License-Identifier: curl 4 Title: curl_easy_upkeep 5 Section: 3 6 Source: libcurl 7 See-also: 8 - CURLOPT_TCP_KEEPALIVE (3) 9 - CURLOPT_TCP_KEEPIDLE (3) 10 Protocol: 11 - All 12 Added-in: 7.62.0 13 --- 14 15 # NAME 16 17 curl_easy_upkeep - keep existing connections alive 18 19 # SYNOPSIS 20 21 ~~~c 22 #include <curl/curl.h> 23 24 CURLcode curl_easy_upkeep(CURL *handle); 25 ~~~ 26 27 # DESCRIPTION 28 29 Some protocols have "connection upkeep" mechanisms. These mechanisms usually 30 send some traffic on existing connections in order to keep them alive; this 31 can prevent connections from being closed due to overzealous firewalls, for 32 example. 33 34 For HTTP/2 we have an upkeep mechanism: when 35 the connection upkeep interval is exceeded and curl_easy_upkeep(3) 36 is called, an HTTP/2 PING frame is sent on the connection. 37 38 For MQTT the upkeep interval defines when to send ping requests to prevent the 39 server from disconnecting. 40 41 This function must be explicitly called in order to perform the upkeep work. 42 The connection upkeep interval is set with 43 CURLOPT_UPKEEP_INTERVAL_MS(3). 44 45 If you call this function on an easy handle that uses a shared connection cache 46 then upkeep is performed on the connections in that cache, even if those 47 connections were never used by the easy handle. (Added in 8.10.0) 48 49 # %PROTOCOLS% 50 51 # EXAMPLE 52 53 ~~~c 54 int main(void) 55 { 56 CURL *curl = curl_easy_init(); 57 if(curl) { 58 /* Make a connection to an HTTP/2 server. */ 59 curl_easy_setopt(curl, CURLOPT_URL, "https://example.com"); 60 61 /* Set the interval to 30000ms / 30s */ 62 curl_easy_setopt(curl, CURLOPT_UPKEEP_INTERVAL_MS, 30000L); 63 64 curl_easy_perform(curl); 65 66 /* Perform more work here. */ 67 68 /* While the connection is being held open, curl_easy_upkeep() can be 69 called. If curl_easy_upkeep() is called and the time since the last 70 upkeep exceeds the interval, then an HTTP/2 PING is sent. */ 71 curl_easy_upkeep(curl); 72 73 /* Perform more work here. */ 74 75 /* always cleanup */ 76 curl_easy_cleanup(curl); 77 } 78 } 79 ~~~ 80 81 # %AVAILABILITY% 82 83 # RETURN VALUE 84 85 This function returns a CURLcode indicating success or error. 86 87 CURLE_OK (0) means everything was OK, non-zero means an error occurred, see 88 libcurl-errors(3). If CURLOPT_ERRORBUFFER(3) was set with curl_easy_setopt(3) 89 there can be an error message stored in the error buffer when non-zero is 90 returned.