mirror of
https://github.com/alibaba/anyproxy.git
synced 2025-04-24 16:51:29 +00:00
fix #508
This commit is contained in:
parent
65f530047a
commit
d9f2e7528c
1
.gitignore
vendored
1
.gitignore
vendored
@ -36,3 +36,4 @@ doc_compiled.md
|
|||||||
docs-md/cn/doc.md
|
docs-md/cn/doc.md
|
||||||
docs-md/en/doc.md
|
docs-md/en/doc.md
|
||||||
docs/gitbook/gitbook-plugin-livereload/
|
docs/gitbook/gitbook-plugin-livereload/
|
||||||
|
node_modules
|
||||||
|
@ -86,11 +86,22 @@ class Recorder extends events.EventEmitter {
|
|||||||
this.globalId = 1;
|
this.globalId = 1;
|
||||||
this.cachePath = getCacheDir();
|
this.cachePath = getCacheDir();
|
||||||
this.db = new Datastore();
|
this.db = new Datastore();
|
||||||
this.db.persistence.setAutocompactionInterval(5001);
|
|
||||||
|
|
||||||
this.recordBodyMap = []; // id - body
|
this.recordBodyMap = []; // id - body
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setDbAutoCompact() {
|
||||||
|
this.db.persistence.setAutocompactionInterval(5001);
|
||||||
|
}
|
||||||
|
|
||||||
|
stopDbAutoCompact() {
|
||||||
|
try {
|
||||||
|
this.db.persistence.stopAutocompaction();
|
||||||
|
} catch (e) {
|
||||||
|
logUtil.printLog(e, logUtil.T_ERR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
emitUpdate(id, info) {
|
emitUpdate(id, info) {
|
||||||
const self = this;
|
const self = this;
|
||||||
if (info) {
|
if (info) {
|
||||||
@ -333,6 +344,7 @@ class Recorder extends events.EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
clear() {
|
clear() {
|
||||||
|
logUtil.printLog('clearing cache file...');
|
||||||
const self = this;
|
const self = this;
|
||||||
proxyUtil.deleteFolderContentsRecursive(self.cachePath, true);
|
proxyUtil.deleteFolderContentsRecursive(self.cachePath, true);
|
||||||
}
|
}
|
||||||
|
@ -216,8 +216,6 @@ function deleteFolderContentsRecursive(dirPath, ifClearFolderItself) {
|
|||||||
throw new Error('can_not_delete_this_dir');
|
throw new Error('can_not_delete_this_dir');
|
||||||
}
|
}
|
||||||
|
|
||||||
logUtil.info('==>>> clearing cache ', dirPath);
|
|
||||||
|
|
||||||
if (fs.existsSync(dirPath)) {
|
if (fs.existsSync(dirPath)) {
|
||||||
fs.readdirSync(dirPath).forEach((file) => {
|
fs.readdirSync(dirPath).forEach((file) => {
|
||||||
const curPath = path.join(dirPath, file);
|
const curPath = path.join(dirPath, file);
|
||||||
|
@ -308,11 +308,15 @@ class webInterface extends events.EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
close() {
|
close() {
|
||||||
this.server && this.server.close();
|
const self = this;
|
||||||
this.wsServer && this.wsServer.closeAll();
|
return new Promise((resolve, reject) => {
|
||||||
this.server = null;
|
self.server && self.server.close();
|
||||||
this.wsServer = null;
|
self.wsServer && self.wsServer.closeAll();
|
||||||
this.proxyInstance = null;
|
self.server = null;
|
||||||
|
self.wsServer = null;
|
||||||
|
self.proxyInstance = null;
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
50
proxy.js
50
proxy.js
@ -248,7 +248,6 @@ class ProxyCore extends events.EventEmitter {
|
|||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* close the proxy server
|
* close the proxy server
|
||||||
*
|
*
|
||||||
@ -326,12 +325,17 @@ class ProxyServer extends ProxyCore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
start() {
|
start() {
|
||||||
|
if (this.recorder) {
|
||||||
|
this.recorder.setDbAutoCompact();
|
||||||
|
}
|
||||||
|
|
||||||
// start web interface if neeeded
|
// start web interface if neeeded
|
||||||
if (this.proxyWebinterfaceConfig && this.proxyWebinterfaceConfig.enable) {
|
if (this.proxyWebinterfaceConfig && this.proxyWebinterfaceConfig.enable) {
|
||||||
this.webServerInstance = new WebInterface(this.proxyWebinterfaceConfig, this.recorder);
|
this.webServerInstance = new WebInterface(this.proxyWebinterfaceConfig, this.recorder);
|
||||||
// start web server
|
// start web server
|
||||||
this.webServerInstance.start().then(() => {
|
this.webServerInstance.start()
|
||||||
// start proxy core
|
// start proxy core
|
||||||
|
.then(() => {
|
||||||
super.start();
|
super.start();
|
||||||
})
|
})
|
||||||
.catch((e) => {
|
.catch((e) => {
|
||||||
@ -343,35 +347,23 @@ class ProxyServer extends ProxyCore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
close() {
|
close() {
|
||||||
return new Promise((resolve, reject) => {
|
const self = this;
|
||||||
super.close()
|
// release recorder
|
||||||
.then((error) => {
|
if (self.recorder) {
|
||||||
if (error) {
|
self.recorder.stopDbAutoCompact();
|
||||||
resolve(error);
|
self.recorder.clear();
|
||||||
}
|
}
|
||||||
});
|
self.recorder = null;
|
||||||
|
|
||||||
if (this.recorder) {
|
// close ProxyCore
|
||||||
logUtil.printLog('clearing cache file...');
|
return super.close()
|
||||||
this.recorder.clear();
|
// release webInterface
|
||||||
}
|
.then(() => {
|
||||||
const tmpWebServer = this.webServerInstance;
|
if (self.webServerInstance) {
|
||||||
this.recorder = null;
|
const tmpWebServer = self.webServerInstance;
|
||||||
this.webServerInstance = null;
|
self.webServerInstance = null;
|
||||||
if (tmpWebServer) {
|
logUtil.printLog('closing webInterface...');
|
||||||
logUtil.printLog('closing webserver...');
|
return tmpWebServer.close();
|
||||||
tmpWebServer.close((error) => {
|
|
||||||
if (error) {
|
|
||||||
console.error(error);
|
|
||||||
logUtil.printLog(`proxy web server close FAILED: ${error.message}`, logUtil.T_ERR);
|
|
||||||
} else {
|
|
||||||
logUtil.printLog(`proxy web server closed at ${this.proxyHostName} : ${this.webPort}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
resolve(error);
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
resolve(null);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user