简体   繁体   中英

NodeJS generic-pool how to set request timeout?

I'm new to using the generic-pool. I see that there is a maxWaitingClients setting, but it doesn't specify for how long a request can stay in the queue before it times out. Is there any way for me to specify such a timeout setting?

EDIT: Added my code for how I'm currently using generic-pool:

function createPool() {
    const factory = {
        create: function() {
            return new Promise((resolve, reject) => {
                const socket = new net.Socket();
                socket.connect({
                    host: sdkAddress,
                    port: sdkPort,
                });
                socket.setKeepAlive(true);
                socket.on('connect', () => {
                    resolve(socket);
                });
                socket.on('error', error => {
                    reject(error);
                });
                socket.on('close', hadError => {
                    console.log(`socket closed: ${hadError}`);
                });
            });
        },
        destroy: function(socket) {
            return new Promise((resolve) => {
                socket.destroy();
                resolve();
            });
        },
        validate: function (socket) {
            return new Promise((resolve) => {
                if (socket.destroyed || !socket.readable || !socket.writable) {
                    return resolve(false);
                } else {
                    return resolve(true);
                }
            });
        }
    };
    return genericPool.createPool(factory, {
        max: poolMax,
        min: poolMin,
        maxWaitingClients: poolQueue,
        testOnBorrow: true
    });
}
 
const pool = createPool();

async function processPendingBlocks(ProcessingMap, channelid, configPath) {
    setTimeout(async () => {
        let nextBlockNumber = fs.readFileSync(configPath, "utf8");
        let processBlock;
 
        do {
            processBlock = ProcessingMap.get(channelid, nextBlockNumber);
            if (processBlock == undefined) {
                break;
            }
 
            try {
                const sock = await pool.acquire();
                await blockProcessing.processBlockEvent(channelid, processBlock, sock, configPath, folderLog);
                await pool.release(sock);
            } catch (error) {
                console.error(`Failed to process block: ${error}`);
            }
 
            ProcessingMap.remove(channelid, nextBlockNumber);
            fs.writeFileSync(configPath, parseInt(nextBlockNumber, 10) + 1);
            nextBlockNumber = fs.readFileSync(configPath, "utf8");
        } while (true);
 
        processPendingBlocks(ProcessingMap, channelid, configPath);
    }, blockProcessInterval)
}

Since you are using pool.acquire() , you would use the option acquireTimeoutMillis to set a timeout for how long .acquire() will wait for an available resource from the pool before timing out.

You would presumably add that option here:

return genericPool.createPool(factory, {
    max: poolMax,
    min: poolMin,
    maxWaitingClients: poolQueue,
    testOnBorrow: true,
    acquireTimeoutMillis, 3000            // max time to wait for an available resource
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM