Skip to content
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

add cache option #566

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class Recorder extends events.EventEmitter {
constructor(config) {
super(config);
this.globalId = 1;
this.fake = false;
this.cachePath = getCacheDir();
this.db = new Datastore();

Expand Down
22 changes: 14 additions & 8 deletions lib/requestHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,12 +415,14 @@ function getUserReqHandler(userRule, recorder) {
req,
startTime: new Date().getTime()
};
resourceInfoId = recorder.appendRecord(resourceInfo);
if (!recorder.fake) {
resourceInfoId = recorder.appendRecord(resourceInfo);
}
}

try {
resourceInfo.reqBody = reqData.toString(); //TODO: deal reqBody in webInterface.js
recorder && recorder.updateRecord(resourceInfoId, resourceInfo);
recorder && !recorder.fake && recorder.updateRecord(resourceInfoId, resourceInfo);
} catch (e) { }
})

Expand Down Expand Up @@ -505,7 +507,7 @@ function getUserReqHandler(userRule, recorder) {

// console.info('===> resbody in record', resourceInfo);

recorder && recorder.updateRecord(resourceInfoId, resourceInfo);
recorder && !recorder.fake && recorder.updateRecord(resourceInfoId, resourceInfo);
})
.catch((e) => {
logUtil.printLog(color.green('Send final response failed:' + e.message), logUtil.T_ERR);
Expand Down Expand Up @@ -619,7 +621,9 @@ function getConnectReqHandler(userRule, recorder, httpsServerMgr) {
req,
startTime: new Date().getTime()
};
resourceInfoId = recorder.appendRecord(resourceInfo);
if (!recorder.fake) {
resourceInfoId = recorder.appendRecord(resourceInfo);
}
}
})
.then(() => {
Expand Down Expand Up @@ -677,7 +681,7 @@ function getConnectReqHandler(userRule, recorder, httpsServerMgr) {
resourceInfo.resBody = '';
resourceInfo.length = 0;

recorder && recorder.updateRecord(resourceInfoId, resourceInfo);
recorder && !recorder.fake && recorder.updateRecord(resourceInfoId, resourceInfo);
}
})
.catch(co.wrap(function *(error) {
Expand Down Expand Up @@ -726,7 +730,9 @@ function getWsHandler(userRule, recorder, wsClient, wsReq) {
req: wsReq,
startTime: new Date().getTime()
});
resourceInfoId = recorder.appendRecord(resourceInfo);
if (!recorder.fake) {
resourceInfoId = recorder.appendRecord(resourceInfo);
}
}

/**
Expand Down Expand Up @@ -794,7 +800,7 @@ function getWsHandler(userRule, recorder, wsClient, wsReq) {
};

// resourceInfo.wsMessages.push(message);
recorder && recorder.updateRecordWsMessage(resourceInfoId, message);
recorder && !recorder.fake && recorder.updateRecordWsMessage(resourceInfoId, message);
};

proxyWs.onopen = () => {
Expand All @@ -815,7 +821,7 @@ function getWsHandler(userRule, recorder, wsClient, wsReq) {
resourceInfo.resBody = '';
resourceInfo.length = resourceInfo.resBody.length;

recorder && recorder.updateRecord(resourceInfoId, resourceInfo);
recorder && !recorder.fake && recorder.updateRecord(resourceInfoId, resourceInfo);
});

proxyWs.onerror = (e) => {
Expand Down
18 changes: 12 additions & 6 deletions proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ class ProxyCore extends events.EventEmitter {

// init recorder
this.recorder = config.recorder;

// init request handler
const RequestHandler = util.freshRequire('./requestHandler');
this.requestHandler = new RequestHandler({
Expand Down Expand Up @@ -293,13 +292,20 @@ class ProxyServer extends ProxyCore {
* @param {object} [config.webInterface] - config of the web interface
* @param {boolean} [config.webInterface.enable=false] - if web interface is enabled
* @param {number} [config.webInterface.webPort=8002] - http port of the web interface
* @param {boolean} [config.cache=false] - never store cache on disk if cache is false
*/
constructor(config) {
// prepare a recorder
const recorder = new Recorder();
const configForCore = Object.assign({
if (!('cache' in config)) {
config.cache = true;
}
const recorder = config.cache ? new Recorder() : { fake: true };
Copy link
Member

Choose a reason for hiding this comment

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

试试这种模式:

config.recorderDiskCache  ? new Recorder() : new Recorder({diskCache: false});

if (!config.cache && config.webInterface) {
config.webInterface.enable = false;
}
const configForCore = Object.assign(config, {
recorder,
}, config);
});

super(configForCore);

Expand All @@ -309,7 +315,7 @@ class ProxyServer extends ProxyCore {
}

start() {
if (this.recorder) {
if (this.recorder && !this.recorder.fake) {
this.recorder.setDbAutoCompact();
}

Expand All @@ -333,7 +339,7 @@ class ProxyServer extends ProxyCore {
close() {
const self = this;
// release recorder
if (self.recorder) {
if (self.recorder && !self.recorder.fake) {
self.recorder.stopDbAutoCompact();
self.recorder.clear();
}
Expand Down