Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

True async reading of ResponseBody #4169

Closed
herbyderby opened this issue Jul 20, 2018 · 4 comments
Closed

True async reading of ResponseBody #4169

herbyderby opened this issue Jul 20, 2018 · 4 comments
Labels
enhancement Feature not a bug
Milestone

Comments

@herbyderby
Copy link

#873
added the ability to read the ResponseBody on a different thread "at your leisure" (i.e. not constrained to be entirely within the onResponse method).

However, the actual reading of ResponseBody is still blocking, whether via bytes(), byteStream(), or any of the methods on okio.BufferedSource. That can be a problem for long or slow requests.

For truly async processing of a request, there should instead be some callback/listener API that pushes the data to the application, or at least notifies it that more data is available to be read.

(It's possible that this makes more sense as a feature request for okio)

@swankjesse
Copy link
Member

Yeah we've long been considering how to do this. The difficult part is that consuming data one block at a time is often quite ugly.

Maybe coroutines will save the day!

@herbyderby
Copy link
Author

herbyderby commented Jul 21, 2018

FWIW my use case is using okio as the implementation of a higher-level API (which uses Guava ListenableFuture), so it is more important that it is possible than elegant.

Here are some options to consider:

Option 1: new methods on okio.BufferedSource:

/** Returns how many bytes can be read without blocking (possibly zero). */
long available();

/** Requests the given number of bytes, invoking the callback when available. */
void requestAsync(long byteCount, Runnable callback);

Option 2: new method on okhttp3.ResponseBody (may require Option 1 anyway):

/** Passes data to the callback as it becomes available. */
void asyncStream(ByteCallback callback);

public interface ByteCallback {
  void onBytes(ByteString bytes);
  void onExhausted();
}

@swankjesse
Copy link
Member

First steps:
square/okio#531

@swankjesse swankjesse added the enhancement Feature not a bug label Nov 4, 2018
@swankjesse swankjesse added this to the 3.13 milestone Nov 4, 2018
@yschimke
Copy link
Collaborator

Closing as low priority. Could be more relevant if we want to support a really nice JS API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Feature not a bug
Projects
None yet
Development

No branches or pull requests

3 participants