E
E
eifory2020-09-07 21:42:33
JavaScript
eifory, 2020-09-07 21:42:33

How to implement GET request timeout for leader election?

I'm trying to implement the leader Election algorithm for educational purposes.
Here is the TK https://github.com/peterservice-rnd/new-job/blob/m...

Inside the function that starts the election, there is a section of code in which you need to set a timeout for waiting for
a GET request for the route. I don't send requests, I wait for requests from other nodes. Something like
router.get('/IAMTHEKING/:id', setLeaderId);

...
const setLeaderAnswerTimeout = new Promise((resolve, reject) => {
    timeoutId = setTimeout(() => reject('leaderAnswer timeout'), process.env.CHECK_PERIOD);
});

await Promise.race([setLeaderAnswerTimeout, waitLeaderAnswer]);
...

waitLeaderAnswer must return resolve if the request has arrived, if there is no request, then reject must be returned by timeout.

I also thought about the request.timeout options or plugging in the middleware.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question