Skip to content

Instantly share code, notes, and snippets.

@kilork
Last active May 27, 2020 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kilork/4899caddd6b6f9d9263c0b48c2d36a5c to your computer and use it in GitHub Desktop.
Save kilork/4899caddd6b6f9d9263c0b48c2d36a5c to your computer and use it in GitHub Desktop.
Retry-After tests
$ curl --version
curl 7.70.0 (x86_64-w64-mingw32) libcurl/7.70.0 OpenSSL/1.1.1g (Schannel) zlib/1.2.11 brotli/1.0.7 libidn2/2.3.0 libpsl/0.21.0 (+libidn2/2.3.0) libssh2/1.9.0 nghttp2/1.40.0
Release-Date: 2020-04-29
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp scp sftp smtp smtps telnet tftp
Features: AsynchDNS brotli HTTP2 HTTPS-proxy IDN IPv6 Kerberos Largefile libz Metalink MultiSSL NTLM PSL SPNEGO SSL SSPI TLS-SRP
$ curl -v -L --retry 4 -H "Content-Type: application/json" --data '{"hello":"world"}' http://localhost:8000/report/generate
* Trying ::1:8000...
* Trying 127.0.0.1:8000...
* Connected to localhost (127.0.0.1) port 8000 (#0)
> POST /report/generate HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.70.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 17
>
* upload completely sent off: 17 out of 17 bytes
* Mark bundle as not supporting multiuse
< HTTP/1.1 303 See Other
< content-length: 0
< location: /poll/7
<
* Connection #0 to host localhost left intact
* Issue another request to this URL: 'http://localhost:8000/poll/7'
* Switch to GET
* Found bundle for host localhost: 0x26047a0 [serially]
* Can not multiplex, even if we wanted to!
* Re-using existing connection! (#0) with host localhost
* Connected to localhost (127.0.0.1) port 8000 (#0)
> GET /poll/7 HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.70.0
> Accept: */*
> Content-Type: application/json
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 429 Too Many Requests
< content-length: 65
< retry-after: 5
< content-type: text/html
<
<head><meta http-equiv="Refresh" content="5; URL=/poll/7"></head>* Connection #0 to host localhost left intact
Warning: Transient problem: HTTP error Will retry in 5 seconds. 4 retries
Warning: left.
* Found bundle for host localhost: 0x26047a0 [serially]
* Can not multiplex, even if we wanted to!
* Re-using existing connection! (#0) with host localhost
* Connected to localhost (127.0.0.1) port 8000 (#0)
> GET /report/generate HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.70.0
> Accept: */*
> Content-Type: application/json
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 303 See Other
< content-length: 0
< location: /poll/8
<
* Connection #0 to host localhost left intact
* Issue another request to this URL: 'http://localhost:8000/poll/8'
* Found bundle for host localhost: 0x26047a0 [serially]
* Can not multiplex, even if we wanted to!
* Re-using existing connection! (#0) with host localhost
* Connected to localhost (127.0.0.1) port 8000 (#0)
> GET /poll/8 HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.70.0
> Accept: */*
> Content-Type: application/json
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 429 Too Many Requests
< content-length: 65
< retry-after: 5
< content-type: text/html
<
<head><meta http-equiv="Refresh" content="5; URL=/poll/8"></head>* Connection #0 to host localhost left intact
Warning: Transient problem: HTTP error Will retry in 5 seconds. 3 retries
Warning: left.
* Found bundle for host localhost: 0x26047a0 [serially]
* Can not multiplex, even if we wanted to!
* Re-using existing connection! (#0) with host localhost
* Connected to localhost (127.0.0.1) port 8000 (#0)
> GET /report/generate HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.70.0
> Accept: */*
> Content-Type: application/json
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 303 See Other
< content-length: 0
< location: /poll/9
<
* Connection #0 to host localhost left intact
* Issue another request to this URL: 'http://localhost:8000/poll/9'
* Found bundle for host localhost: 0x26047a0 [serially]
* Can not multiplex, even if we wanted to!
* Re-using existing connection! (#0) with host localhost
* Connected to localhost (127.0.0.1) port 8000 (#0)
> GET /poll/9 HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.70.0
> Accept: */*
> Content-Type: application/json
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 429 Too Many Requests
< content-length: 65
< retry-after: 5
< content-type: text/html
<
<head><meta http-equiv="Refresh" content="5; URL=/poll/9"></head>* Connection #0 to host localhost left intact
Warning: Transient problem: HTTP error Will retry in 5 seconds. 2 retries
Warning: left.
* Found bundle for host localhost: 0x26047a0 [serially]
* Can not multiplex, even if we wanted to!
* Re-using existing connection! (#0) with host localhost
* Connected to localhost (127.0.0.1) port 8000 (#0)
> GET /report/generate HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.70.0
> Accept: */*
> Content-Type: application/json
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 303 See Other
< content-length: 0
< location: /poll/10
<
* Connection #0 to host localhost left intact
* Issue another request to this URL: 'http://localhost:8000/poll/10'
* Found bundle for host localhost: 0x26047a0 [serially]
* Can not multiplex, even if we wanted to!
* Re-using existing connection! (#0) with host localhost
* Connected to localhost (127.0.0.1) port 8000 (#0)
> GET /poll/10 HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.70.0
> Accept: */*
> Content-Type: application/json
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 429 Too Many Requests
< content-length: 66
< retry-after: 5
< content-type: text/html
<
<head><meta http-equiv="Refresh" content="5; URL=/poll/10"></head>* Connection #0 to host localhost left intact
Warning: Transient problem: HTTP error Will retry in 5 seconds. 1 retries
Warning: left.
* Found bundle for host localhost: 0x26047a0 [serially]
* Can not multiplex, even if we wanted to!
* Re-using existing connection! (#0) with host localhost
* Connected to localhost (127.0.0.1) port 8000 (#0)
> GET /report/generate HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.70.0
> Accept: */*
> Content-Type: application/json
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 303 See Other
< content-length: 0
< location: /poll/11
<
* Connection #0 to host localhost left intact
* Issue another request to this URL: 'http://localhost:8000/poll/11'
* Found bundle for host localhost: 0x26047a0 [serially]
* Can not multiplex, even if we wanted to!
* Re-using existing connection! (#0) with host localhost
* Connected to localhost (127.0.0.1) port 8000 (#0)
> GET /poll/11 HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.70.0
> Accept: */*
> Content-Type: application/json
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 429 Too Many Requests
< content-length: 66
< retry-after: 5
< content-type: text/html
<
<head><meta http-equiv="Refresh" content="5; URL=/poll/11"></head>* Connection #0 to host localhost left intact
* Trying ::1:8000...
* Connection failed
* connect to ::1 port 8000 failed: Connection refused
* Trying 127.0.0.1:8000...
* Connected to localhost (127.0.0.1) port 8000 (#0)
> POST /report/generate HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.70.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 17
>
* upload completely sent off: 17 out of 17 bytes
* Mark bundle as not supporting multiuse
< HTTP/1.1 301 Moved Permanently
< content-length: 0
< location: /poll/1
<
* Connection #0 to host localhost left intact
* Issue another request to this URL: 'http://localhost:8000/poll/1'
* Switch from POST to GET
* Found bundle for host localhost: 0x7f914b607f80 [serially]
* Re-using existing connection! (#0) with host localhost
* Connected to localhost (127.0.0.1) port 8000 (#0)
> GET /poll/1 HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.70.0
> Accept: */*
> Content-Type: application/json
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 429 Too Many Requests
< content-length: 0
< retry-after: 5
<
* Connection #0 to host localhost left intact
Warning: Transient problem: HTTP error Will retry in 5 seconds. 4 retries
Warning: left.
* Found bundle for host localhost: 0x7f914b607f80 [serially]
* Re-using existing connection! (#0) with host localhost
* Connected to localhost (127.0.0.1) port 8000 (#0)
> GET /report/generate HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.70.0
> Accept: */*
> Content-Type: application/json
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 301 Moved Permanently
< content-length: 0
< location: /poll/2
<
import { serve } from "https://deno.land/std@0.53.0/http/server.ts";
const s = serve({ port: 8000 });
console.log("http://localhost:8000/");
let i = 0;
let reports: Map<string, boolean> = new Map();
let id: number = 0;
for await (const req of s) {
console.log(`${req.method} ${req.url}`);
req.headers.forEach((v, k) => {
console.log(`${k}: ${v}`);
});
switch (req.url) {
case "/report/generate":
let trackId = id.toString();
id += 1;
reports.set(trackId, false);
let delay: number = Math.round(Math.random() * 20);
console.log(`long running report ${trackId} requires ${delay} seconds to finish... `);
setTimeout(() => {
reports.set(trackId, true);
console.log(`report ${trackId} is ready`);
}, delay * 1000);
let headers = new Headers();
headers.set("Location", `/poll/${trackId}`);
req.respond({ status: 303, headers }); // we trigger here redirect right now and use 303 to clear body (next would be GET instead of POST)
break;
default:
let m: any = /\/poll\/(\d+)/.exec(req.url);
if ((m?.length || 0) > 0) {
let trackId = m[1];
if (reports.get(trackId)) {
let headers = new Headers();
headers.set("Location", `/download/${trackId}`);
req.respond({ status: 301, headers });
} else {
let headers = new Headers();
headers.set("Retry-After", "5");
// we are using 429 here, but actually we need to use 301. but no clients support this correctly, curl support only 429
req.respond({ status: 429, headers });
}
} else {
m = /\/download\/(\d+)/.exec(req.url);
if ((m?.length || 0) > 0) {
let trackId = m[1];
if (reports.get(trackId)) {
req.respond({ body: "Hello, world!" });
} else {
req.respond({ status: 404 });
}
}
}
break;
}
}
$ wget --tries=100 --retry-on-http-error=429 -SO- --progress= --post-data="{}" http://localhost:8000/report/generate
--2020-05-26 17:02:38-- http://localhost:8000/report/generate
Auflösen des Hostnamens localhost (localhost)… ::1, 127.0.0.1
Verbindungsaufbau zu localhost (localhost)|::1|:8000 … fehlgeschlagen: Connection refused.
Verbindungsaufbau zu localhost (localhost)|127.0.0.1|:8000 … verbunden.
HTTP-Anforderung gesendet, auf Antwort wird gewartet …
HTTP/1.1 303 See Other
content-length: 0
location: /poll/14
Platz: /poll/14 [folgend]
--2020-05-26 17:02:40-- http://localhost:8000/poll/14
Wiederverwendung der bestehenden Verbindung zu localhost:8000.
HTTP-Anforderung gesendet, auf Antwort wird gewartet …
HTTP/1.1 429 Too Many Requests
content-length: 0
retry-after: 5
Erneuter Versuch.
--2020-05-26 17:02:41-- (Versuch: 2) http://localhost:8000/poll/14
Wiederverwendung der bestehenden Verbindung zu localhost:8000.
HTTP-Anforderung gesendet, auf Antwort wird gewartet …
HTTP/1.1 429 Too Many Requests
content-length: 0
retry-after: 5
Erneuter Versuch.
--2020-05-26 17:02:43-- (Versuch: 3) http://localhost:8000/poll/14
Wiederverwendung der bestehenden Verbindung zu localhost:8000.
HTTP-Anforderung gesendet, auf Antwort wird gewartet …
HTTP/1.1 429 Too Many Requests
content-length: 0
retry-after: 5
Erneuter Versuch.
--2020-05-26 17:02:46-- (Versuch: 4) http://localhost:8000/poll/14
Wiederverwendung der bestehenden Verbindung zu localhost:8000.
HTTP-Anforderung gesendet, auf Antwort wird gewartet …
HTTP/1.1 429 Too Many Requests
content-length: 0
retry-after: 5
Erneuter Versuch.
--2020-05-26 17:02:50-- (Versuch: 5) http://localhost:8000/poll/14
Wiederverwendung der bestehenden Verbindung zu localhost:8000.
HTTP-Anforderung gesendet, auf Antwort wird gewartet …
HTTP/1.1 301 Moved Permanently
content-length: 0
location: /download/14
Platz: /download/14 [folgend]
--2020-05-26 17:02:50-- http://localhost:8000/download/14
Wiederverwendung der bestehenden Verbindung zu localhost:8000.
HTTP-Anforderung gesendet, auf Antwort wird gewartet …
HTTP/1.1 200 OK
content-length: 13
Länge: 13
Wird in »STDOUT« gespeichert.
Hello, world!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment