Skip to content

Add isBuffered methods on BinaryData and HttpResponse #38228

@lmolkova

Description

@lmolkova
  1. boolean HttpResponse.isBuffered() method? It'd allow to optimize stuff here and in HttpResponseLogger
  2. something like HttpResponse.addCloseHook(BiSupplier<HttpResponse, Throwable> hook) - this would help us avoid wrapping streams

Not strictly related, but I think BinaryData should also have isBuffered or isBufferable and close to optimize/allow more control to close streams under (e.g. when I don't want to read full content)

Originally posted by @lmolkova in #38013 (comment)

Use cases:

  • Logging: when we log response body, we want to know when response body has been received. Today we have to wrap HttpResponse object into LoggingHttpResponse to achieve it
  • Tracing: the same
  • HttpClients sending request body: can safely do BinaryData.toBytes() is content is already buffered. Today they have to switch over all content implementations. We had some cases in the past where we forgot to update all the places when we added new content types.

Before:

BinaryDataContent content = BinaryDataHelper.getContent(bodyContent);
if (content instanceof ByteArrayContent
|| content instanceof StringContent
|| content instanceof SerializableContent
|| content instanceof ByteBufferContent) {
return RequestBody.create(content.toBytes(), mediaType);
} else {

After

if (bodyContent.isBuffered()) {
    return RequestBody.create(content.toBytes(), mediaType);
} else {

In logging/tracing:

Before

return next.process()
    .doOnSuccess(response -> updateSpan(response, span))
    .map(response -> new TraceableResponse(response, span))

After:

return next.process()
    .doOnSuccess(response -> updateSpan(response, span))
    .map(response ->  {
           if (response.isBuffered()) {
                 endSpan(span);
                 return response;
           }
           return new TraceableResponse(response, span);
     })

Metadata

Metadata

Assignees

No one assigned

    Labels

    Azure.Coreazure-coreClientThis issue points to a problem in the data-plane of the library.needs-team-triageWorkflow: This issue needs the team to triage.

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions