Skip to content

Commit

Permalink
fix: cfg.timeout option to explicitly enable pTimeout on getByIds
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Jan 11, 2022
1 parent 04b3442 commit 6a75dc2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/datastore.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
JsonSchemaRootObject,
CommonLogger,
commonLoggerMinLevel,
pTimeout,
} from '@naturalcycles/js-lib'
import { ReadableTyped } from '@naturalcycles/nodejs-lib'
import { boldWhite } from '@naturalcycles/nodejs-lib/dist/colors'
Expand Down Expand Up @@ -116,7 +117,15 @@ export class DatastoreDB extends BaseCommonDB implements CommonDB {
let rows: any[]

try {
rows = (await this.ds().get(keys))[0]
if (this.cfg.timeout) {
const r = await pTimeout(this.ds().get(keys), {
timeout: this.cfg.timeout,
name: `datastore.getByIds(${table})`,
})
rows = r[0]
} else {
rows = (await this.ds().get(keys))[0]
}
} catch (err) {
this.cfg.logger.log('datastore recreated on error')

Expand Down
8 changes: 8 additions & 0 deletions src/datastore.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ export interface DatastoreDBCfg extends DatastoreOptions {
* Default to `console`
*/
logger?: CommonLogger

/**
* Experimental option, currently only applies to `getByIds`.
* Applies pTimeout to Datastore operation, re-creates Datastore on any error.
*
* @experimental
*/
timeout?: number
}

export interface DatastoreCredentials {
Expand Down

0 comments on commit 6a75dc2

Please sign in to comment.