Artifactspublic-v0

curl

master
ply4 git calls191 ms290 KiB2 cachedprotocol v2
184 lines8.3 KiBRaw

NAME

libcurl-multi - how to use the multi interface

DESCRIPTION

This is an overview on how to use the libcurl multi interface in your C programs. There are specific man pages for each function mentioned in here. There is also the libcurl-tutorial(3) man page for a complete tutorial to programming with libcurl and the libcurl-easy(3) man page for an overview of the libcurl easy interface.

All functions in the multi interface are prefixed with curl_multi.

OBJECTIVES

The multi interface offers several abilities that the easy interface does not. They are mainly:

  1. Enable a "pull" interface. The application that uses libcurl decides where and when to ask libcurl to get/send data.

  2. Enable multiple simultaneous transfers in the same thread without making it complicated for the application.

  3. Enable the application to wait for action on its own file descriptors and curl's file descriptors simultaneously.

  4. Enable event-based handling and scaling transfers up to and beyond thousands of parallel connections.

ONE MULTI HANDLE MANY EASY HANDLES

To use the multi interface, you must first create a 'multi handle' with curl_multi_init(3). This handle is then used as input to all further curl_multi_* functions.

With a multi handle and the multi interface you can do several simultaneous transfers in parallel. Each single transfer is built up around an easy handle. You create all the easy handles you need, and setup the appropriate options for each easy handle using curl_easy_setopt(3).

There are two flavors of the multi interface, the select() oriented one and the event based one we call multi_socket. You benefit from reading through the description of both versions to fully understand how they work and differentiate. We start out with the select() oriented version.

When an easy handle is setup and ready for transfer, then instead of using curl_easy_perform(3) like when using the easy interface for transfers, you should add the easy handle to the multi handle with curl_multi_add_handle(3). You can add more easy handles to a multi handle at any point, even if other transfers are already running.

Should you change your mind, the easy handle is again removed from the multi stack using curl_multi_remove_handle(3). Once removed from the multi handle, you can again use other easy interface functions like curl_easy_perform(3) on the handle or whatever you think is necessary. You can remove handles at any point during transfers.

Adding the easy handle to the multi handle does not start the transfer. Remember that one of the main ideas with this interface is to let your application drive. You drive the transfers by invoking curl_multi_perform(3). libcurl then transfers data if there is anything available to transfer. It uses the callbacks and everything else you have setup in the individual easy handles. It transfers data on all current transfers in the multi stack that are ready to transfer anything. It may be all, it may be none. When there is nothing more to do for now, it returns back to the calling application.

Your application extracts info from libcurl about when it would like to get invoked to transfer data or do other work. The most convenient way is to use curl_multi_poll(3) that helps you wait until the application should call libcurl again. The older API to accomplish the same thing is curl_multi_fdset(3) that extracts fd_sets from libcurl to use in select() or poll() calls in order to get to know when the transfers in the multi stack might need attention. Both these APIs allow for your program to wait for input on your own private file descriptors at the same time. curl_multi_timeout(3) also helps you with providing a suitable timeout period for your select() calls.

curl_multi_perform(3) stores the number of still running transfers in one of its input arguments, and by reading that you can figure out when all the transfers in the multi handles are done. 'done' does not mean successful. One or more of the transfers may have failed.

To get information about completed transfers, to figure out success or not and similar, curl_multi_info_read(3) should be called. It can return a message about a current or previous transfer. Repeated invokes of the function get more messages until the message queue is empty. The information you receive there includes an easy handle pointer which you may use to identify which easy handle the information regards.

When a single transfer is completed, the easy handle is still left added to the multi stack. You need to first remove the easy handle with curl_multi_remove_handle(3) and then close it with curl_easy_cleanup(3), or possibly set new options to it and add it again with curl_multi_add_handle(3) to start another transfer.

When all transfers in the multi stack are done, close the multi handle with curl_multi_cleanup(3). Be careful and please note that you MUST invoke separate curl_easy_cleanup(3) calls for every single easy handle to clean them up properly.

