Skip to content

Commit 2a82c1c

Browse files
author
mjgp2
committed
Using hostaddr will cache the DNS lookup
"If host is specified without hostaddr, a host name lookup occurs." https://www.postgresql.org/docs/8.1/libpq.html#LIBPQ-CONNECT
1 parent f5e87ac commit 2a82c1c

File tree

2 files changed

+2
-52
lines changed

2 files changed

+2
-52
lines changed

packages/pg/lib/connection-parameters.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,7 @@ class ConnectionParameters {
155155
if (this.client_encoding) {
156156
params.push('client_encoding=' + quoteParamValue(this.client_encoding))
157157
}
158-
dns.lookup(this.host, function (err, address) {
159-
if (err) return cb(err, null)
160-
params.push('hostaddr=' + quoteParamValue(address))
161-
return cb(null, params.join(' '))
162-
})
158+
return cb(null, params.join(' '))
163159
}
164160
}
165161

packages/pg/test/unit/connection-parameters/creation-tests.js

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ const helper = require('../test-helper')
33
const assert = require('assert')
44
const ConnectionParameters = require('../../../lib/connection-parameters')
55
const defaults = require('../../../lib').defaults
6-
const dns = require('dns')
76

87
// clear process.env
98
for (var key in process.env) {
@@ -152,14 +151,6 @@ var checkForPart = function (array, part) {
152151
assert.ok(array.indexOf(part) > -1, array.join(' ') + ' did not contain ' + part)
153152
}
154153

155-
const getDNSHost = async function (host) {
156-
return new Promise((resolve, reject) => {
157-
dns.lookup(host, (err, addresses) => {
158-
err ? reject(err) : resolve(addresses)
159-
})
160-
})
161-
}
162-
163154
suite.testAsync('builds simple string', async function () {
164155
var config = {
165156
user: 'brian',
@@ -169,57 +160,20 @@ suite.testAsync('builds simple string', async function () {
169160
database: 'bam',
170161
}
171162
var subject = new ConnectionParameters(config)
172-
const dnsHost = await getDNSHost(config.host)
173163
return new Promise((resolve) => {
174164
subject.getLibpqConnectionString(function (err, constring) {
175165
assert(!err)
176166
var parts = constring.split(' ')
177167
checkForPart(parts, "user='brian'")
178168
checkForPart(parts, "password='xyz'")
179169
checkForPart(parts, "port='888'")
180-
checkForPart(parts, `hostaddr='${dnsHost}'`)
170+
checkForPart(parts, "host='localhost'")
181171
checkForPart(parts, "dbname='bam'")
182172
resolve()
183173
})
184174
})
185175
})
186176

187-
suite.test('builds dns string', async function () {
188-
var config = {
189-
user: 'brian',
190-
password: 'asdf',
191-
port: 5432,
192-
host: 'localhost',
193-
}
194-
var subject = new ConnectionParameters(config)
195-
const dnsHost = await getDNSHost(config.host)
196-
return new Promise((resolve) => {
197-
subject.getLibpqConnectionString(function (err, constring) {
198-
assert(!err)
199-
var parts = constring.split(' ')
200-
checkForPart(parts, "user='brian'")
201-
checkForPart(parts, `hostaddr='${dnsHost}'`)
202-
resolve()
203-
})
204-
})
205-
})
206-
207-
suite.test('error when dns fails', function () {
208-
var config = {
209-
user: 'brian',
210-
password: 'asf',
211-
port: 5432,
212-
host: 'asdlfkjasldfkksfd#!$!!!!..com',
213-
}
214-
var subject = new ConnectionParameters(config)
215-
subject.getLibpqConnectionString(
216-
assert.calls(function (err, constring) {
217-
assert.ok(err)
218-
assert.isNull(constring)
219-
})
220-
)
221-
})
222-
223177
suite.test('connecting to unix domain socket', function () {
224178
var config = {
225179
user: 'brian',

0 commit comments

Comments
 (0)