-
-
Notifications
You must be signed in to change notification settings - Fork 689
feat(interceptors/dns): cache option #4565
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -5,14 +5,120 @@ const DecoratorHandler = require('../handler/decorator-handler') | |||
| const { InvalidArgumentError, InformationalError } = require('../core/errors') | ||||
| const maxInt = Math.pow(2, 31) - 1 | ||||
|
|
||||
| class DNSInstance { | ||||
| export class DNSCache { | ||||
| #maxTTL = 0 | ||||
| #maxItems = 0 | ||||
| #records = new Map() | ||||
|
|
||||
| constructor (opts) { | ||||
| this.#maxTTL = opts.maxTTL | ||||
| this.#maxItems = opts.maxItems | ||||
| } | ||||
|
|
||||
| get size () { | ||||
| return this.#records.size | ||||
| } | ||||
|
|
||||
| // TODO: it will require to write adapter for different caches, look for a better ideas | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shall we remove the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before changes, can you please help me to choose between DNSStorage implementations:
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'd prefer this one, is simpler and with the example you shared can be enough for implementers to adapt it to their needs
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thans, #4589 is ready to review! |
||||
| get full () { | ||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incompatible with js Map interface |
||||
| return this.size === this.#maxItems | ||||
| } | ||||
|
|
||||
| get (hostname, opts = {}) { | ||||
| if (!this.#records.has(hostname)) { | ||||
| return null | ||||
| } | ||||
|
|
||||
| const records = this.#records.get(hostname) | ||||
|
|
||||
| if (records == null) { | ||||
| return null | ||||
| } | ||||
|
|
||||
| if (records.records[4] != null) { | ||||
| const ips = records.records[4].ips | ||||
|
|
||||
| // We delete expired records before returning cached records | ||||
| records.records[4].ips = ips.filter(ip => { | ||||
| return Date.now() - ip.timestamp <= ip.ttl | ||||
| }) | ||||
|
|
||||
| if (records.records[4].ips.length === 0) { | ||||
| records.records[4] = null | ||||
| } | ||||
| } | ||||
|
|
||||
| // TODO: deduplicate logic | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||||
| if (records.records[6] != null) { | ||||
| const ips = records.records[6].ips | ||||
|
|
||||
| // We delete expired records before returning cached records | ||||
| records.records[6].ips = ips.filter(ip => { | ||||
| return Date.now() - ip.timestamp <= ip.ttl | ||||
| }) | ||||
|
|
||||
| if (records.records[6].ips.length === 0) { | ||||
| records.records[6] = null | ||||
| } | ||||
| } | ||||
|
|
||||
| return records | ||||
| } | ||||
|
|
||||
| set (hostname, records, opts = {}) { | ||||
| const timestamp = Date.now() | ||||
|
|
||||
| if (records == null) { | ||||
| return | ||||
| } | ||||
|
|
||||
| if (records.records[4] != null) { | ||||
| const ips = records.records[4].ips | ||||
|
|
||||
| for (const ip of ips) { | ||||
| ip.timestamp = timestamp | ||||
|
|
||||
| if (typeof ip.ttl === 'number') { | ||||
| // The record TTL is expected to be in ms | ||||
| ip.ttl = Math.min(ip.ttl, this.#maxTTL) | ||||
| } else { | ||||
| ip.ttl = this.#maxTTL | ||||
| } | ||||
| } | ||||
| } | ||||
|
|
||||
| // TODO: deduplicate logic | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shall we abstract it then? |
||||
| if (records.records[6] != null) { | ||||
| const ips = records.records[6].ips | ||||
|
|
||||
| for (const ip of ips) { | ||||
| ip.timestamp = timestamp | ||||
|
|
||||
| if (typeof ip.ttl === 'number') { | ||||
| // The record TTL is expected to be in ms | ||||
| ip.ttl = Math.min(ip.ttl, this.#maxTTL) | ||||
| } else { | ||||
| ip.ttl = this.#maxTTL | ||||
| } | ||||
| } | ||||
| } | ||||
|
|
||||
| this.#records.set(hostname, records) | ||||
| } | ||||
|
|
||||
| delete (hostname) { | ||||
| this.#records.delete(hostname) | ||||
| } | ||||
| } | ||||
|
|
||||
| class DNSInstance { | ||||
| #maxTTL = 0 | ||||
| #maxItems = 0 | ||||
| dualStack = true | ||||
| affinity = null | ||||
| lookup = null | ||||
| pick = null | ||||
| cache = null | ||||
|
||||
| cache = null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As @Uzlopak suggestion 😛