-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Manage HTTP/2 connections and keep them alive with PING frames (#673)
- Loading branch information
Showing
12 changed files
with
1,855 additions
and
464 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Copyright 2021-2023 Buf Technologies, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import { Code, ConnectError } from "@bufbuild/connect"; | ||
import { | ||
createConnectTransport, | ||
Http2SessionManager, | ||
} from "@bufbuild/connect-node"; | ||
|
||
describe("createConnectTransport()", function () { | ||
it("should take just httpVersion and baseUrl", function () { | ||
const t = createConnectTransport({ | ||
httpVersion: "2", | ||
baseUrl: "https://example.com", | ||
}); | ||
expect(t).toBeDefined(); | ||
}); | ||
it("should take session options", function () { | ||
const t = createConnectTransport({ | ||
httpVersion: "2", | ||
baseUrl: "https://example.com", | ||
pingIntervalMs: 1000 * 30, | ||
pingIdleConnection: true, | ||
pingTimeoutMs: 1000 * 5, | ||
idleConnectionTimeoutMs: 1000 * 60 * 5, | ||
}); | ||
expect(t).toBeDefined(); | ||
}); | ||
it("should take node options", function () { | ||
const t = createConnectTransport({ | ||
httpVersion: "2", | ||
baseUrl: "https://example.com", | ||
nodeOptions: { | ||
maxSessionMemory: 1024 * 1024 * 4, | ||
}, | ||
}); | ||
expect(t).toBeDefined(); | ||
}); | ||
it("should take session manager", function () { | ||
const sm = new Http2SessionManager( | ||
"https://example.com", | ||
{ | ||
pingIntervalMs: 1000 * 10, | ||
}, | ||
{ | ||
maxSessionMemory: 1024 * 1024 * 4, | ||
} | ||
); | ||
const t = createConnectTransport({ | ||
httpVersion: "2", | ||
baseUrl: "https://example.com", | ||
sessionManager: sm, | ||
}); | ||
expect(t).toBeDefined(); | ||
}); | ||
}); | ||
|
||
describe("using a session manager to open a connection before starting an application", function () { | ||
it("should work", async function () { | ||
const sm = new Http2SessionManager("https://demo.connect.build"); | ||
for (let backoff = 1; ; backoff++) { | ||
const state = await sm.connect(); | ||
if (state == "error") { | ||
// For a transient error, we can retry here | ||
const reason = sm.error(); | ||
if (ConnectError.from(reason).code !== Code.Unavailable) { | ||
throw sm.error(); | ||
} | ||
await new Promise<void>((resolve) => | ||
setTimeout(resolve, backoff * 1000) | ||
); | ||
} else { | ||
// we are connected (either open or idle), break the loop | ||
break; | ||
} | ||
} | ||
// here we would enter the application logic, and start calling RPCs | ||
}); | ||
}); | ||
|
||
describe("using a session manager to explicitly close all connections", function () { | ||
it("should work", function () { | ||
// create a client, keeping a reference to the session manage | ||
const sessionManager = new Http2SessionManager( | ||
"https://demo.connect.build" | ||
); | ||
createConnectTransport({ | ||
httpVersion: "2", | ||
baseUrl: "https://demo.connect.build", | ||
sessionManager, | ||
}); | ||
// const client = createPromiseClient(..., transport); | ||
|
||
// make calls with the client | ||
|
||
// close the connection | ||
sessionManager.abort(); | ||
}); | ||
}); |
Oops, something went wrong.