RejectedSoftware Forums

Sign up

Do I need to call flush when I use HTTPServerResponse.bodyWriter

Code example:

void foo(HTTPServerRequest req, HTTPServerResponse res)
{
	string str = `[1,2,3]`;
	
	res.statusCode = HTTPStatus.OK;
	res.headers["Content-Type"] = "application/json; charset=UTF-8";
	
	res.bodyWriter.write(str);
	res.bodyWriter.flush();
}

Do I need to call res.bodyWriter.flush explicitly?

Re: Do I need to call flush when I use HTTPServerResponse.bodyWriter

Am 01.12.2013 19:36, schrieb ilya-stromberg:

Code example:

void foo(HTTPServerRequest req, HTTPServerResponse res)
{
	string str = `[1,2,3]`;
	
	res.statusCode = HTTPStatus.OK;
	res.headers["Content-Type"] = "application/json; charset=UTF-8";
	
	res.bodyWriter.write(str);
	res.bodyWriter.flush();
}

Do I need to call res.bodyWriter.flush explicitly?

After foo returns, everything is flushed/finalized automatically, so
it's useful only if there is going to be some more processing between
the last write and returning from the function to reduce latency.

Re: Do I need to call flush when I use HTTPServerResponse.bodyWriter

On Sun, 01 Dec 2013 19:45:15 +0100, Sönke Ludwig wrote:

so it's useful only if there is going to be some more processing between
the last write and returning from the function to reduce latency.

What should I use in this case? For example, I want to send answer for client and than save information to the log. Shall I use flush or finalize?

Re: Do I need to call flush when I use HTTPServerResponse.bodyWriter

On Sun, 01 Dec 2013 19:56:15 GMT, ilya-stromberg wrote:

On Sun, 01 Dec 2013 19:45:15 +0100, Sönke Ludwig wrote:

so it's useful only if there is going to be some more processing between
the last write and returning from the function to reduce latency.

What should I use in this case? For example, I want to send answer for client and than save information to the log. Shall I use flush or finalize?

I think that was a bad example on my part*. To do that, the best way is to perform the logging (if it is heavyweight enough, of course) in parallel using runTask, because even if finalize would be used, the (keep-alive) connection would still be blocked until the function returns and would thus delay the next request.

But apart from that, finalize would completely finish the response, so that would be the most correct call, but flush may be enough if the client just needs all data and not necessarily the end marker.

* Needing to avoid latencies during the request is a better example, see f.ex. vibe.stream.operations.pipeRealtime