DeepLcom / deepl-node

Official Node.js library for the DeepL language translation API.
MIT License
371 stars 22 forks source link

cause: Error: socket hang up when try to send many requests #24

Open CentralMatthew opened 1 year ago

CentralMatthew commented 1 year ago

I need to translate a lot of data, when i try to do translate in loop i got this error. How i can prevent this?

ConnectionError: Connection failure: socket hang up at Function.sendAxiosRequest (/Users/matthew/IdeaProjects/backend/node_modules/deepl-node/dist/client.js:163:27) at runMicrotasks () at processTicksAndRejections (node:internal/process/task_queues:96:5) at async HttpClient.sendRequestWithBackoff (/Users/matthew/IdeaProjects/backend/node_modules/deepl-node/dist/client.js:114:28) at async Translator.translateText (/Users/matthew/IdeaProjects/backend/node_modules/deepl-node/dist/index.js:354:41) at async /Users/matthew/IdeaProjects/backend/lib/utils/formatDocsJSON.js:156:17 at async Promise.all (index 45) at async /Users/matthew/IdeaProjects/backend/lib/utils/formatDocsJSON.js:140:16 at async Promise.all (index 3) at async formatJsonFile (/Users/matthew/IdeaProjects/backend/lib/utils/formatDocsJSON.js:139:18) at async main (/Users/matthew/IdeaProjects/backend/lib/utils/formatDocsJSON.js:34:5) { error: AxiosError: socket hang up at Function.AxiosError.from (/Users/matthew/IdeaProjects/backend/node_modules/axios/dist/node/axios.cjs:789:14) at RedirectableRequest.handleRequestError (/Users/matthew/IdeaProjects/backend/node_modules/axios/dist/node/axios.cjs:2744:25) at RedirectableRequest.emit (node:events:525:35) at ClientRequest.eventHandlers. (/Users/matthew/IdeaProjects/backend/node_modules/follow-redirects/index.js:14:24) at ClientRequest.emit (node:events:513:28) at TLSSocket.socketOnEnd (node:_http_client:518:9) at TLSSocket.emit (node:events:525:35) at endReadableNT (node:internal/streams/readable:1358:12) at processTicksAndRejections (node:internal/process/task_queues:83:21) { code: 'ECONNRESET', config: { transitional: [Object], adapter: [Array], transformRequest: [Array], transformResponse: [Array], timeout: 20000, xsrfCookieName: 'XSRF-TOKEN', xsrfHeaderName: 'X-XSRF-TOKEN', maxContentLength: -1, maxBodyLength: -1, env: [Object], validateStatus: null, headers: [AxiosHeaders], url: '/v2/translate', method: 'post', baseURL: 'https://api.deepl.com', responseType: 'text', data: 'target_lang=en-US&source_lang=de&text=Apikoaortales%2C+klappentragendes+Konduit' }, request: Writable { _writableState: [WritableState], _events: [Object: null prototype], _eventsCount: 3, _maxListeners: undefined, _options: [Object], _ended: true, _ending: true, _redirectCount: 0, _redirects: [], _requestBodyLength: 79, _requestBodyBuffers: [Array], _onNativeResponse: [Function (anonymous)], _currentRequest: [ClientRequest], _currentUrl: 'https://api.deepl.com/v2/translate', _timeout: null,

},
cause: Error: socket hang up
    at connResetException (node:internal/errors:705:14)
    at TLSSocket.socketOnEnd (node:_http_client:518:23)
    at TLSSocket.emit (node:events:525:35)
    at endReadableNT (node:internal/streams/readable:1358:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  code: 'ECONNRESET'
}

}, shouldRetry: false }

This is my configutration deepl.Translator(config.deeplApiKey, { maxRetries: 50, minTimeout: 20000 });

JanEbbing commented 1 year ago

Hi, could you share the code (the for loop) please? I would guess this either happens when the API is down/the server crashes or something similar, maybe it can also happen when you send too many requests in a short timeframe.

I would recommend two things: a) If you have many small texts, bundle them in one request - translateText accepts an array (translateText(["Hallo Welt", "Hello World"], "PL")) with up to 50 entries. b) If that doesn't solve the problem, add some client-side rate limiting if you send too many requests at once (if you have many texts and your for loop is written asynchronously, you could be launching hundreds of requests in parallel, which would explain this error - you want to have a parameter that controls how many parallel connections you can have at most).

We will support b) better in the future, this is a feature we are working on.