-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support customize dns lookup function (#723)
Since Redis cluster doesn't support hostname at all (redis/redis#2410), it's reasonable to resolve the hostnames to IPs before connecting.
- Loading branch information
Showing
14 changed files
with
286 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export type CallbackFunction<T = void> = (err?: Error | null, result?: T) => void | ||
export type CallbackFunction<T = void> = (err?: NodeJS.ErrnoException | null, result?: T) => void |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
describe('cluster:dnsLookup', () => { | ||
it('resolve hostnames to IPs', (done) => { | ||
const slotTable = [ | ||
[0, 1000, ['127.0.0.1', 30001]], | ||
[1001, 16383, ['127.0.0.1', 30002]] | ||
] | ||
new MockServer(30001, (argv, c) => { | ||
}, slotTable) | ||
new MockServer(30002, (argv, c) => { | ||
}, slotTable) | ||
|
||
const cluster = new Redis.Cluster([ | ||
{ host: 'localhost', port: '30001' } | ||
]) | ||
cluster.on('ready', () => { | ||
const nodes = cluster.nodes('master') | ||
expect(nodes.length).to.eql(2) | ||
expect(nodes[0].options.host).to.eql('127.0.0.1') | ||
expect(nodes[1].options.host).to.eql('127.0.0.1') | ||
cluster.disconnect() | ||
done() | ||
}) | ||
}) | ||
|
||
it('support customize dnsLookup function', (done) => { | ||
let dnsLookupCalledCount = 0 | ||
const slotTable = [ | ||
[0, 1000, ['127.0.0.1', 30001]], | ||
[1001, 16383, ['127.0.0.1', 30002]] | ||
] | ||
new MockServer(30001, (argv, c) => { | ||
}, slotTable) | ||
new MockServer(30002, (argv, c) => { | ||
}, slotTable) | ||
|
||
const cluster = new Redis.Cluster([ | ||
{ host: 'a.com', port: '30001' } | ||
], { | ||
dnsLookup (hostname, callback) { | ||
dnsLookupCalledCount += 1 | ||
if (hostname === 'a.com') { | ||
callback(null, '127.0.0.1') | ||
} else { | ||
callback(new Error('Unknown hostname')) | ||
} | ||
} | ||
}) | ||
cluster.on('ready', () => { | ||
const nodes = cluster.nodes('master') | ||
expect(nodes.length).to.eql(2) | ||
expect(nodes[0].options.host).to.eql('127.0.0.1') | ||
expect(nodes[1].options.host).to.eql('127.0.0.1') | ||
expect(dnsLookupCalledCount).to.eql(1) | ||
cluster.disconnect() | ||
done() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.