xref: /curl/docs/libcurl/curl_easy_pause.md (revision 5a488251)
1---
2c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
3SPDX-License-Identifier: curl
4Title: curl_easy_pause
5Section: 3
6Source: libcurl
7See-also:
8  - curl_easy_cleanup (3)
9  - curl_easy_reset (3)
10Protocol:
11  - All
12Added-in: 7.18.0
13---
14
15# NAME
16
17curl_easy_pause - pause and unpause a connection
18
19# SYNOPSIS
20
21~~~c
22#include <curl/curl.h>
23
24CURLcode curl_easy_pause(CURL *handle, int bitmask );
25~~~
26
27# DESCRIPTION
28
29Using this function, you can explicitly mark a running connection to get
30paused, and you can unpause a connection that was previously paused. Unlike
31most other libcurl functions, curl_easy_pause(3) can be used from within
32callbacks.
33
34A connection can be paused by using this function or by letting the read or
35the write callbacks return the proper magic return code
36(*CURL_READFUNC_PAUSE* and *CURL_WRITEFUNC_PAUSE*). A write callback
37that returns pause signals to the library that it could not take care of any
38data at all, and that data is then delivered again to the callback when the
39transfer is unpaused.
40
41While it may feel tempting, take care and notice that you cannot call this
42function from another thread. To unpause, you may for example call it from the
43progress callback (CURLOPT_PROGRESSFUNCTION(3)).
44
45When this function is called to unpause receiving, the write callback might
46get called before this function returns to deliver cached content. When
47libcurl delivers such cached data to the write callback, it is delivered as
48fast as possible, which may overstep the boundary set in
49CURLOPT_MAX_RECV_SPEED_LARGE(3) etc.
50
51The **handle** argument identifies the transfer you want to pause or
52unpause.
53
54A paused transfer is excluded from low speed cancels via the
55CURLOPT_LOW_SPEED_LIMIT(3) option and unpausing a transfer resets the
56time period required for the low speed limit to be met.
57
58The **bitmask** argument is a set of bits that sets the new state of the
59connection. The following bits can be used:
60
61## CURLPAUSE_RECV
62
63Pause receiving data. There is no data received on this connection until this
64function is called again without this bit set. Thus, the write callback
65(CURLOPT_WRITEFUNCTION(3)) is not called.
66
67## CURLPAUSE_SEND
68
69Pause sending data. There is no data sent on this connection until this
70function is called again without this bit set. Thus, the read callback
71(CURLOPT_READFUNCTION(3)) is not called.
72
73## CURLPAUSE_ALL
74
75Convenience define that pauses both directions.
76
77## CURLPAUSE_CONT
78
79Convenience define that unpauses both directions.
80
81# LIMITATIONS
82
83The pausing of transfers does not work with protocols that work without
84network connectivity, like FILE://. Trying to pause such a transfer, in any
85direction, might cause problems or error.
86
87# MULTIPLEXED
88
89When a connection is used multiplexed, like for HTTP/2, and one of the
90transfers over the connection is paused and the others continue flowing,
91libcurl might end up buffering contents for the paused transfer. It has to do
92this because it needs to drain the socket for the other transfers and the
93already announced window size for the paused transfer allows the server to
94continue sending data up to that window size amount. By default, libcurl
95announces a 32 megabyte window size, which thus can make libcurl end up
96buffering 32 megabyte of data for a paused stream.
97
98When such a paused stream is unpaused again, any buffered data is delivered
99first.
100
101# %PROTOCOLS%
102
103# EXAMPLE
104
105~~~c
106int main(void)
107{
108  CURL *curl = curl_easy_init();
109  if(curl) {
110    /* pause a transfer in both directions */
111    curl_easy_pause(curl, CURLPAUSE_RECV | CURLPAUSE_SEND);
112
113  }
114}
115~~~
116
117# MEMORY USE
118
119When pausing a download transfer by returning the magic return code from a
120write callback, the read data is already in libcurl's internal buffers so it
121has to keep it in an allocated buffer until the receiving is again unpaused
122using this function.
123
124If the downloaded data is compressed and is asked to get uncompressed
125automatically on download, libcurl continues to uncompress the entire
126downloaded chunk and it caches the data uncompressed. This has the side-
127effect that if you download something that is compressed a lot, it can result
128in a large data amount needing to be allocated to save the data during the
129pause. Consider not using paused receiving if you allow libcurl to uncompress
130data automatically.
131
132If the download is done with HTTP/2 or HTTP/3, there is up to a stream window
133size worth of data that curl cannot stop but instead needs to cache while the
134transfer is paused. This means that if a window size of 64 MB is used, libcurl
135might end up having to cache 64 MB of data.
136
137# %AVAILABILITY%
138
139# RETURN VALUE
140
141CURLE_OK (zero) means that the option was set properly, and a non-zero return
142code means something wrong occurred after the new state was set. See the
143libcurl-errors(3) man page for the full list with descriptions.
144