From 236cedc7cba21132a2280c86ae4cf44c057ab5d8 Mon Sep 17 00:00:00 2001 From: "Kevin (Kun) \"Kassimo\" Qian" Date: Sat, 13 Apr 2019 12:23:56 -0700 Subject: Enforce HTTP/1.1 pipeline response order (denoland/deno_std#331) Original: https://github.com/denoland/deno_std/commit/144ef0e08d589fad2ca19eb4ef1ea20f1749bb5c --- http/racing_server.ts | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 http/racing_server.ts (limited to 'http/racing_server.ts') diff --git a/http/racing_server.ts b/http/racing_server.ts new file mode 100644 index 000000000..c44fc1216 --- /dev/null +++ b/http/racing_server.ts @@ -0,0 +1,53 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +import { serve, ServerRequest } from "./server.ts"; + +const addr = Deno.args[1] || "127.0.0.1:4501"; +const server = serve(addr); + +const body = new TextEncoder().encode("Hello 1\n"); +const body4 = new TextEncoder().encode("World 4\n"); + +function sleep(ms: number): Promise { + return new Promise(res => setTimeout(res, ms)); +} + +async function delayedRespond(request: ServerRequest): Promise { + await sleep(3000); + await request.respond({ status: 200, body }); +} + +async function largeRespond(request: ServerRequest, c: string): Promise { + const b = new Uint8Array(1024 * 1024); + b.fill(c.charCodeAt(0)); + await request.respond({ status: 200, body: b }); +} + +async function main(): Promise { + let step = 1; + for await (const request of server) { + switch (step) { + case 1: + // Try to wait long enough. + // For pipelining, this should cause all the following response + // to block. + delayedRespond(request); + break; + case 2: + // HUGE body. + largeRespond(request, "a"); + break; + case 3: + // HUGE body. + largeRespond(request, "b"); + break; + default: + request.respond({ status: 200, body: body4 }); + break; + } + step++; + } +} + +main(); + +console.log("Racing server listening...\n"); -- cgit v1.2.3