nodejs and express with conection timeout (h13) in heroku when dealing with pagseguro notifications

1

Hello, I have an api in heroku and she communicates with the pagseguro api (notifications), the wheel that handles the notification needs to pick up the transaction code that is received by post, make an appointment in the pagemobile api get the statuses and some data, then need to access my database to check some tables and update others (add currencies to the user and register in the flow) the problem I'm encountering is h13 (conection timeout). When I put the code on another server the error remains my route:

router.post("/notificacao", function (req, res) {
    const parseString = require('xml2js').parseString;
    const request = require("request");
    if (req.body.notificationType == "transaction") {
        console.log("reconheceu o type")
        if (req.body.notificationCode) {
            const link = "https://ws.pagseguro.uol.com.br/v3/transactions/notifications/"
                + req.body.notificationCode
                + "?email=****&token=****";
            console.log(link);
            request.get(link, (error, response, body) => {
                if (error) {
                    res.status(503).send({ status: "erro" })
                    return console.dir(error);
                }
                // return
                parseString(body, function (err, result) {
                    const status = result.transaction.status
                    Transacao.select(null, [{ name: "cod_transacao", value: result.transaction.code }])
                        .then((rowsT, fieldsR) => {
                            rowsT = rowsT[0]
                            console.log("rowsT", rowsT)
                            const notData = {
                                cod_transacao: result.transaction.code,
                                cod_mod: req.body.notificationCode,
                                status: result.transaction.status,
                                data_modificacao: fecha.format(new Date(), 'YYYY-MM-DD HH:mm:ss'),
                            }
                            // console.log("o q eu consultei", rowsT[0].instalador_id)
                            if ((parseInt(status) == 3 || parseInt(status) == 4) && rowsT.moedas != "adicionadas") {
                                console.log("add moedas")
                                const Instalador = require("../../instalador/instalador.js")
                                Instalador.addMoedas(rowsT.instalador_id, rowsT.id_plano).then(() => {
                                    notData['moedas'] = 'adicionadas'
                                    Transacao.update(notData).then((rows, fields) => {
                                        res.status(200).send(rows)
                                    })
                                })
                            } else {
                                if (parseInt(status) > 5) {
                                    if (rowsT.moedas = 'adicionadas') {
                                        const Instalador = require("../../instalador/instalador.js")
                                        Instalador.removeMoedas(rowsT.instalador_id, rowsT.id_plano).then(() => {
                                            notData['moedas'] = 'removidas'
                                            Transacao.update(notData).then((rows, fields) => {
                                                res.status(200).send(rows)
                                            })
                                        })
                                    } else {
                                        Transacao.update(notData).then((rows, fields) => {
                                            res.status(200).send(rows)
                                        })
                                    }
                                }

                            }


                        }).catch((error => {
                            res.status(500).send({
                                erro: String(err)
                            })
                        }))
                });
            });
        }
    }
    console.log(req.body)
})

The error message is:

Dec 26 02:56:07 api-webar-teste heroku/router:  at=error code=H13 desc="Connection closed without response" method=POST path="/financeiro/transacao/notificacao" host=****.herokuapp.com request_id=940e82ff-ae69-41c6-8c4e-da22cea9f5eb fwd="186.234.144.18" dyno=web.1 connect=1ms service=26003ms status=503 bytes=0 protocol=https  
    
asked by anonymous 26.12.2018 / 13:22

1 answer

0

I moved my bank connection to a pool as in docs

var mysql = require('mysql');
var pool  = mysql.createPool({
  connectionLimit : 10,
  host            : 'example.org',
  user            : 'bob',
  password        : 'secret',
  database        : 'my_db'
});
 
pool.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
  if (error) throw error;
  console.log('The solution is: ', results[0].solution);
});

this solved the timeouts

    
12.01.2019 / 22:36