HTTPS request on Lambda very slow on first request

0

Hi,

I am making HTTPS POST request to an API. The first request I make will take somewhere between 30-130 seconds. The subsequent request only takes 2-300 ms. This API handles millions of requests every day, so the problem does not seem to be in the API.

My simple test code:

 const https = require('https');

const acquire = (data, options) => {
  return new Promise((resolve, reject) => {
    options.headers['Content-Length'] = data.length;
    const req = https.request(options, (res) => {
      console.log("REQ compl")
      res.on('data', (data) => {
        console.log(data)
        resolve(data)
      });
    });
    req.on ('error', (error) => {
      console.log("error", error)
    })
    req.write(data);
    req.end();
  })
}

const release = (data, options) => {
  return new Promise((resolve, reject) => {
    options.headers['Content-Length'] = data.length;
    const req = https.request(options, (res) => {
      console.log("REQ compl")
      res.on('data', (data) => {
        console.log(data)
        resolve(data)
      })
    });
    req.on ('error', (error) => {
      console.log("error", error)
    })
    req.write(data);
    req.end();
  })
}

const data = `<methodCall>
  <methodName>acquire_token</methodName>
  <params>
    <param>
      <value>
        <string>KS034</string>
      </value>
    </param>
    <param>
      <value>
        <string>password</string>
      </value>
    </param>
    <param>
      <value>
        <string>secret string</string>
      </value>
    </param>
  </params>
</methodCall>`

const options = {
  hostname: 'wired.wubook.net',
  port: 443,
  path: '/xrws/',
  method: 'POST',
  headers: {
    'Content-Type': 'text/xml',
    'Connection': 'keep-alive',
    'Content-Length': 0
  }
}

exports.handler = async (event) => {
  let resa = 'aaa';
  let resb = '';
    
  await acquire(data, options).then(res => {
    xmlrpc.CustomType.call()
    res = res.toString();
    console.log("acquire complete")
    var start = res.indexOf("<string>");
    var end = res.indexOf("</string>");
    // resa = JSON.stringify(res);
    resa = res.substring(start + 8, end)
  });

  const data1 = `<methodCall>
    <methodName>release_token</methodName>
    <params>
      <param>
        <value>
          <string>${resa}</string>
        </value>
      </param>
    </params>
  </methodCall>`

  await release(data1, options).then(res => {
    resb = res;
    console.log("release complete")
  });

  const response = {
    statusCode: 200,
    body: JSON.stringify('Hello from Lambda!' + resa + resb)
  }
  return response;

};

Is there anything that can explain this behavior? I have tried the XMLRPC client for NodeJS as well; the result is the same unfortunately.

Thank you very much!

Edited by: KasperS on Jan 21, 2020 11:49 AM

KasperS
gefragt vor 4 Jahren342 Aufrufe
1 Antwort
0

It was a problem with the API

KasperS
beantwortet vor 3 Jahren

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen