*Make dht cache optional, but enabled by default
This commit is contained in:
parent
1e3b0e46fe
commit
af4e6155bc
|
@ -29,6 +29,7 @@ config.inject({
|
||||||
logLevel: "info",
|
logLevel: "info",
|
||||||
pluginDir: path.resolve(configDir, "..", "plugins"),
|
pluginDir: path.resolve(configDir, "..", "plugins"),
|
||||||
plugins: ["core"],
|
plugins: ["core"],
|
||||||
|
cache: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
config.load();
|
config.load();
|
||||||
|
|
|
@ -18,6 +18,7 @@ import { Mutex } from "async-mutex";
|
||||||
import { RPCCache } from "./cache";
|
import { RPCCache } from "./cache";
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import jsonStringify from "json-stringify-deterministic";
|
import jsonStringify from "json-stringify-deterministic";
|
||||||
|
import config from "../../config";
|
||||||
|
|
||||||
const sodium = require("sodium-universal");
|
const sodium = require("sodium-universal");
|
||||||
let server: RPCServer;
|
let server: RPCServer;
|
||||||
|
@ -54,9 +55,16 @@ export class RPCServer extends EventEmitter {
|
||||||
>();
|
>();
|
||||||
private pendingRequests: Map<string, Mutex> = new Map<string, Mutex>();
|
private pendingRequests: Map<string, Mutex> = new Map<string, Mutex>();
|
||||||
|
|
||||||
private _cache: RPCCache = new RPCCache(this);
|
private _cache?: RPCCache;
|
||||||
|
|
||||||
get cache(): RPCCache {
|
constructor() {
|
||||||
|
super();
|
||||||
|
if (config.bool("cache")) {
|
||||||
|
this._cache = new RPCCache(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
get cache(): RPCCache | undefined {
|
||||||
return this._cache;
|
return this._cache;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -137,7 +145,7 @@ export class RPCServer extends EventEmitter {
|
||||||
}
|
}
|
||||||
|
|
||||||
return crypto
|
return crypto
|
||||||
.sign(Buffer.from(raw), this._cache.swarm.keyPair.secretKey)
|
.sign(Buffer.from(raw), this._cache?.swarm.keyPair.secretKey)
|
||||||
.toString("hex");
|
.toString("hex");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -201,8 +209,8 @@ export class RPCServer extends EventEmitter {
|
||||||
}
|
}
|
||||||
|
|
||||||
method = method as RPCMethod;
|
method = method as RPCMethod;
|
||||||
if (method.cacheable) {
|
if (config.bool("cache") && method.cacheable) {
|
||||||
this.cache.addItem(request, rpcResult);
|
this.cache?.addItem(request, rpcResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.getRequestLock(request)?.release();
|
this.getRequestLock(request)?.release();
|
||||||
|
@ -211,9 +219,14 @@ export class RPCServer extends EventEmitter {
|
||||||
}
|
}
|
||||||
|
|
||||||
private getCachedRequest(request: RPCRequest): RPCCacheItem | boolean {
|
private getCachedRequest(request: RPCRequest): RPCCacheItem | boolean {
|
||||||
|
if (!config.bool("cache")) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
const req = RPCServer.hashQuery(request);
|
const req = RPCServer.hashQuery(request);
|
||||||
if (this._cache.data.has(req)) {
|
|
||||||
return this._cache.data.get<RPCCacheItem>(req) as RPCCacheItem;
|
if (this._cache?.data.has(req)) {
|
||||||
|
return this._cache?.data.get<RPCCacheItem>(req) as RPCCacheItem;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
@ -256,8 +269,8 @@ export class RPCServer extends EventEmitter {
|
||||||
|
|
||||||
if (lock.isLocked()) {
|
if (lock.isLocked()) {
|
||||||
await lock.waitForUnlock();
|
await lock.waitForUnlock();
|
||||||
if (this._cache.data.has(reqId)) {
|
if (this._cache?.data.has(reqId)) {
|
||||||
return this._cache.data.get<RPCCacheItem>(reqId) as RPCCacheItem;
|
return this._cache?.data.get<RPCCacheItem>(reqId) as RPCCacheItem;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue