#[HTTP Server] Send response and continue working in same goroutine

16 messages · Page 1 of 1 (latest)

tame basin
#

^ Second request "updating" the first one already makes call to the API.

#

First one happens first, within few ms since receiving http request but due to goroutine being alive for over 3s it makes caller assume it failed to respond.

wise yew
#

you can write response early.

tame basin
#

Yeah but it doesn't work because it looks Go doesn't close connection

#

Only workaround for now I found was writing header with StatusNoContent (204 code) and then most services waits patiently over those 3s for full fata.

wise yew
tame basin
#

Yeah, I would like to send it early and close it but remain in same goroutine

wise yew
#

and why you want to do that?
you can try to hijack it.

    // Hijack lets the caller take over the connection.
    // After a call to Hijack the HTTP server library
    // will not do anything else with the connection.
tame basin
#

I want to do it because then I could send my first payload in already opened connection, second in new request. With 204 code trick I end up doing 2 new connections so it means more bandwith being used and slightly slower processing.

#

I just wanted to utilize that there's already http connection

#

Instead just closing it with empty 204 promise

#

Not sure exactly why but if I write header with code 204 and try to add body to it - it will fail, I can do either 204 code or code 200 with body

wise yew
#

i dont think 204 trick will work.
also, you dont need to worry about whether the connection is reused or not, keepalive should take care of that.

tame basin
#

I think I explained it wrongly... Thing is that I can do either:
1.) Return 204 code and then in same goroutine open new API request sending "processing" payload, later do second API request with "finished" payload.
2.) Return "processing" data in original http.ResponseWriter I received and close it with code 200, later do API request with "finished" payload.

Issue with second is that this (potentially) slow processing happens in same goroutine and Go has this mechanic it won't actually close that first http writer until goroutine ends.

#

Second option seems more efficient for me as instead doing 3 http operations in total, it would work on 2 so it should be faster?