Skip to content

Commit

Permalink
[FE-2750][FE-2754] Support connection pool and network level timeouts…
Browse files Browse the repository at this point in the history
… and maxConns in ClientConfiguration (#6)
  • Loading branch information
cleve-fauna authored Oct 12, 2022
1 parent 755ba52 commit bc92b3e
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 3 deletions.
2 changes: 2 additions & 0 deletions __tests__/functional/client-configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ describe("endpoints", () => {
});
const client = new Client({
endpoint: endpoints["my-alternative-port"],
maxConns: 5,
secret: "secret",
queryTimeoutMillis: 60,
});
expect(client.client.defaults.baseURL).toEqual("http://localhost:7443/");
const result = await client.query<number>({ query: '"taco".length' });
expect(result.txn_time).not.toBeUndefined();
expect(result).toEqual({ data: 4, txn_time: result.txn_time });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Client } from "../../src/client";
import { endpoints } from "../../src/client-configuration";
import { QueryError } from "../../src/wire-protocol";
import { env } from "process";

describe("query", () => {
const client = new Client({
endpoint: endpoints.local,
secret: "secret",
endpoint: env["endpoint"] ? new URL(env["endpoint"]) : endpoints.local,
maxConns: 5,
secret: env["secret"] || "secret",
queryTimeoutMillis: 60,
});

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"author": "Fauna",
"private": true,
"dependencies": {
"agentkeepalive": "^4.2.1",
"axios": "^1.1.2"
},
"devDependencies": {
Expand Down
4 changes: 4 additions & 0 deletions src/client-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export interface ClientConfiguration {
* The {@link URL} of Fauna to call. See {@link endpoints} for some default options.
*/
endpoint: URL;
/**
* The maximum number of connections to a make to Fauna.
*/
maxConns: number;
/**
* A secret for your Fauna DB, used to authorize your queries.
* @see https://docs.fauna.com/fauna/current/security/keys
Expand Down
21 changes: 20 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import axios, { type Axios } from "axios";
import Agent, { HttpsAgent } from "agentkeepalive";
import type { ClientConfiguration } from "./client-configuration";
import {
QueryError,
Expand Down Expand Up @@ -31,9 +32,27 @@ export class Client {
*/
constructor(clientConfiguration: ClientConfiguration) {
this.clientConfiguration = clientConfiguration;
// ensure the network timeout > ClientConfiguration.queryTimeoutMillis so we don't
// terminate connections on active queries.
const timeout = this.clientConfiguration.queryTimeoutMillis + 10_000;
const agentSettings = {
maxSockets: this.clientConfiguration.maxConns,
maxFreeSockets: this.clientConfiguration.maxConns,
timeout,
// release socket for usage after 4s of inactivity. Must be less than Fauna's server
// side idle timeout of 5 seconds.
freeSocketTimeout: 4000,
};
let httpAgents;
if (this.clientConfiguration.endpoint.protocol === "http") {
httpAgents = { httpAgent: new Agent(agentSettings) };
} else {
httpAgents = { httpsAgent: new HttpsAgent(agentSettings) };
}
this.client = axios.create({
baseURL: this.clientConfiguration.endpoint.toString(),
timeout: this.clientConfiguration.queryTimeoutMillis + 1000,
timeout,
...httpAgents,
});
this.client.defaults.headers.common[
"Authorization"
Expand Down
26 changes: 26 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,15 @@ acorn@^8.8.0:
resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.8.0.tgz#88c0187620435c7f6015803f5539dae05a9dbea8"
integrity sha512-QOxyigPVrpZ2GXT+PFyZTl6TtOFc5egxHIP9IlQ+RbupQuX4RkT/Bee4/kQuC02Xkzg84JcT7oLYtDIQxp+v7w==

agentkeepalive@^4.2.1:
version "4.2.1"
resolved "https://registry.yarnpkg.com/agentkeepalive/-/agentkeepalive-4.2.1.tgz#a7975cbb9f83b367f06c90cc51ff28fe7d499717"
integrity sha512-Zn4cw2NEqd+9fiSVWMscnjyQ1a8Yfoc5oBajLeo5w+YBHgDUcEBY2hS4YpTz6iN5f/2zQiktcuM6tS8x1p9dpA==
dependencies:
debug "^4.1.0"
depd "^1.1.2"
humanize-ms "^1.2.1"

ajv@^6.10.0, ajv@^6.12.4, ajv@~6.12.6:
version "6.12.6"
resolved "https://registry.yarnpkg.com/ajv/-/ajv-6.12.6.tgz#baf5a62e802b07d977034586f8c3baf5adf26df4"
Expand Down Expand Up @@ -1192,6 +1201,11 @@ delayed-stream@~1.0.0:
resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"
integrity sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==

depd@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/depd/-/depd-1.1.2.tgz#9bcd52e14c097763e749b274c4346ed2e560b5a9"
integrity sha512-7emPTl6Dpo6JRXOXjLRxck+FlLRX5847cLKEn00PLAgc3g2hTZZgr+e4c2v6QpSmLeFP3n5yUo7ft6avBK/5jQ==

detect-newline@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/detect-newline/-/detect-newline-3.1.0.tgz#576f5dfc63ae1a192ff192d8ad3af6308991b651"
Expand Down Expand Up @@ -1670,6 +1684,13 @@ human-signals@^2.1.0:
resolved "https://registry.yarnpkg.com/human-signals/-/human-signals-2.1.0.tgz#dc91fcba42e4d06e4abaed33b3e7a3c02f514ea0"
integrity sha512-B4FFZ6q/T2jhhksgkbEW3HBvWIfDW85snkQgawt07S7J5QXTk6BkNV+0yAeZrM5QpMAdYlocGoljn0sJ/WQkFw==

humanize-ms@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/humanize-ms/-/humanize-ms-1.2.1.tgz#c46e3159a293f6b896da29316d8b6fe8bb79bbed"
integrity sha512-Fl70vYtsAFb/C06PTS9dZBo7ihau+Tu/DNCk/OyHhea07S+aeMWpFFkUaXRa8fI+ScZbEI8dfSxwY7gxZ9SAVQ==
dependencies:
ms "^2.0.0"

husky@^8.0.1:
version "8.0.1"
resolved "https://registry.yarnpkg.com/husky/-/husky-8.0.1.tgz#511cb3e57de3e3190514ae49ed50f6bc3f50b3e9"
Expand Down Expand Up @@ -2346,6 +2367,11 @@ ms@2.1.2:
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==

ms@^2.0.0:
version "2.1.3"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2"
integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==

multimatch@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/multimatch/-/multimatch-4.0.0.tgz#8c3c0f6e3e8449ada0af3dd29efb491a375191b3"
Expand Down

0 comments on commit bc92b3e

Please sign in to comment.