Skip to content

refactor(dnshttpclient): use async function instead of Promise#2774

Merged
popomore merged 9 commits into
masterfrom
dnshttpclient
Aug 24, 2018
Merged

refactor(dnshttpclient): use async function instead of Promise#2774
popomore merged 9 commits into
masterfrom
dnshttpclient

Conversation

@popomore
Copy link
Copy Markdown
Member

@popomore popomore commented Jul 6, 2018

Checklist
  • npm test passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)
Description of change

and deprecate callback

@codecov
Copy link
Copy Markdown

codecov Bot commented Jul 6, 2018

Codecov Report

Merging #2774 into master will increase coverage by 0.36%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2774      +/-   ##
==========================================
+ Coverage   99.39%   99.75%   +0.36%     
==========================================
  Files          29       29              
  Lines         824      825       +1     
==========================================
+ Hits          819      823       +4     
+ Misses          5        2       -3
Impacted Files Coverage Δ
config/config.default.js 100% <ø> (ø) ⬆️
lib/core/dnscache_httpclient.js 100% <100%> (+3.17%) ⬆️
app/extend/request.js 100% <0%> (+1.17%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fe9e956...9345ad6. Read the comment docs.

@popomore popomore requested a review from fengmk2 July 6, 2018 12:49
@fengmk2
Copy link
Copy Markdown
Member

fengmk2 commented Jul 12, 2018

rebase 一下

Comment thread lib/core/dnscache_httpclient.js Outdated
.then(result => {
return super.request(result.url, result.args);
})
.then(result => callback(null, result))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果 callback 执行抛错,又会进入到下面的 catch 导致二次执行,需要把下面的 catch 放到 then 的第二个参数中

Comment thread lib/core/dnscache_httpclient.js Outdated

curl(url, args, callback) {
if (callback) {
this.app.deprecate('[dnscache_httpclient] Please don\'t use callback when `curl()`');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里会输出两条 message 吧,统一在 request() 中提示好了?

// make sure not callback twice
callback = null;
if (record && now - record.timestamp < this.dnsCacheLookupInterval) {
return { url: formatDnsLookupUrl(hostname, url, record.ip), args };
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

record.timestamp = now; 的逻辑不需要了?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

原来的逻辑看起来是直接响应,然后异步更新的,是否就改成同步等待?

如果要保留异步实现的话,可能要用到 runInBackground @fengmk2

@popomore
Copy link
Copy Markdown
Member Author

@dead-horse @fengmk2 看看

.then(result => {
return super.request(result.url, result.args);
})
.then(result => process.nextTick(() => callback(null, result.data, result.res)))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不需要加 nextTick 吧,Promise 应该本来就是异步了,可以直接:

.then(result => callback(null, result.data, result.res), callback)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯,不需要加,Promise 一定是异步的。

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

主要为了不在 promise 链上

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

但是这样又多等来一个 tick

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

明白了,这是 callback。没问题。

if (needUpdate) {
if (now - record.timestamp >= this.dnsCacheLookupInterval) {
// make sure next request don't refresh dns query
record.timestamp = now;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里没覆盖到

Comment thread config/config.default.js
*/
config.httpclient = {
enableDNSCache: false,
dnsCacheLookupInterval: 10000,
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fengmk2 是不是 bug?之前没看到这个配置

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

呃,之前使用的是 dnsCacheMaxAge 配置。。。

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

配了没用。。。

@popomore
Copy link
Copy Markdown
Member Author

@fengmk2 @dead-horse 再看看?

@fengmk2
Copy link
Copy Markdown
Member

fengmk2 commented Aug 8, 2018

@popomore rebase 再跑一遍测试,然后合并。

@popomore
Copy link
Copy Markdown
Member Author

@fengmk2 过了

@popomore popomore merged commit 80528cc into master Aug 24, 2018
@popomore popomore deleted the dnshttpclient branch August 24, 2018 06:50
@popomore
Copy link
Copy Markdown
Member Author

@dead-horse @fengmk2 没发布前再看看,有问题我再改下

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants