Skip to content

Commit

Permalink
Rename timeout option to idle_timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
porsager committed Apr 6, 2020
1 parent e61e8f4 commit 90865cd
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const sql = postgres('postgres://username:password@host:port/database', {
password : '', // Password of database user
ssl : false, // True, or options for tls.connect
max : 10, // Max number of connections
timeout : 0, // Idle connection timeout in seconds
idle_timeout: 0, // Idle connection timeout in seconds
types : [], // Array of custom types, see more below
onnotice : fn // Defaults to console.log
onparameter : fn // (key, value) when server param change
Expand Down
8 changes: 4 additions & 4 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function Connection(options = {}) {
const {
onparameter,
transform,
timeout,
idle_timeout,
onnotify,
onnotice,
parsers
Expand Down Expand Up @@ -113,7 +113,7 @@ function Connection(options = {}) {
query.args = args
query.result = []
query.result.count = null
timeout && clearTimeout(timer)
idle_timeout && clearTimeout(timer)

typeof options.debug === 'function' && options.debug(id, str, args)
const buffer = query.simple
Expand Down Expand Up @@ -157,7 +157,7 @@ function Connection(options = {}) {

function idle() {
clearTimeout(timer)
timer = setTimeout(socket.end, timeout * 1000)
timer = setTimeout(socket.end, idle_timeout * 1000)
}

function onready(err) {
Expand All @@ -181,7 +181,7 @@ function Connection(options = {}) {
}

backend.query = backend.error = null
timeout && queries.length === 0 && idle()
idle_timeout && queries.length === 0 && idle()

if (!open) {
messages.forEach(socket.write)
Expand Down
7 changes: 6 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ function parseOptions(a, b) {
max : o.max || url.query.max || 10,
types : o.types || {},
ssl : o.ssl || url.ssl || false,
timeout : o.timeout,
idle_timeout: o.idle_timeout || warn(o.timeout, 'The timeout option is deprecated, use idle_timeout instead'),
onnotice : o.onnotice,
onparameter : o.onparameter,
transform : Object.assign({}, o.transform),
Expand All @@ -505,6 +505,11 @@ function parseOptions(a, b) {
)
}

function warn(x, message) {
typeof x !== 'undefined' && console.log(message)
return x
}

function osUsername() {
try {
return require('os').userInfo().username // eslint-disable-line
Expand Down
14 changes: 6 additions & 8 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const options = {
db: 'postgres_js_test',
user: login.user,
pass: login.pass,
timeout: 0.5,
idle_timeout: 0.2,
max: 1
}

Expand Down Expand Up @@ -273,7 +273,7 @@ t('Throw syntax error', async() =>
t('Connect using uri', async() =>
[true, await new Promise((resolve, reject) => {
const sql = postgres('postgres://' + login.user + ':' + (login.pass || '') + '@localhost:5432/' + options.db, {
timeout: 0.1
idle_timeout: options.idle_timeout
})
sql`select 1`.then(() => resolve(true), reject)
})]
Expand All @@ -282,7 +282,7 @@ t('Connect using uri', async() =>
t('Fail with proper error on no host', async() =>
['ECONNREFUSED', (await new Promise((resolve, reject) => {
const sql = postgres('postgres://localhost:33333/' + options.db, {
timeout: 0.1
idle_timeout: options.idle_timeout
})
sql`select 1`.then(reject, resolve)
})).code]
Expand All @@ -292,7 +292,7 @@ t('Connect using SSL', async() =>
[true, (await new Promise((resolve, reject) => {
postgres({
ssl: { rejectUnauthorized: false },
timeout: 0.1
idle_timeout: options.idle_timeout
})`select 1`.then(() => resolve(true), reject)
}))]
)
Expand Down Expand Up @@ -741,9 +741,7 @@ t('notice works', async() => {
notice = x
}

const sql = postgres({
...options
})
const sql = postgres(options)

await sql`create table if not exists users()`
await sql`create table if not exists users()`
Expand Down Expand Up @@ -904,7 +902,7 @@ t('Async stack trace', async() => {
})

t('Debug has long async stack trace', async() => {
const sql = postgres({ debug: true })
const sql = postgres({ ...options, debug: true })

return [
'watyo',
Expand Down

0 comments on commit 90865cd

Please sign in to comment.