Skip to content

Commit

Permalink
std/http/server.ts: Abort read when connection closes
Browse files Browse the repository at this point in the history
  • Loading branch information
nayeemrmn committed Jan 11, 2020
1 parent 1ea06f9 commit ce8ebf6
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion std/http/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,12 +375,17 @@ export async function readRequest(

export class Server implements AsyncIterable<ServerRequest> {
private closing = false;
private signalClose: () => void = () => {};
private signaledClose: Promise<void> = new Promise(
resolve => (this.signalClose = resolve)
);

constructor(public listener: Listener) {}

close(): void {
this.closing = true;
this.listener.close();
this.signalClose();
}

// Yields all HTTP requests on a single TCP connection.
Expand All @@ -394,7 +399,10 @@ export class Server implements AsyncIterable<ServerRequest> {

while (!this.closing) {
try {
req = await readRequest(conn, bufr);
req = await Promise.race([
this.signaledClose,
readRequest(conn, bufr)
]) || Deno.EOF;
} catch (e) {
err = e;
break;
Expand Down

0 comments on commit ce8ebf6

Please sign in to comment.