HomePhabricator
Diffusion Comm 20d64119a849

[lib] Add fetch timeout to reports service calls

Description

[lib] Add fetch timeout to reports service calls

Summary:
Raised by @tomek in D9043 - this adds a timeout to the fetch calls to the reports service.

After doing some researc, I've decided to go with AbortController instead of Promise.race. The main advantage of this solution is, that it cancels the fetch request, instead of just ignoring it. This should prevent the request from being executed in the background and potentially consuming bandwidth.

Depends on D9043

Test Plan:
Tested on web and native. Used https://httpstat.us/ to generate delayed responses.

For native, it's convenient to add a button to dev-menu.js:

{
  name: 'Test timeout',
  callback: async () => {
    try {
      const response = await fetchWithTimeout('https://httpstat.us/200', {
        timeout: 3000,
        headers: {
          'X-HttpStatus-Sleep': '4000',
        },
      });
      const text = await response.text();
      // for timeout > delay, this should be displayed with "OK"
      Alert.alert('Got response', text);
    } catch (e) {
       // for timeout < delay, this should be displayed with "Request timed out"
      Alert.alert('Got error:', getMessageForException(e));
    }
  },
},

When externalSignal is set, the error message is AbortError: Operation aborted on native and DOMException: The user aborted a request. on web

Reviewers: tomek, kamil, michal

Reviewed By: tomek

Subscribers: ashoat, tomek

Differential Revision: https://phab.comm.dev/D9073

Details

Provenance
bartekAuthored on Sep 3 2023, 4:09 AM
Reviewer
tomek
Differential Revision
D9073: [lib] Add fetch timeout to reports service calls
Parents
rCOMMebfc840ce3ad: [native] Fix long ID display on crash screen
Branches
Unknown
Tags
Unknown