master
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24#include "first.h"
26/* An event-based download client, driving transfers purely via
27 * curl_multi_socket_action() with its own socket/timer callbacks, the
28 * way an event-based application does. It runs its transfers serially
29 * with CURLOPT_FORBID_REUSE, so every transfer's connection enters
30 * shutdown when done, and reports whether the connection sockets
31 * remain under event supervision until their shutdown finished. */
33#define EV_DL_MAX_SOCKS 8
35struct ev_dl_ctx {
36 CURLM *multi;
37 struct curltime timer_started;
38 curl_socket_t fds[EV_DL_MAX_SOCKS];
39 int actions[EV_DL_MAX_SOCKS];
40 size_t nsocks;
41 long timer_ms; /* last timeout set, -1 for none */
42};
44static int ev_dl_socket_cb(CURL *easy, curl_socket_t fd, int what,
45 void *clientp, void *socketp)
46{
47 struct ev_dl_ctx *ctx = clientp;
48 size_t i;
50 (void)easy;
51 (void)socketp;
52 for(i = 0; i < ctx->nsocks; ++i) {
53 if(ctx->fds[i] == fd)
54 break;
55 }
56 if(what == CURL_POLL_REMOVE) {
57 if(i < ctx->nsocks) {
58 ctx->nsocks--;
59 ctx->fds[i] = ctx->fds[ctx->nsocks];
60 ctx->actions[i] = ctx->actions[ctx->nsocks];
61 }
62 }
63 else {
64 if(i == ctx->nsocks) {
65 if(ctx->nsocks >= EV_DL_MAX_SOCKS) {
66 curl_mfprintf(stderr, "[ev] too many sockets to track\n");
67 return -1;
68 }
69 ctx->nsocks++;
70 }
71 ctx->fds[i] = fd;
72 ctx->actions[i] = what;
73 }
74 curl_mfprintf(stderr, "[ev] socket fd=%ld what=%d, tracking %zu\n",
75 (long)fd, what, ctx->nsocks);
76 return 0;
77}
79static int ev_dl_timer_cb(CURLM *multi, long timeout_ms, void *clientp)
80{
81 struct ev_dl_ctx *ctx = clientp;
83 (void)multi;
84 ctx->timer_ms = timeout_ms;
85 if(timeout_ms >= 0)
86 ctx->timer_started = curlx_now();
87 return 0;
88}
90static size_t ev_dl_discard_cb(char *ptr, size_t size, size_t nmemb,
91 void *userdata)
92{
93 (void)ptr;
94 (void)userdata;
95 return size * nmemb;
96}
98/* wait for socket events or the timer, then feed libcurl. */
99static CURLMcode ev_dl_roundtrip(struct ev_dl_ctx *ctx)
100{
101 curl_socket_t fds[EV_DL_MAX_SOCKS];
102 int actions[EV_DL_MAX_SOCKS];
103 struct timeval tv;
104 fd_set rd, wr;
105 size_t i, nsocks;
106 long wait_ms = 100;
107 int maxfd = -1;
108 int running = 0;
109 CURLMcode mres = CURLM_OK;
111 FD_ZERO(&rd);
112 FD_ZERO(&wr);
113 for(i = 0; i < ctx->nsocks; ++i) {
114 if(ctx->actions[i] & CURL_POLL_IN)
115 FD_SET(ctx->fds[i], &rd);
116 if(ctx->actions[i] & CURL_POLL_OUT)
117 FD_SET(ctx->fds[i], &wr);
118 if((int)ctx->fds[i] > maxfd)
119 maxfd = (int)ctx->fds[i];
120 }
121 if(ctx->timer_ms >= 0) {
122 long left = ctx->timer_ms -
123 (long)curlx_timediff_ms(curlx_now(), ctx->timer_started);
124 if(left < 0)
125 left = 0;
126 if(left < wait_ms)
127 wait_ms = left;
128 }
129 tv.tv_sec = wait_ms / 1000;
130 tv.tv_usec = (int)(wait_ms % 1000) * 1000;
131 select_wrapper(maxfd + 1, &rd, &wr, NULL, &tv);
133 /* the callbacks change our socket table while we dispatch */
134 nsocks = ctx->nsocks;
135 memcpy(fds, ctx->fds, sizeof(fds));
136 memcpy(actions, ctx->actions, sizeof(actions));
137 for(i = 0; i < nsocks; ++i) {
138 int ev_bitmask = 0;
139 if((actions[i] & CURL_POLL_IN) && FD_ISSET(fds[i], &rd))
140 ev_bitmask |= CURL_CSELECT_IN;
141 if((actions[i] & CURL_POLL_OUT) && FD_ISSET(fds[i], &wr))
142 ev_bitmask |= CURL_CSELECT_OUT;
143 if(ev_bitmask) {
144 mres = curl_multi_socket_action(ctx->multi, fds[i], ev_bitmask,
145 &running);
146 if(mres)
147 return mres;
148 }
149 }
151 if((ctx->timer_ms >= 0) &&
152 (curlx_timediff_ms(curlx_now(), ctx->timer_started) >= ctx->timer_ms)) {
153 ctx->timer_ms = -1;
154 mres = curl_multi_socket_action(ctx->multi, CURL_SOCKET_TIMEOUT, 0,
155 &running);
156 }
157 return mres;
158}
160static void usage_ev_download(const char *msg)
161{
162 if(msg)
163 curl_mfprintf(stderr, "%s\n", msg);
164 curl_mfprintf(stderr,
165 "usage: [options] url\n"
166 " event-based downloads with connection reuse forbidden\n"
167 " -n number of transfers to do serially (default: 5)\n"
168 " -C certfile for CA verification\n"
169 " -t ms server response timeout (default: libcurl default)\n"
170 " -m ms test time limit per transfer (default: 30000)\n"
171 );
172}
174static CURLcode test_cli_ev_download(const char *URL)
175{
176 struct ev_dl_ctx ctx;
177 CURLM *multi = NULL;
178 char *cafile = NULL;
179 const char *url;
180 size_t transfer_count = 5;
181 long response_timeout_ms = 0;
182 long test_timeout_ms = 30000;
183 size_t i;
184 int watched = 0;
185 struct curltime started;
186 CURLcode result = CURLE_OK;
187 int ch;
189 (void)URL;
190 memset(&ctx, 0, sizeof(ctx));
191 ctx.timer_ms = -1;
193 while((ch = cgetopt(test_argc, test_argv, "hn:C:t:m:")) != -1) {
194 switch(ch) {
195 case 'h':
196 usage_ev_download(NULL);
197 result = (CURLcode)2;
198 goto optcleanup;
199 case 'n': {
200 const char *opt = coptarg;
201 curl_off_t num;
202 if(!curlx_str_number(&opt, &num, LONG_MAX))
203 transfer_count = (size_t)num;
204 break;
205 }
206 case 'C':
207 curlx_free(cafile);
208 cafile = curlx_strdup(coptarg);
209 break;
210 case 't':
211 case 'm': {
212 const char *opt = coptarg;
213 curl_off_t num;
214 if(curlx_str_number(&opt, &num, LONG_MAX)) {
215 usage_ev_download("invalid timeout");
216 result = (CURLcode)1;
217 goto optcleanup;
218 }
219 if(ch == 't')
220 response_timeout_ms = (long)num;
221 else
222 test_timeout_ms = (long)num;
223 break;
224 }
225 default:
226 usage_ev_download("invalid option");
227 result = (CURLcode)1;
228 goto optcleanup;
229 }
230 }
231 test_argc -= coptind;
232 test_argv += coptind;
234 if(test_argc != 1) {
235 usage_ev_download("not enough arguments");
236 result = (CURLcode)2;
237 goto optcleanup;
238 }
239 url = test_argv[0];
241 if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
242 curl_mfprintf(stderr, "curl_global_init() failed\n");
243 result = (CURLcode)3;
244 goto optcleanup;
245 }
246 curl_global_trace("ids,time");
248 multi = curl_multi_init();
249 if(!multi) {
250 curl_mfprintf(stderr, "curl_multi_init() failed\n");
251 result = (CURLcode)3;
252 goto cleanup;
253 }
254 ctx.multi = multi;
255 curl_multi_setopt(multi, CURLMOPT_SOCKETFUNCTION, ev_dl_socket_cb);
256 curl_multi_setopt(multi, CURLMOPT_SOCKETDATA, &ctx);
257 curl_multi_setopt(multi, CURLMOPT_TIMERFUNCTION, ev_dl_timer_cb);
258 curl_multi_setopt(multi, CURLMOPT_TIMERDATA, &ctx);
260 for(i = 0; i < transfer_count && !result; ++i) {
261 CURL *easy = curl_easy_init();
262 int done = 0;
263 int running = 0;
265 if(!easy) {
266 curl_mfprintf(stderr, "[t-%zu] FAILED setup\n", i);
267 result = (CURLcode)1;
268 goto cleanup;
269 }
270 curl_easy_setopt(easy, CURLOPT_URL, url);
271 curl_easy_setopt(easy, CURLOPT_WRITEFUNCTION, ev_dl_discard_cb);
272 curl_easy_setopt(easy, CURLOPT_FORBID_REUSE, 1L);
273 curl_easy_setopt(easy, CURLOPT_VERBOSE, 1L);
274 if(response_timeout_ms)
275 curl_easy_setopt(easy, CURLOPT_SERVER_RESPONSE_TIMEOUT_MS,
276 response_timeout_ms);
277 if(cafile)
278 curl_easy_setopt(easy, CURLOPT_CAINFO, cafile);
280 curl_multi_add_handle(multi, easy);
281 curl_mfprintf(stderr, "[t-%zu] STARTED\n", i);
282 curl_multi_socket_action(multi, CURL_SOCKET_TIMEOUT, 0, &running);
284 started = curlx_now();
285 while(!done) {
286 struct CURLMsg *m;
287 int msgq = 0;
289 if(ev_dl_roundtrip(&ctx)) {
290 curl_mfprintf(stderr, "[t-%zu] multi failure\n", i);
291 result = (CURLcode)1;
292 goto cleanup;
293 }
294 m = curl_multi_info_read(multi, &msgq);
295 if(m && (m->msg == CURLMSG_DONE)) {
296 done = 1;
297 result = m->data.result;
298 curl_mfprintf(stderr, "[t-%zu] FINISHED with result %d\n",
299 i, (int)result);
300 }
301 else if(curlx_timediff_ms(curlx_now(), started) > test_timeout_ms) {
302 curl_mfprintf(stderr, "[t-%zu] transfer timed out\n", i);
303 result = (CURLcode)1;
304 curl_multi_remove_handle(multi, easy);
305 curl_easy_cleanup(easy);
306 goto cleanup;
307 }
308 }
309 curl_multi_remove_handle(multi, easy);
310 curl_easy_cleanup(easy);
312 /* The transfer is over and its connection may not be reused. A
313 * graceful shutdown that could not finish right away needs its
314 * socket watched by us, or it can never make progress. */
315 curl_mfprintf(stderr, "[t-%zu] sockets tracked after done: %zu\n",
316 i, ctx.nsocks);
317 if(ctx.nsocks)
318 watched++;
319 }
321 /* drive the remaining shutdowns via socket events */
322 started = curlx_now();
323 while(ctx.nsocks &&
324 (curlx_timediff_ms(curlx_now(), started) < (timediff_t)5000)) {
325 if(ev_dl_roundtrip(&ctx))
326 break;
327 }
328 curl_mfprintf(stderr, "[ev] final: watched=%d socks_left=%zu\n",
329 watched, ctx.nsocks);
331cleanup:
332 curl_multi_cleanup(multi);
333 curl_global_cleanup();
334optcleanup:
335 curlx_free(cafile);
336 return result;
337}
Diagnostics4 git calls · 139 ms
CallStatusSizeTime0139 ms
discover200152 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch 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 · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=135 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=2 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=86Open pack map
fetch2003.5 KiB135 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=3553 git.response.cache=MISS git.response.duration_ms=135 git.fetch.wants=5aa99bedc1e2f03e4d3275191040bd4fb8a35b68 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=3553 git.pkt.payload.max=3532 git.sideband.band1.bytes=3531 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
- a5e30aa968f8ee28
- Request
- 5b35f83b…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format