D
D
Danbka2022-03-14 17:39:57
go
Danbka, 2022-03-14 17:39:57

How to put the application "on pause"?

Hey!

Simplified:
There is a golang application that receives data via http from an external server once a second, somehow processes them and sends them back.

If the server returns code 500 upon receiving data, then it is necessary to pause the application: stop processing already received data and send the processed data back to the server (technically, this will mean clearing buffered channels and data that is already spinning inside the application).

As soon as the response from the server comes 200, everything continues to work again.

Please tell me links with examples of how best to manage such a kind of global state of the application.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vindicar, 2022-03-14
@Vindicar

I have never written in Go, but I can ask a smart question about the architecture. =)

As soon as the response from the server comes 200, everything continues to work again.

That is, the application must continue to work, at least in terms of sending requests to the server! Otherwise, how does it know that it is possible to work further?
Further, if I understand correctly that you are twisting the following pseudo-code in a loop?
while (true) {
    data = query_server();
    result = process_data(data);
    send_result(result);
}

Then why not have query_server() report the success of the query one way or another?
And then, if the request was successful, perform the following two steps.

R
Roman Mirilaczvili, 2022-03-17
@2ord

It's all wrong somehow.
If there is a request, then there is a connection. Until a response is returned, the web server must keep the connection. And if no response is returned, then the client waits until the connection times out. So this technique is quite costly in terms of resources that can be exhausted. Especially if you have a large number of clients.
Well, and so, error 500 can mean both a logical and a connection error with the DBMS, and that some resource of the 3rd party is unavailable, and much more. So if it is logical, then the server will not "correct" and, at the same time, will not come out of hibernation if it waits for a response of 200.
Well, you can block execution using channels. But I don't see any practical use for it.

D
darst, 2022-03-24
@darst

Try this

switch request.StatusCode {
case http.StatusOK:
    продолжаешь обработывать
case http.StatusInternalServerError:
    здесь например ставишь таймаут (time.Sleep(2*time.Second)) или как-то ещё тормозишь
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question