Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(node/http2): fixes to support grpc #20712

Merged
merged 19 commits into from
Oct 12, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(node/http2): use options.createConnection
  • Loading branch information
bartlomieju committed Sep 27, 2023
commit 0b74980e235e126f4ce2262a15c856cd40a958ec
65 changes: 48 additions & 17 deletions ext/node/polyfills/http2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Buffer } from "node:buffer";
import { Server, Socket, TCP } from "node:net";
import { TypedArray } from "ext:deno_node/internal/util/types.ts";
import {
kHandle,
kMaybeDestroy,
kUpdateTimer,
setStreamTimeout,
Expand Down Expand Up @@ -83,7 +84,7 @@ const SESSION_FLAGS_DESTROYED = 0x4;
const ENCODER = new TextEncoder();
type Http2Headers = Record<string, string | string[]>;

const debugHttp2Enabled = false;
const debugHttp2Enabled = true;
bartlomieju marked this conversation as resolved.
Show resolved Hide resolved
function debugHttp2(...args) {
if (debugHttp2Enabled) {
console.log(...args);
Expand Down Expand Up @@ -264,7 +265,7 @@ export class Http2Session extends EventEmitter {
}

setTimeout(msecs: number, callback?: () => void) {
setStreamTimeout(this, msecs, callback);
setStreamTimeout.call(this, msecs, callback);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes problems with setStreamTimeout that were happening before

}
}

Expand Down Expand Up @@ -302,6 +303,7 @@ function closeSession(session: Http2Session, code?: number, error?: Error) {
session[kDenoConnRid],
session[kDenoClientRid],
);
console.table(Deno.resources());
core.tryClose(session[kDenoConnRid]);
core.tryClose(session[kDenoClientRid]);

Expand Down Expand Up @@ -354,9 +356,10 @@ export class ClientHttp2Session extends Http2Session {
// TODO(bartlomieju): cleanup
this.#connectPromise = (async () => {
debugHttp2(">>> before connect");
const conn = await connPromise;
const [clientRid, connRid] = await op_http2_connect(conn.rid, url);
debugHttp2(">>> after connect");
const connRid_ = await connPromise;
console.log(">>>> awaited connRid", connRid_);
const [clientRid, connRid] = await op_http2_connect(connRid_, url);
debugHttp2(">>> after connect", clientRid, connRid);
this[kDenoClientRid] = clientRid;
this[kDenoConnRid] = connRid;
// TODO(bartlomieju): save this promise, so the session can be unrefed
Expand Down Expand Up @@ -1190,7 +1193,9 @@ function finishCloseStream(stream, code) {
);
core.tryClose(stream[kDenoRid]);
core.tryClose(stream[kDenoResponse].bodyRid);
stream.emit("close");
nextTick(() => {
stream.emit("close");
});
}).catch(() => {
debugHttp2(
">>> finishCloseStream close2 catch",
Expand All @@ -1199,7 +1204,11 @@ function finishCloseStream(stream, code) {
);
core.tryClose(stream[kDenoRid]);
core.tryClose(stream[kDenoResponse].bodyRid);
stream.emit("close");
nextTick(() => {
nextTick(() => {
stream.emit("close");
});
});
bartlomieju marked this conversation as resolved.
Show resolved Hide resolved
});
}
}
Expand Down Expand Up @@ -1488,24 +1497,46 @@ export function connect(
host = authority.host;
}

let conn, nodeConn, url;

// TODO(bartlomieju): handle defaults
if (typeof options.createConnection === "function") {
console.error("Not implemented: http2.connect.options.createConnection");
// console.error("Not implemented: http2.connect.options.createConnection");
// notImplemented("http2.connect.options.createConnection");
nodeConn = options.createConnection(authority, options);
} else {
// TODO(bartlomieju): using `localhost` as a `host` refuses to connect
// when server is listening on `0.0.0.0`
console.log(protocol, host, port);

if (protocol == "http:") {
conn = Deno.connect({ port, hostname: "127.0.0.1" });
url = `https://${host}${port == 80 ? "" : (":" + port)}`;
} else if (protocol == "https:") {
conn = Deno.connectTls({ port, hostname: host, alpnProtocols: ["h2"] });
url = `https://${host}${port == 443 ? "" : (":" + port)}`;
} else {
throw new TypeError("Unexpected URL protocol");
}
}

let conn, url;
if (protocol == "http:") {
conn = Deno.connect({ port, hostname: host });
url = `https://${host}${port == 80 ? "" : (":" + port)}`;
} else if (protocol == "https:") {
conn = Deno.connectTls({ port, hostname: host, alpnProtocols: ["h2"] });
url = `https://${host}${port == 443 ? "" : (":" + port)}`;
let connPromise;

if (nodeConn) {
connPromise = new Promise((resolve) => {
nodeConn.once("connect", () => {
const rid = nodeConn[kHandle][kStreamBaseField].rid;
resolve(rid);
});
});
} else {
throw new TypeError("Unexpected URL protocol");
connPromise = (async () => {
const c = await conn;
return c.rid;
})();
}

const session = new ClientHttp2Session(conn, url, options);
const session = new ClientHttp2Session(connPromise, url, options);
session[kAuthority] = `${options.servername || host}:${port}`;
session[kProtocol] = protocol;

Expand Down