If you want to reuse an easy handle that was added to the multi handle for transfer, you must first remove it from the multi stack and then re-add it again (possibly after having altered some options at your own choice).

MULTI_SOCKET

curl_multi_socket_action(3) function offers a way for applications to not only avoid being forced to use select(), but it also offers a much more high-performance API that makes a significant difference for applications using large numbers of simultaneous connections.

curl_multi_socket_action(3) is then used instead of curl_multi_perform(3).

When using this API, you add easy handles to the multi handle like with the normal multi interface. Then you also set two callbacks with the CURLMOPT_SOCKETFUNCTION(3) and CURLMOPT_TIMERFUNCTION(3) options to curl_multi_setopt(3). They are two callback functions that libcurl calls with information about what sockets to wait for, and for what activity, and what the current timeout time is - if that expires libcurl should be notified.

The multi_socket API is designed to inform your application about which sockets libcurl is currently using and for what activities (read and/or write) on those sockets your application is expected to wait for.

Your application must make sure to receive all sockets informed about in the CURLMOPT_SOCKETFUNCTION(3) callback and make sure it reacts on the given activity on them. When a socket has the given activity, you call curl_multi_socket_action(3) specifying which socket and action there are.

The CURLMOPT_TIMERFUNCTION(3) callback is called to set a timeout. When that timeout expires, your application should call the curl_multi_socket_action(3) function saying it was due to a timeout.

This API is typically used with an event-driven underlying functionality (like libevent, libev, kqueue, epoll or similar) with which the application "subscribes" on socket changes. This allows applications and libcurl to much better scale upward and beyond thousands of simultaneous transfers without losing performance.

When you have added your initial set of handles, you call curl_multi_socket_action(3) with CURL_SOCKET_TIMEOUT set in the sockfd argument, and you get callbacks invoked that set you up and you then continue to call curl_multi_socket_action(3) accordingly when you get activity on the sockets you have been asked to wait on, or if the timeout timer expires.

You can poll curl_multi_info_read(3) to see if any transfer has completed, as it then has a message saying so. To know how many transfers are currently queued, running, pending or done, you can use the curl_multi_get_offt(3) function.

BLOCKING

A few areas in the code are still using blocking code, even when used from the multi interface. While we certainly want and intend for these to get fixed in the future, you should be aware of the following current restrictions:

  • Name resolves unless the c-ares or threaded-resolver backends are used
  • file:// transfers
  • TELNET transfers
  • FTP, IMAP, SMTP, SCP and SFTP wait for the final server response before the transfer is reported as done
  • FTP, IMAP, POP3, SMTP, SCP and SFTP can block during protocol-specific shutdown when a connection is closed instead of kept for reuse
