Skip to content

Commit

Permalink
feat: allow to httpClient use HTTP2 first (#5332)
Browse files Browse the repository at this point in the history
base on node-modules/urllib#516


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Introduced the ability to enable HTTP/2 support through the `allowH2`
property when using the next-generation HTTP client.
  
- **Bug Fixes**
- Enhanced clarity and accuracy of comments for HTTP client
configuration properties.

- **Improvements**
- Updated `urllib-next` dependency to version `^3.26.0` for better
performance and stability.
- Added tests to verify HTTP/2 functionality and custom HTTP client
behavior.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
fengmk2 authored Jun 27, 2024
1 parent 44950ed commit ceded0b
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 4 deletions.
2 changes: 2 additions & 0 deletions config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ module.exports = appInfo => {
* @property {Number} httpsAgent.maxSockets - https agent max socket number of one host, default is `Number.MAX_SAFE_INTEGER` @ses https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER
* @property {Number} httpsAgent.maxFreeSockets - https agent max free socket number of one host, default is 256.
* @property {Boolean} useHttpClientNext - use urllib@3 HttpClient
* @property {Boolean} allowH2 - Allow to use HTTP2 first, only work on `useHttpClientNext = true`
*/
config.httpclient = {
enableDNSCache: false,
Expand All @@ -326,6 +327,7 @@ module.exports = appInfo => {
maxFreeSockets: 256,
},
useHttpClientNext: false,
// allowH2: false,
};

/**
Expand Down
8 changes: 5 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,16 +296,18 @@ declare module 'egg' {
request?: HttpClientRequestOptions | RequestOptionsOld;
/** Whether enable dns cache */
enableDNSCache?: boolean;
/** Enable proxy request, default is false. */
/** Enable proxy request. Default is `false`. */
enableProxy?: boolean;
/** proxy agent uri or options, default is null. */
/** proxy agent uri or options. Default is `null`. */
proxy?: string | { [key: string]: any };
/** DNS cache lookup interval */
dnsCacheLookupInterval?: number;
/** DNS cache max age */
dnsCacheMaxLength?: number;
/** use urllib@3 HttpClient */
/** use urllib@3 HttpClient. Default is `false` */
useHttpClientNext?: boolean;
/** Allow to use HTTP2 first, only work on `useHttpClientNext = true`. Default is `false` */
allowH2?: boolean;
}

export interface EggAppConfig {
Expand Down
1 change: 1 addition & 0 deletions lib/core/httpclient_next.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class HttpClientNext extends HttpClient {
super({
app,
defaultArgs: config.request,
allowH2: config.allowH2,
});
this.app = app;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"onelogger": "^1.0.0",
"sendmessage": "^2.0.0",
"urllib": "^2.33.0",
"urllib-next": "npm:urllib@^3.22.4",
"urllib-next": "npm:urllib@^3.26.0",
"utility": "^2.1.0",
"ylru": "^1.3.2"
},
Expand Down
21 changes: 21 additions & 0 deletions test/fixtures/apps/httpclient-http2/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

const assert = require('assert');

module.exports = app => {
class CustomHttpClient extends app.HttpClientNext {
request(url, opt) {
return new Promise(resolve => {
assert(/^http/.test(url), 'url should start with http, but got ' + url);
resolve();
}).then(() => {
return super.request(url, opt);
});
}

curl(url, opt) {
return this.request(url, opt);
}
}
app.HttpClientNext = CustomHttpClient;
};
4 changes: 4 additions & 0 deletions test/fixtures/apps/httpclient-http2/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
exports.httpclient = {
useHttpClientNext: true,
allowH2: true,
};
3 changes: 3 additions & 0 deletions test/fixtures/apps/httpclient-http2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "httpclient-overwrite"
}
37 changes: 37 additions & 0 deletions test/lib/core/httpclient.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const assert = require('node:assert');
const { sensitiveHeaders } = require('node:http2');
const mm = require('egg-mock');
const urllib = require('urllib');
const Httpclient = require('../../../lib/core/httpclient');
Expand Down Expand Up @@ -237,6 +238,12 @@ describe('test/lib/core/httpclient.test.js', () => {
});
after(() => app.close());

it('should work', async () => {
const res = await app.httpclient.request(url);
assert.equal(res.status, 200);
assert.equal(res.data.toString(), 'GET /');
});

it('should set request default global timeout to 99ms', () => {
return app.httpclient.curl(`${url}/timeout`)
.catch(err => {
Expand All @@ -255,6 +262,36 @@ describe('test/lib/core/httpclient.test.js', () => {
});
});

describe('overwrite httpclient support allowH2=true', () => {
let app;
before(() => {
app = utils.app('apps/httpclient-http2');
return app.ready();
});
after(() => app.close());

it('should work on http2', async () => {
const res = await app.httpclient.request(url);
assert.equal(res.status, 200);
assert.equal(res.data.toString(), 'GET /');
assert.equal(sensitiveHeaders in res.headers, false);
const res2 = await app.httpclient.request('https://registry.npmmirror.com/urllib/latest', {
dataType: 'json',
});
assert.equal(res2.status, 200);
assert.equal(res2.data.name, 'urllib');
assert.equal(sensitiveHeaders in res2.headers, true);
});

it('should assert url', () => {
return app.httpclient.curl('unknown url')
.catch(err => {
assert(err);
assert(err.message.includes('url should start with http, but got unknown url'));
});
});
});

describe('httpclient tracer', () => {
let app;
before(() => {
Expand Down

0 comments on commit ceded0b

Please sign in to comment.