Skip to content

Commit

Permalink
Oss 953 consolidate query timeout for v5 (#549)
Browse files Browse the repository at this point in the history
* tests(OSS-92): remove resources after all test completed (#527)

* tests(OSS-92): remove resources after all test completed

* tests: posttest

* posttest

* posttest

* posttest

* fix: ContainsStr FQL to string (#537)

* fix: ContainsStr FQL to string

* test

* fix

* move test

* OSS-906 fix: restore _isFaunaValue and _isFaunaRef properties (#529)

* fix: restore _isFaunaValue and _isFaunaRef properties

* test: added tests for values type-helpers

* Add toFQL() to type declaration for Expr (#544)

I wanted to print a query as FQL for debugging purposes, but didn't find the toFQL method because it wasn't in the type declaration. A helpful person on StackOverflow pointed it out to me. https://stackoverflow.com/questions/69418846/how-can-i-print-an-fql-query-from-javascript-as-a-string-e-g-to-test-it-in-the/69472058

* fix tests clean (#545)

* TGB-921 page helper (#543)

* TGB-921 page helper

* remove page.data.forEach as it's deprecated

* fix test

* fix test

* debug auth0 tests (#546)

* slack notification (#547)

* OSS-953 Consolidate query timeout for v5

Co-authored-by: Paul Paterson <ptpaterson@gmail.com>
Co-authored-by: Kevin <89287578+ktint@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 18, 2021
1 parent 7fd7231 commit c64cf55
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 69 deletions.
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
20 changes: 20 additions & 0 deletions concourse/pipeline.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
---
resource_types:
- name: slack-notification
type: docker-image
source:
repository: cfcommunity/slack-notification-resource

resources:
- name: notify
type: slack-notification
source:
url: ((slack-webhook))

- name: fauna-js-repository
type: git
icon: github
Expand Down Expand Up @@ -39,6 +50,15 @@ jobs:
file: fauna-js-repository/concourse/tasks/browserify-and-npm-publish.yml
params:
NPM_TOKEN: ((npm_token))
on_success:
put: notify
params:
text_file: slack-message/publish
on_failure:
put: notify
params:
text_file: slack-message/publish


- task: publish-docs
file: fauna-js-repository/concourse/tasks/publish-docs.yml
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
12 changes: 1 addition & 11 deletions src/PageHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,7 @@ PageHelper.prototype._adjustCursors = function(page) {
PageHelper.prototype._consumePages = function(lambda, reverse) {
var self = this
return function(page) {
var data = []
page.data.forEach(function(item) {
if (item.document) {
item.instance = item.document
}
if (item.value && item.value.document) {
item.value.instance = item.value.document
}
data.push(item)
})
lambda(data)
lambda(page.data)

var nextCursor
if (reverse) {
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
69 changes: 42 additions & 27 deletions test/afterComplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const query = require('../src/query')
const Client = require('../src/Client')
const util = require('../src/_util')
const testConfig = require('./config')
const errors = require('../src/errors')

function takeObjectKeys(object) {
var out = {}
Expand All @@ -20,32 +21,46 @@ const rootClient = new Client(
)
)
)
rootClient.query(
query.Let(
{
rootKey: query.KeyFromSecret(testConfig.auth),
keys: query.Map(query.Paginate(query.Keys()), ref => query.Get(ref)),
allKeysExceptRoot: query.Map(
query.Filter(query.Var('keys'), key =>
query.Not(
query.Equals(
query.Select(['ref'], key, null),
query.Select(['ref'], query.Var('rootKey'))
)
)
),
key => query.Select(['ref'], key)
),
refsToRemove: query.Union(
query.Select(['data'], query.Paginate(query.Databases())),
query.Select(['data'], query.Paginate(query.Collections())),
query.Select(['data'], query.Paginate(query.Indexes())),
query.Select(['data'], query.Paginate(query.Functions())),
query.Select(['data'], query.Var('allKeysExceptRoot'))
),
},
query.Foreach(query.Var('refsToRemove'), ref =>
query.If(query.Exists(ref), query.Delete(ref), '')

rootClient
.query(query.KeyFromSecret(testConfig.auth))
.catch(error => {
if (error instanceof errors.BadRequest) {
return null
}
throw error
})
.then(rootKey =>
rootClient.query(
query.Let(
{
rootKey,
keys: query.Map(query.Paginate(query.Keys()), ref => query.Get(ref)),
allKeysExceptRoot: query.Map(
rootKey
? query.Filter(query.Var('keys'), key =>
query.Not(
query.Equals(
query.Select(['ref'], key, null),
query.Select(['ref'], query.Var('rootKey'))
)
)
)
: query.Var('keys'),
key => query.Select(['ref'], key)
),
refsToRemove: query.Union(
query.Select(['data'], query.Paginate(query.Databases())),
query.Select(['data'], query.Paginate(query.Collections())),
query.Select(['data'], query.Paginate(query.Indexes())),
query.Select(['data'], query.Paginate(query.Functions())),
query.Select(['data'], query.Var('allKeysExceptRoot'))
),
},
query.Foreach(query.Var('refsToRemove'), ref =>
query.If(query.Exists(ref), query.Delete(ref), '')
)
)
)
)
)
.catch(console.error)
2 changes: 1 addition & 1 deletion test/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('auth', () => {
.then(resp => resp.json())
.then(data => data.access_token)
}
const providerName = util.randomString('provider_')
const providerName = util.randomString('js_driver_')
const roleOneName = util.randomString('role_one_')

let resource
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
2 changes: 0 additions & 2 deletions test/page.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ describe('page', () => {
expect('ts' in item).toBeTruthy()
expect('action' in item).toBeTruthy()
expect('document' in item).toBeTruthy()
expect('instance' in item).toBeTruthy()
expect('data' in item).toBeTruthy()
})
})
Expand Down Expand Up @@ -293,7 +292,6 @@ describe('page', () => {
expect('ts' in value).toBeTruthy()
expect('action' in value).toBeTruthy()
expect('document' in value).toBeTruthy()
expect('instance' in value).toBeTruthy()
expect('data' in value).toBeTruthy()
})
})
Expand Down

0 comments on commit c64cf55

Please sign in to comment.