Skip to content

Commit

Permalink
keep tcp connections open if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgtho committed Sep 22, 2023
1 parent d8c6f8e commit fb6683f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/archive.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { logger } = require('@vtfk/logger')
const axios = require('axios').default
const axios = require('./axios-instance')()
const { fromBuffer } = require('file-type')
const path = require('path')
const { ARCHIVE_URL, ARCHIVE_KEY, FILE_FORMATS } = require('../config')
Expand Down Expand Up @@ -48,11 +48,11 @@ const callArchive = async (payload, e18JobId, e18TaskId, endpoint) => {
},
maxBodyLength
}

const { data } = await axios.post(`${ARCHIVE_URL}/${endpoint}`, payload, headers)
logger('info', ['archive', 'callArchive', 'data found'])
return data
} catch (error) {
console.log(error.response?.data || error.stack || error.toString())
const { status, message, data } = error.response
if ((status === 404 && endpoint === 'SyncElevmappe') || (data && data.error && data.error === 'VANLIG BOSATT, mangler ADR og ADR1-3')) {
logger('info', ['archive', 'aiaiaiaiai', status, message || data])
Expand Down
18 changes: 18 additions & 0 deletions lib/axios-instance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const axios = require('axios').default
const https = require('https')

let instance = null

// To avoid TCP port exhaustion on many runs at once
module.exports = () => {
if (!instance) {
console.log('instance does not exits - creating')
instance = axios.create({
httpsAgent: new https.Agent({
keepAlive: true,
maxSockets: 200
})
})
}
return instance
}
2 changes: 1 addition & 1 deletion lib/e18.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const axios = require('axios').default
const axios = require('./axios-instance')()
const { logger } = require('@vtfk/logger')
const { E18_URL, E18_KEY, E18: { PROJECT_ID: projectId, SYSTEM: system, TYPE: type } } = require('../config')

Expand Down
2 changes: 1 addition & 1 deletion lib/isi-lokal.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { logger } = require('@vtfk/logger')
const axios = require('axios').default
const axios = require('./axios-instance')()
const { parseStringPromise, processors } = require('xml2js')

const { ISI: { URL, GET_DOCUMENTS, SET_STATUS } } = require('../config')
Expand Down

0 comments on commit fb6683f

Please sign in to comment.