I’m trying to understand how to use HTTP.jl’s middleware to log the amount of time between two distinct events:
The server started sending the response
The client received it
The following MWE doesn’t accomplish this, but might serve as a good starting point:
using HTTP, Random
const times = UInt64[]
function get_durations(times)
calculating, sending = round.(diff(times) ./ 10^9, digits = 4)
@info "durations:" calculating sending
empty!(times)
end
function log_time(handler)
return function(req)
ret = handler(req)
push!(times, time_ns()) # log time to send data?
get_durations(times)
return ret
end
end
function handler(req)
push!(times, time_ns()) # log starting time
data = randstring(1_000_000)
push!(times, time_ns()) # log time to calculate data
return HTTP.Response(200, data)
end
HTTP.serve(log_time(handler))
Generally you can’t know this during request handling. There may be intervening servers, such as reverse proxies. If you control the client, maybe have the client send back a timestamp after receiving the response.
Yeah, I agree. We could potentially add an “eventing system” to HTTP.jl on both client and server side so you could hook into various checkpoints, but even then, you would need to even both client and server to really get what you’re after here.
This doesn’t address the issue raised above. The client doesn’t necessarily receive the response when the server has finished writing. It depends on your use case and client/server configuration whether it is good enough.