Skip to content

Commit

Permalink
Cancel okhttp3 requests.
Browse files Browse the repository at this point in the history
Progress toward #257
  • Loading branch information
sjudd committed Jan 24, 2016
1 parent 24f395b commit 0bc1dc6
Showing 1 changed file with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class OkHttpStreamFetcher implements DataFetcher<InputStream> {
private final GlideUrl url;
private InputStream stream;
private ResponseBody responseBody;
private volatile Call call;

public OkHttpStreamFetcher(Call.Factory client, GlideUrl url) {
this.client = client;
Expand All @@ -37,7 +38,9 @@ public InputStream loadData(Priority priority) throws Exception {
}
Request request = requestBuilder.build();

Response response = client.newCall(request).execute();
Response response;
call = client.newCall(request);
response = call.execute();
responseBody = response.body();
if (!response.isSuccessful()) {
throw new IOException("Request failed with code: " + response.code());
Expand Down Expand Up @@ -69,6 +72,9 @@ public String getId() {

@Override
public void cancel() {
// TODO: call cancel on the client when this method is called on a background thread. See #257
Call local = call;
if (local != null) {
local.cancel();
}
}
}

0 comments on commit 0bc1dc6

Please sign in to comment.