Sobes.tech
Junior — Middle+

Creating a progressive update of long-running operation results

livecode

Task condition

It is necessary to modify the existing web service so that a long-running operation is performed in a separate thread and does not block the main server process. The client should receive the latest data as it becomes available, rather than waiting for the entire task to complete.

Requirements:

  • Convert the execution of the long function to asynchronous mode.
  • Ensure gradual sending of the most recent results to the client.
  • Guarantee that the main server thread remains free to handle new requests.

Example of the original code (variable names can be changed, but the logic remains the same):

func ComputeHeavy() int {
    res := externalCall()
    // process res
    return res
}

func main() {
    // initialize server
    
    handler := func(w http.ResponseWriter, r *http.Request) {
        // write response
    }
    http.HandleFunc("/", handler)
    
    // run server
}