Daki Withanage
Daki Withanage

Reputation: 53

Azure Http Trigger function Hangup socket after the first webhook response

I am using following code inside azure http trigger function.

var form = new FormData();
var fetch = require("node-fetch");
var https = require('https');
var httpAgents_ = new https.Agent({keepAlive: true});
httpAgents_.maxSockets = 200;
module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

   let form_ = getMultiData(context);

   const response = await getResponse(form_,context);

    context.res = { 
           body: response };
    context.done;
};
async function cerenceLookResponse(body_,context)
{
  context.log('calling http function')
   const url = "https://webhook.site/5c7950af-85fc-49b5-a677-12430805a159";
    return fetch(url, {
        method: 'POST',
        body: form,
        headers: {
            "Accept": "*/*",
            "Accept-Encoding": "gzip, deflate, br",
            "Connection":"Keep-Alive",
            "Content-Length":1330,
            "Keep-Alive": "timeout=30000",
            "Transfer-Encoding": "chunked",
        },
        agent: httpAgents_
        
        }).then(res => res.text());
};
function getMultiData(context){
    var content = { "cmdDict":{ "device": "iso", };
    var content2 ={"appserver_data": {"action_mode": "default",  },"enable_filtering": 1,  };
    var options = { header: {'content-type': "application/JSON; charset=utf-8"}};

    form.append('RequestData', JSON.stringify(content), options);
    form.append('DictParameter', JSON.stringify(content2), options);

    var ar = []
    for(var i=0;i<54;i++){
        ar.push(form._streams[3][i]); }
    var ar2 = []
    for(var i=0;i<52;i++){
        ar2.push(form._streams[3][i]);}
    boun = ar2.join("")
    test1_orig = ar.join("")
    test2 = "Content-Disposition: form-data; name=\"DictParameter\"; paramName=\"REQUEST_INFO\""
    test1 = test1_orig + test2 + "\r\n" +'content-type: application/JSON; charset=utf-8\r\n' + '\r\n',
    form._streams[3] = test1
    form._streams.push(boun + "--\r\n");
  
    return form;

};

This program perfectly fine when I work with a local WebStorm terminal. However, on Azure portal first time it gets the response from the web hook and then if I want to run once again right after I get the response. The function stall and after 3 min and throws an error "socket hang upStack: FetchError". What am I doing wrong here?

Upvotes: 0

Views: 413

Answers (1)

krishg
krishg

Reputation: 6508

You are creating form in the global scope. So every time you are calling the getMultiData function, duplicate stuffs getting added in the same FormData instance. Declare it locally inside getMultiData function.

Upvotes: 1

Related Questions