Skip to content

Commit

Permalink
Add support for nameserver redirect without tenant database name
Browse files Browse the repository at this point in the history
  • Loading branch information
IanMcCurdy committed Feb 9, 2021
1 parent 4ae917b commit 5bc7b94
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 19 deletions.
58 changes: 40 additions & 18 deletions lib/Client.js
Expand Up @@ -130,8 +130,26 @@ Client.prototype.connect = function connect(options, cb) {

var self = this;

function done(err) {
if (!err) {
function done(err, reply) {
if (err) {
var dbi = reply && reply['dbConnectInfo'];
if (dbi) {
// Abandon connection and reconnect to provided host/port
var host, port;
dbi.forEach(function(option) {
if (option['name'] === protocol.common.DbConnectInfoOption.HOST) {
host = option['value'];
} else if (option['name'] === protocol.common.DbConnectInfoOption.PORT) {
port = option['value'];
}
});
connectOptions['host'] = host;
connectOptions['port'] = port;
self._connection._closeSilently();
doConnect();
return;
}
} else {
self.emit('connect');
}
if (util.isFunction(cb)) {
Expand All @@ -147,24 +165,28 @@ Client.prototype.connect = function connect(options, cb) {
self._connection.connect(connectOptions, done);
}

if (this._connection.readyState === 'new') {
connManager.openConnection(this._connection, onopen);
} else if (this._connection.readyState === 'closed') {
this._connection = this._createConnection(this._settings);
connManager.openConnection(this._connection, onopen);
} else if (this._connection.readyState === 'disconnected') {
this._connection.connect(connectOptions, done);
} else {
if (util.isFunction(cb)) {
util.setImmediate(function deferError() {
var msg = util.format('Cannot connect in state "%s"', self.readyState);
var err = new Error(msg);
err.code = 'EHDBCONNECT';
cb(err);
});
function doConnect() {
if (self._connection.readyState === 'new') {
connManager.openConnection(self._connection, onopen);
} else if (self._connection.readyState === 'closed') {
self._connection = self._createConnection(self._settings);
connManager.openConnection(self._connection, onopen);
} else if (self._connection.readyState === 'disconnected') {
self._connection.connect(connectOptions, done);
} else {
if (util.isFunction(cb)) {
util.setImmediate(function deferError() {
var msg = util.format('Cannot connect in state "%s"', self.readyState);
var err = new Error(msg);
err.code = 'EHDBCONNECT';
cb(err);
});
}
}
return self;
}
return this;

doConnect();
};

Client.prototype.disconnect = function disconnect(cb) {
Expand Down
2 changes: 1 addition & 1 deletion lib/protocol/Connection.js
Expand Up @@ -421,7 +421,7 @@ Connection.prototype.connect = function connect(options, cb) {

function authReceive(err, reply) {
if (err) {
return cb(err);
return cb(err, reply);
}
manager.initialize(reply.authentication, function(err) {
if (err) return cb(err);
Expand Down
75 changes: 75 additions & 0 deletions test/hdb.Client.js
Expand Up @@ -803,5 +803,80 @@ describe('hdb', function () {

});

it('should connect to specified host upon nameserver redirect', function (done) {
var client = new lib.Client({ host: 'localhost', port: 30013, user: 'testuser', password: 'secret'});

var connOpenCount = 0;

var systemDbConnOpened = false;
var systemDbConnClosed = false;
var tenantDbConnOpened = false;

client._connection.open = function (options, cb) {
++connOpenCount;
if (connOpenCount === 1) {
options.host.should.equal('localhost');
options.port.should.equal(30013);
systemDbConnOpened = true;
cb();
} else if (connOpenCount === 2) {
options.host.should.equal('127.0.0.1');
options.port.should.equal(30041);
tenantDbConnOpened = true;
cb();
} else {
cb(new Error('Test error. Open method called on the connection ' + connOpenCount + ' times.'));
}
};

client._connection._createAuthenticationManager = function(options) {
return mock.createManager({});
};

var sendCount = 0;
var reply1 = {
dbConnectInfo: [ { name: 4, type: 28, value: false },
{ name: 2, type: 29, value: '127.0.0.1' },
{ name: 3, type: 3, value: 30041 } ]
};
var reply2 = {
kind: 2,
functionCode: 0,
resultSets: [],
authentication: 'INITIAL'
};
var reply3 = {
kind: 2,
functionCode: 0,
resultSets: [],
authentication: 'FINAL',
connectOptions: []
};

client._connection.send = function (data, cb) {
++sendCount;
if (sendCount == 1) {
cb(new Error(), reply1);
} else if (sendCount == 2) {
cb(undefined, reply2);
} else if (sendCount == 3) {
cb(undefined, reply3);
}
};

client._connection._closeSilently = function () {
connOpenCount.should.equal(1);
systemDbConnClosed = true;
};

client.connect(function (err) {
systemDbConnOpened.should.equal(true);
systemDbConnClosed.should.equal(true);
tenantDbConnOpened.should.equal(true);
done(err);
});

});

});
});

0 comments on commit 5bc7b94

Please sign in to comment.