I have a web client and a Go server. The client send some json data, which is processed and the server then return a json response.
But what can I do when I want to inform the client about the results of a very slow process, and even allow the client to stop the process?
I've been thinking maybe I could keep sending new requests every 5-10 seconds for updates, but that doesn't seem very efficient, and it wouldn't allow me to stop a process I started using go mySlowFunc()
You may create some “guards” for slow functions. They limit execution time, of function succeeded during this time they return result, if not - default value is returned and function is cancelled.
Example of code:
select {
case result := <-successChan:
return result, nil
case <-timeoutChan:
return "", nil
}
Example of usage: https://github.com/lisitsky/go-site-search-string