resurfaceio / logger-nodejs

Log API calls with Node.js
Apache License 2.0
16 stars 2 forks source link

Background submission with Axios #24

Closed RobDickinson closed 2 years ago

RobDickinson commented 3 years ago

Send POST requests on a separate thread with bounded queue. Introduce BaseLogger.maxQueueDepth (with default value of 128) to control the depth of the bounded queue before the response is blocked.

BaseLogger.submit is where the handoff to the background thread should be done. (Rules engine and JSON conversion will continue to be done in the calling thread)

BaseLogger needs only a single background thread for performing POST requests since a single CPU core is normally capable of saturating the network.

Use async and axios libraries to do POST submissions asynchronously on a separate thread.

const async = require('async');
const transmitQ = async.queue((task, callback) => {
    send_message(task.message, callback);  // poll until queue is reasonably sized?
}, 1);
const axios = require('axios');
function send_message(message, callback) {
    try {
    axios.post(url, message.toString())
        .then(function (response) {
            if (response.status != 204) console.log("ARRRRHHHHHHHHHHHHHHHHHHHHHHHHHHHHH");
            callback();
        })
        .catch(function (error) {
            callback();
        });
    } catch (e) {
        callback();
    }
}
monrax commented 2 years ago

Manually closing this since the PR was never merged. Commit https://github.com/resurfaceio/logger-nodejs/commit/9ccfc95f8b24e3b74909cfe2fcb938707c6e714c introduced the feature instead.