ram64
Version:
Multi-threaded 64bit memory cache database inspired by redis-like features
45 lines (44 loc) • 1.85 kB
JavaScript
;
exports.startup = startup;
exports.DEFAULT_CONCURRENCY = exports.DEFAULT_SHARD_COUNT = void 0;
var _workerThreads = require("worker_threads");
var _os = require("os");
var _ = require(".");
var _createWorker = require("./create-worker");
var _rand = require("../util/rand");
const CPU_COUNT = (0, _os).cpus().length;
const DEFAULT_SHARD_COUNT = 100000;
exports.DEFAULT_SHARD_COUNT = DEFAULT_SHARD_COUNT;
const DEFAULT_CONCURRENCY = 100;
exports.DEFAULT_CONCURRENCY = DEFAULT_CONCURRENCY;
async function startup({ threadCount =CPU_COUNT , shardCount =DEFAULT_SHARD_COUNT , maxMemory , concurrency =DEFAULT_CONCURRENCY } = {}) {
if (!_workerThreads.isMainThread) throw new Error(`RAM64.startup() must be called from the main thread`);
const connectKey = (0, _rand).randomString();
const shardsPerThread = Math.ceil(shardCount / threadCount);
shardCount = shardsPerThread * threadCount;
// TODO: support timeouts/errors?
const promise = new Promise((resolve, reject)=>{
// create workers
const workers = Array.from({
length: threadCount
}).map((v, workerIndex)=>(0, _createWorker).createWorker({
connectKey,
workerIndex,
workerCount: threadCount,
shardIndex: workerIndex * shardsPerThread,
shardsPerThread,
shardCount,
maxMemory
}));
const instance = new _.RAM64({
connectKey,
workers,
shardCount,
concurrency
});
// wait until the first message is received from each worker
return Promise.all(workers.map((worker)=>new Promise((resolve2)=>worker.once('message', resolve2)).then(()=>resolve(instance))));
});
return promise;
}
//# sourceMappingURL=startup.js.map