Skip to content

Using hostaddr will cache the DNS lookup #2740

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions packages/pg/lib/connection-parameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,7 @@ class ConnectionParameters {
if (this.client_encoding) {
params.push('client_encoding=' + quoteParamValue(this.client_encoding))
}
dns.lookup(this.host, function (err, address) {
if (err) return cb(err, null)
params.push('hostaddr=' + quoteParamValue(address))
return cb(null, params.join(' '))
})
return cb(null, params.join(' '))
}
}

Expand Down
48 changes: 1 addition & 47 deletions packages/pg/test/unit/connection-parameters/creation-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const helper = require('../test-helper')
const assert = require('assert')
const ConnectionParameters = require('../../../lib/connection-parameters')
const defaults = require('../../../lib').defaults
const dns = require('dns')

// clear process.env
for (var key in process.env) {
Expand Down Expand Up @@ -155,14 +154,6 @@ var checkForPart = function (array, part) {
assert.ok(array.indexOf(part) > -1, array.join(' ') + ' did not contain ' + part)
}

const getDNSHost = async function (host) {
return new Promise((resolve, reject) => {
dns.lookup(host, (err, addresses) => {
err ? reject(err) : resolve(addresses)
})
})
}

suite.testAsync('builds simple string', async function () {
var config = {
user: 'brian',
Expand All @@ -172,57 +163,20 @@ suite.testAsync('builds simple string', async function () {
database: 'bam',
}
var subject = new ConnectionParameters(config)
const dnsHost = await getDNSHost(config.host)
return new Promise((resolve) => {
subject.getLibpqConnectionString(function (err, constring) {
assert(!err)
var parts = constring.split(' ')
checkForPart(parts, "user='brian'")
checkForPart(parts, "password='xyz'")
checkForPart(parts, "port='888'")
checkForPart(parts, `hostaddr='${dnsHost}'`)
checkForPart(parts, "host='localhost'")
checkForPart(parts, "dbname='bam'")
resolve()
})
})
})

suite.test('builds dns string', async function () {
var config = {
user: 'brian',
password: 'asdf',
port: 5432,
host: 'localhost',
}
var subject = new ConnectionParameters(config)
const dnsHost = await getDNSHost(config.host)
return new Promise((resolve) => {
subject.getLibpqConnectionString(function (err, constring) {
assert(!err)
var parts = constring.split(' ')
checkForPart(parts, "user='brian'")
checkForPart(parts, `hostaddr='${dnsHost}'`)
resolve()
})
})
})

suite.test('error when dns fails', function () {
var config = {
user: 'brian',
password: 'asf',
port: 5432,
host: 'asdlfkjasldfkksfd#!$!!!!..com',
}
var subject = new ConnectionParameters(config)
subject.getLibpqConnectionString(
assert.calls(function (err, constring) {
assert.ok(err)
assert.isNull(constring)
})
)
})

suite.test('connecting to unix domain socket', function () {
var config = {
user: 'brian',
Expand Down