1---
2c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
3SPDX-License-Identifier: curl
4Title: libcurl-multi
5Section: 3
6Source: libcurl
7See-also:
8 - libcurl (3)
9 - libcurl-easy (3)
10 - libcurl-errors (3)
11Protocol:
12 - All
13Added-in: 7.9.6
14---
16# NAME
18libcurl-multi - how to use the multi interface
20# DESCRIPTION
22This is an overview on how to use the libcurl multi interface in your C
23programs. There are specific man pages for each function mentioned in
24here. There is also the libcurl-tutorial(3) man page for a complete
25tutorial to programming with libcurl and the libcurl-easy(3) man page
26for an overview of the libcurl easy interface.
28All functions in the multi interface are prefixed with curl_multi.
30# OBJECTIVES
32The multi interface offers several abilities that the easy interface does not.
33They are mainly:
351. Enable a "pull" interface. The application that uses libcurl decides where
36and when to ask libcurl to get/send data.
382. Enable multiple simultaneous transfers in the same thread without making it
39complicated for the application.
413. Enable the application to wait for action on its own file descriptors and
42curl's file descriptors simultaneously.
444. Enable event-based handling and scaling transfers up to and beyond
45thousands of parallel connections.
47# ONE MULTI HANDLE MANY EASY HANDLES
49To use the multi interface, you must first create a 'multi handle' with
50curl_multi_init(3). This handle is then used as input to all further
51curl_multi_* functions.
53With a multi handle and the multi interface you can do several simultaneous
54transfers in parallel. Each single transfer is built up around an easy
55handle. You create all the easy handles you need, and setup the appropriate
56options for each easy handle using curl_easy_setopt(3).
58There are two flavors of the multi interface, the select() oriented one and
59the event based one we call multi_socket. You benefit from reading through the
60description of both versions to fully understand how they work and
61differentiate. We start out with the select() oriented version.
63When an easy handle is setup and ready for transfer, then instead of using
64curl_easy_perform(3) like when using the easy interface for transfers,
65you should add the easy handle to the multi handle with
66curl_multi_add_handle(3). You can add more easy handles to a multi
67handle at any point, even if other transfers are already running.
69Should you change your mind, the easy handle is again removed from the multi
70stack using curl_multi_remove_handle(3). Once removed from the multi
71handle, you can again use other easy interface functions like
72curl_easy_perform(3) on the handle or whatever you think is
73necessary. You can remove handles at any point during transfers.
75Adding the easy handle to the multi handle does not start the transfer.
76Remember that one of the main ideas with this interface is to let your
77application drive. You drive the transfers by invoking
78curl_multi_perform(3). libcurl then transfers data if there is anything
79available to transfer. It uses the callbacks and everything else you have
80setup in the individual easy handles. It transfers data on all current
81transfers in the multi stack that are ready to transfer anything. It may be
82all, it may be none. When there is nothing more to do for now, it returns back
83to the calling application.
85Your application extracts info from libcurl about when it would like to get
86invoked to transfer data or do other work. The most convenient way is to use
87curl_multi_poll(3) that helps you wait until the application should call
88libcurl again. The older API to accomplish the same thing is
89curl_multi_fdset(3) that extracts *fd_sets* from libcurl to use in
90select() or poll() calls in order to get to know when the transfers in the
91multi stack might need attention. Both these APIs allow for your program to
92wait for input on your own private file descriptors at the same time.
93curl_multi_timeout(3) also helps you with providing a suitable timeout
94period for your select() calls.
96curl_multi_perform(3) stores the number of still running transfers in
97one of its input arguments, and by reading that you can figure out when all
98the transfers in the multi handles are done. 'done' does not mean
99successful. One or more of the transfers may have failed.
101To get information about completed transfers, to figure out success or not and
102similar, curl_multi_info_read(3) should be called. It can return a
103message about a current or previous transfer. Repeated invokes of the function
104get more messages until the message queue is empty. The information you
105receive there includes an easy handle pointer which you may use to identify
106which easy handle the information regards.
108When a single transfer is completed, the easy handle is still left added to
109the multi stack. You need to first remove the easy handle with
110curl_multi_remove_handle(3) and then close it with
111curl_easy_cleanup(3), or possibly set new options to it and add it again
112with curl_multi_add_handle(3) to start another transfer.
114When all transfers in the multi stack are done, close the multi handle with
115curl_multi_cleanup(3). Be careful and please note that you **MUST**
116invoke separate curl_easy_cleanup(3) calls for every single easy handle
117to clean them up properly.
119If you want to reuse an easy handle that was added to the multi handle for
120transfer, you must first remove it from the multi stack and then re-add it
121again (possibly after having altered some options at your own choice).
123# MULTI_SOCKET
125curl_multi_socket_action(3) function offers a way for applications to
126not only avoid being forced to use select(), but it also offers a much more
127high-performance API that makes a significant difference for applications
128using large numbers of simultaneous connections.
130curl_multi_socket_action(3) is then used instead of
131curl_multi_perform(3).
133When using this API, you add easy handles to the multi handle like with the
134normal multi interface. Then you also set two callbacks with the
135CURLMOPT_SOCKETFUNCTION(3) and CURLMOPT_TIMERFUNCTION(3) options to
136curl_multi_setopt(3). They are two callback functions that libcurl calls with
137information about what sockets to wait for, and for what activity, and what
138the current timeout time is - if that expires libcurl should be notified.
140The multi_socket API is designed to inform your application about which
141sockets libcurl is currently using and for what activities (read and/or write)
142on those sockets your application is expected to wait for.
144Your application must make sure to receive all sockets informed about in the
145CURLMOPT_SOCKETFUNCTION(3) callback and make sure it reacts on the given
146activity on them. When a socket has the given activity, you call
147curl_multi_socket_action(3) specifying which socket and action there
148are.
150The CURLMOPT_TIMERFUNCTION(3) callback is called to set a timeout. When
151that timeout expires, your application should call the
152curl_multi_socket_action(3) function saying it was due to a timeout.
154This API is typically used with an event-driven underlying functionality (like
155libevent, libev, kqueue, epoll or similar) with which the application
156"subscribes" on socket changes. This allows applications and libcurl to much
157better scale upward and beyond thousands of simultaneous transfers without
158losing performance.
160When you have added your initial set of handles, you call
161curl_multi_socket_action(3) with CURL_SOCKET_TIMEOUT set in the
162*sockfd* argument, and you get callbacks invoked that set you up and you
163then continue to call curl_multi_socket_action(3) accordingly when you
164get activity on the sockets you have been asked to wait on, or if the timeout
165timer expires.
167You can poll curl_multi_info_read(3) to see if any transfer has
168completed, as it then has a message saying so. To know how many transfers are
169currently queued, running, pending or done, you can use the
170curl_multi_get_offt(3) function.
172# BLOCKING
174A few areas in the code are still using blocking code, even when used from the
175multi interface. While we certainly want and intend for these to get fixed in
176the future, you should be aware of the following current restrictions:
178- Name resolves unless the c-ares or threaded-resolver backends are used
179- `file://` transfers
180- TELNET transfers
181- FTP, IMAP, SMTP, SCP and SFTP wait for the final server response before the
182transfer is reported as done
183- FTP, IMAP, POP3, SMTP, SCP and SFTP can block during protocol-specific
184shutdown when a connection is closed instead of kept for reuse
Diagnostics4 git calls · 191 ms
CallStatusSizeTime0191 ms
discover200152 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=152
git.server.agent=gitty/1.0
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch object-format
git.server.fetch=shallow filter sideband-all
git.protocol.version=2
git.pkt.frames=8
git.pkt.data=6
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=152
git.pkt.payload.max=34
ls-refs200149 B · hit2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=149
git.response.cache=HIT
git.response.duration_ms=2
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=149
git.pkt.payload.max=78
git.ls_refs.count=2
git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit26 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=293470
git.response.cache=HIT
git.response.duration_ms=26
git.fetch.filter=blob:none
git.fetch.filter.requested=tree:0
git.fetch.filter.fallback=blob:none
git.fetch.filter.learned=false
git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=10
git.pkt.data=8
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=293470
git.pkt.payload.max=65516
git.sideband.band1.bytes=293354
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=87
git.pack.bytes=293354
git.pack.ofs_delta=0
git.pack.ref_delta=1
git.pack.chain.max=1
git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0
git.pack.full=86
Open pack map
fetch2003.5 KiB163 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=163
git.response.bytes=3533
git.response.cache=MISS
git.response.duration_ms=163
git.fetch.wants=0d56dac769bd0aa3d1fdf35bc24d8b400a1e2552
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=3533
git.pkt.payload.max=3512
git.sideband.band1.bytes=3511
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
Ref
refs/heads/master
Commit
31c911efe5652725e930f4d2e60c9060e5f6646d
cf-ray
a5e1d5c37cbb3f27
Request
f062b1a2…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format