Merge pull request #3392 from gradido/backend_keep_alive

feat(backend): speed up gdt request with keep alive connections
This commit is contained in:
einhornimmond 2024-12-17 17:01:45 +01:00 committed by GitHub
commit 02a2b46cf5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 19 additions and 6 deletions

View File

@ -0,0 +1,5 @@
import { Agent } from 'http'
import { Agent as HttpsAgent } from 'https'
export const httpAgent = new Agent({ keepAlive: true })
export const httpsAgent = new HttpsAgent({ keepAlive: true })

View File

@ -7,10 +7,12 @@ import axios from 'axios'
import { LogError } from '@/server/LogError'
import { backendLogger as logger } from '@/server/logger'
import { httpAgent, httpsAgent } from './ConnectionAgents'
export const apiPost = async (url: string, payload: unknown): Promise<any> => {
logger.trace('POST', url, payload)
try {
const result = await axios.post(url, payload)
const result = await axios.post(url, payload, { httpAgent, httpsAgent })
logger.trace('POST-Response', result)
if (result.status !== 200) {
throw new LogError('HTTP Status Error', result.status)
@ -27,7 +29,7 @@ export const apiPost = async (url: string, payload: unknown): Promise<any> => {
export const apiGet = async (url: string): Promise<any> => {
logger.trace('GET: url=' + url)
try {
const result = await axios.get(url)
const result = await axios.get(url, { httpAgent, httpsAgent })
logger.trace('GET-Response', result)
if (result.status !== 200) {
throw new LogError('HTTP Status Error', result.status)

View File

@ -4,6 +4,7 @@
/* eslint-disable @typescript-eslint/no-unsafe-argument */
import axios from 'axios'
import { httpAgent, httpsAgent } from '@/apis/ConnectionAgents'
import { CONFIG } from '@/config'
import { LogError } from '@/server/LogError'
import { backendLogger as logger } from '@/server/logger'
@ -126,9 +127,10 @@ export async function createGmsUser(apiKey: string, user: GmsUser): Promise<bool
accept: 'application/json',
language: 'en',
timezone: 'UTC',
connection: 'keep-alive',
authorization: apiKey,
},
httpAgent,
httpsAgent,
}
try {
const result = await axios.post(baseUrl.concat(service), user, config)
@ -160,9 +162,10 @@ export async function updateGmsUser(apiKey: string, user: GmsUser): Promise<bool
accept: 'application/json',
language: 'en',
timezone: 'UTC',
connection: 'keep-alive',
authorization: apiKey,
},
httpAgent,
httpsAgent,
}
try {
const result = await axios.patch(baseUrl.concat(service), user, config)
@ -197,9 +200,10 @@ export async function verifyAuthToken(
accept: 'application/json',
language: 'en',
timezone: 'UTC',
connection: 'keep-alive',
// authorization: apiKey,
},
httpAgent,
httpsAgent,
}
try {
const result = await axios.get(baseUrl.concat(service), config)

View File

@ -20,7 +20,9 @@ export class HumHubClient {
// eslint-disable-next-line no-useless-constructor, @typescript-eslint/no-empty-function
private constructor() {
this.restClient = new RestClient('gradido-backend', CONFIG.HUMHUB_API_URL)
this.restClient = new RestClient('gradido-backend', CONFIG.HUMHUB_API_URL, undefined, {
keepAlive: true,
})
logger.info('create rest client for', CONFIG.HUMHUB_API_URL)
}