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

Oss 953 consolidate query timeout for v5 #549

Merged
merged 10 commits into from
Oct 18, 2021
17 changes: 2 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,21 +172,9 @@ more information on the pagination helper.

#### Timeouts

The client can be configured to handle timeouts in two different ways:
The client can be configured to handle timeout by setting a `queryTimeout` on the client (or passing the value to the client's `.query()` method directly)

1. Add a `timeout` field to the `options` block when instantiating the client
2. By setting a `queryTimeout` on the client (or passing the value to the client's `.query()` method directly)

The first option (i.e. `timeout`) represents a HTTP timeout on the client side. Defined in seconds, the client will wait the specified period before timing out if it has yet to receive a response.

```javascript
const client = new faunadb.Client({
secret: 'YOUR_FAUNADB_SECRET',
timeout: 100,
})
```

On the other hand, using the client's `queryTimeout` dictates how long FaunaDB will process the request on the server before timing out if it hasn't finished running the operation. This can be done in two different ways:
Using the client's `queryTimeout` dictates how long FaunaDB will process the request on the server before timing out if it hasn't finished running the operation. This can be done in two different ways:

```javascript
// 1. Setting the value when instantiating a new client
Expand Down Expand Up @@ -280,7 +268,6 @@ async function main() {
main().catch(console.error)
```


## Known issues

### Using with Cloudflare Workers
Expand Down
2 changes: 0 additions & 2 deletions src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ var notifyAboutNewVersion = util.notifyAboutNewVersion()
* @param {?number} options.port
* Port of the FaunaDB server.
* @param {?string} options.secret FaunaDB secret (see [Reference Documentation](https://app.fauna.com/documentation/intro/security))
* @param {?number} options.timeout Read timeout in seconds.
* @param {?Client~observerCallback} options.observer
* Callback that will be called after every completed request.
* @param {?boolean} options.keepAlive
Expand All @@ -176,7 +175,6 @@ function Client(options) {
scheme: 'https',
port: null,
secret: null,
timeout: 60,
observer: null,
keepAlive: true,
headers: {},
Expand Down
6 changes: 3 additions & 3 deletions src/_http/fetchAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function FetchAdapter(options) {
* @param {?string} options.body Request body utf8 string.
* @params {?object} options.streamConsumer Stream consumer.
* @param {?object} options.signal Abort signal object.
* @param {?number} options.timeout Request timeout.
* @param {?number} options.queryTimeout Request timeout.
* @returns {Promise} Request result.
*/
FetchAdapter.prototype.execute = function(options) {
Expand All @@ -78,7 +78,7 @@ FetchAdapter.prototype.execute = function(options) {
var timerId = null
var isStreaming = options.streamConsumer != null
// Use timeout only if no signal provided
var useTimeout = !options.signal && !!options.timeout
var useTimeout = !options.signal && !!options.queryTimeout
var ctrl = new AbortController()
var pendingRequest = {
isStreaming: isStreaming,
Expand Down Expand Up @@ -163,7 +163,7 @@ FetchAdapter.prototype.execute = function(options) {
timerId = setTimeout(function() {
timerId = null
ctrl.abort()
}, options.timeout)
}, options.queryTimeout)
}

if (options.signal) {
Expand Down
6 changes: 3 additions & 3 deletions src/_http/http2Adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ Http2Adapter.prototype._cleanupSessionFor = function(origin, isStreaming) {
* @param {?string} options.body Request body utf8 string.
* @params {?object} options.streamConsumer Stream consumer.
* @param {?object} options.signal Abort signal object.
* @param {?number} options.timeout Request timeout.
* @param {?number} options.queryTimeout Request timeout.
* @returns {Promise} Request result.
*/
Http2Adapter.prototype.execute = function(options) {
Expand Down Expand Up @@ -305,8 +305,8 @@ Http2Adapter.prototype.execute = function(options) {
sessionInterface.onRequestStart()

// Set up timeout only if no signal provided.
if (!options.signal && options.timeout) {
request.setTimeout(options.timeout, onTimeout)
if (!options.signal && options.queryTimeout) {
request.setTimeout(options.queryTimeout, onTimeout)
}

if (options.signal) {
Expand Down
5 changes: 2 additions & 3 deletions src/_http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ function HttpClient(options) {
this._baseUrl = options.scheme + '://' + options.domain + ':' + options.port
this._secret = options.secret
this._headers = Object.assign({}, options.headers, getDefaultHeaders())
this._queryTimeout = options.queryTimeout
this._lastSeen = null
this._timeout = Math.floor(options.timeout * 1000)
this._queryTimeout = options.queryTimeout
}

/**
Expand Down Expand Up @@ -120,7 +119,7 @@ HttpClient.prototype.execute = function(options) {
headers: util.removeNullAndUndefinedValues(headers),
body: options.body,
signal: options.signal,
timeout: this._timeout,
queryTimeout: this._queryTimeout,
streamConsumer: options.streamConsumer,
})
}
Expand Down
1 change: 0 additions & 1 deletion src/types/Client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export interface ClientConfig {
domain?: string
scheme?: 'http' | 'https'
port?: number
timeout?: number
queryTimeout?: number
observer?: <T extends object = object>(
res: RequestResult<T | errors.FaunaHTTPError>,
Expand Down
2 changes: 1 addition & 1 deletion test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ describe('Client', () => {
const customTimeout = 3
const mockedFetch = mockFetch({}, true)
const clientWithTimeout = new Client({
timeout: customTimeout,
queryTimeout: customTimeout,
fetch: mockedFetch,
})

Expand Down