resurfaceio / logger-nodejs

Log API calls with Node.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Background submission with Axios

RobDickinson opened this issue · comments

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();
    }
}

Manually closing this since the PR was never merged. Commit 9ccfc95 introduced the feature instead.