A
A
Artem Rogozin2019-12-04 11:04:34
go
Artem Rogozin, 2019-12-04 11:04:34

Exiting a function if the variable is empty for a certain amount of time?

The essence is this - there is a function that sends requests to the server in an endless loop, at a certain interval. The response is parsed into a structure, but the response is not empty only if an event occurs on the server. So, how to implement a condition in th that will check that the variable was empty for 2 hours, i.e. no events occurred on the server for two hours. Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Shitskov, 2019-12-04
@syberianv

Here is an example of such an implementation
https://yourbasic.org/golang/time-reset-wait-stop-...
Each time you receive non-zero data from the server, reset the timer back to 2 hours (reset method) . When a timer event occurs - return

I
Ilya Kaznacheev, 2019-12-05
@Color

Two options: statefull and stateless
- statefull - start a timer (the link was given above) for two hours, and exit either when a non-empty response arrives, or according to the timer.
- stateless - store the start time somewhere, and with each empty response, read it and compare it with the current time, exit if exceeded. In this case, you need to add storage (db, file, whatever), but you can not be afraid to lose the process in two hours and generally more flexibility.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question