diff options
author | Anonymous <no-reply@google.com> | 2018-05-07 11:39:31 -0700 |
---|---|---|
committer | Jeff Davidson <jpd@google.com> | 2018-05-10 20:11:47 +0000 |
commit | 9a128540048414c78a2be9524f93b6234c640b3a (patch) | |
tree | f39fa51c442a9c6922d1f6def2e75d5dca677f20 /src/main/java/com/android/volley/CacheDispatcher.java | |
parent | aee0d184daa41c0abb5bf88580ad6cf7fea63d72 (diff) | |
download | volley-9a128540048414c78a2be9524f93b6234c640b3a.tar.gz |
Import of Volley from GitHub to AOSP.android-o-mr1-iot-release-1.0.1
Adds android-support-annotations as a runtime dependency in Android.mk.
- 28588322fa4eca3c1a7922b8a2f7b4a97d5c099a Fix concurrent modification errors in ImageContainer. (#1... by Jeff Davidson <jpd236@cornell.edu>
- ba3b40457a97b3b3812cb00191b41cb0af4149df Fix unchecked operations in Volley build. (#178) by Jeff Davidson <jpd236@cornell.edu>
- 62c19019e43a76ba4a084b25548e7fb1cfd4a457 Always close HttpURLConnections in HurlStack. (#176) by Jeff Davidson <jpd236@cornell.edu>
- 36c80f25a78cf2ba2feb2d697f12f9bbca96bf43 Port dispatcher tests to mockito. (#172) by Jeff Davidson <jpd236@cornell.edu>
- 15b55093fc8ac86902a745f46a2bdea825e797a3 Cleaning up parameter comments + unnecessary generics. (#... by Jeff Davidson <jpd236@cornell.edu>
- 6b341755e522f23de149544536b9b69c7b7b8305 Remove all wildcard imports. (#170) by Jeff Davidson <jpd236@cornell.edu>
- a8424005b3ba6d17fc9e123a2176da702f3ead10 Use actual annotations instead of comments. (#169) by Jeff Davidson <jpd236@cornell.edu>
- bdc80555aee738235bd33c26878bbe8ce382d795 Apply google-java-format to all code. (#165) by Jeff Davidson <jpd236@cornell.edu>
- 5307293b15dd0c32f53b8f1fd8c198caa443982e Integrate Volley builds with errorprone. (#162) by Jeff Davidson <jpd236@cornell.edu>
- fa586029500e6813d654f1b454cd3345b0a43897 Fail Travis builds if snapshot uploading fails. (#163) by Jeff Davidson <jpd236@cornell.edu>
- 0ebb97e9b0192e879088b38b5992158e99364dc6 Stop publishing bintray-info.json. (#141) by Jeff Davidson <jpd236@cornell.edu>
- b89dfbd2e6964acfe5561e268300611a83957a15 fix: request time contains RequestQueue blocking time. (#... by dezng <dezng123@gmail.com>
- c2bfd86596e588c924c2c7e0ae0940053800af5c Guard against illegal argument and OOM exceptions in read... by Joe Bowbeer <joe.bowbeer@gmail.com>
- fc8ff0423fe9a3af6595088b34f912fb2d920ffb Log fix (#112) by Navid Ht <navid.htb@gmail.com>
- 47586f0fd42acd5ce09dfc767387df685edfa547 Bump version to 1.1.1-SNAPSHOT. (#123) by Jeff Davidson <jpd236@cornell.edu>
- 1392f961af17d0f5ba367d8d199a18e5325b882e Update deploy credentials. (#122) by Jeff Davidson <jpd236@cornell.edu>
- ffe9281709f38f84492ce898603d3ec084326a57 Set project group and version. (#120) by Jeff Davidson <jpd236@cornell.edu>
GitOrigin-RevId: 28588322fa4eca3c1a7922b8a2f7b4a97d5c099a
Change-Id: Ifeea069df2f0a5fd10bc3d4686601c88906ee653
Diffstat (limited to 'src/main/java/com/android/volley/CacheDispatcher.java')
-rw-r--r-- | src/main/java/com/android/volley/CacheDispatcher.java | 85 |
1 files changed, 49 insertions, 36 deletions
diff --git a/src/main/java/com/android/volley/CacheDispatcher.java b/src/main/java/com/android/volley/CacheDispatcher.java index cd3635d..4ea8a0b 100644 --- a/src/main/java/com/android/volley/CacheDispatcher.java +++ b/src/main/java/com/android/volley/CacheDispatcher.java @@ -17,7 +17,7 @@ package com.android.volley; import android.os.Process; - +import android.support.annotation.VisibleForTesting; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -27,11 +27,10 @@ import java.util.concurrent.BlockingQueue; /** * Provides a thread for performing cache triage on a queue of requests. * - * Requests added to the specified cache queue are resolved from cache. - * Any deliverable response is posted back to the caller via a - * {@link ResponseDelivery}. Cache misses and responses that require - * refresh are enqueued on the specified network queue for processing - * by a {@link NetworkDispatcher}. + * <p>Requests added to the specified cache queue are resolved from cache. Any deliverable response + * is posted back to the caller via a {@link ResponseDelivery}. Cache misses and responses that + * require refresh are enqueued on the specified network queue for processing by a {@link + * NetworkDispatcher}. */ public class CacheDispatcher extends Thread { @@ -56,8 +55,8 @@ public class CacheDispatcher extends Thread { private final WaitingRequestManager mWaitingRequestManager; /** - * Creates a new cache triage dispatcher thread. You must call {@link #start()} - * in order to begin processing. + * Creates a new cache triage dispatcher thread. You must call {@link #start()} in order to + * begin processing. * * @param cacheQueue Queue of incoming requests for triage * @param networkQueue Queue to post requests that require network to @@ -65,8 +64,10 @@ public class CacheDispatcher extends Thread { * @param delivery Delivery interface to use for posting responses */ public CacheDispatcher( - BlockingQueue<Request<?>> cacheQueue, BlockingQueue<Request<?>> networkQueue, - Cache cache, ResponseDelivery delivery) { + BlockingQueue<Request<?>> cacheQueue, + BlockingQueue<Request<?>> networkQueue, + Cache cache, + ResponseDelivery delivery) { mCacheQueue = cacheQueue; mNetworkQueue = networkQueue; mCache = cache; @@ -75,8 +76,8 @@ public class CacheDispatcher extends Thread { } /** - * Forces this dispatcher to quit immediately. If any requests are still in - * the queue, they are not guaranteed to be processed. + * Forces this dispatcher to quit immediately. If any requests are still in the queue, they are + * not guaranteed to be processed. */ public void quit() { mQuit = true; @@ -111,6 +112,11 @@ public class CacheDispatcher extends Thread { // Get a request from the cache triage queue, blocking until // at least one is available. final Request<?> request = mCacheQueue.take(); + processRequest(request); + } + + @VisibleForTesting + void processRequest(final Request<?> request) throws InterruptedException { request.addMarker("cache-queue-take"); // If the request has been canceled, don't bother dispatching it. @@ -142,8 +148,9 @@ public class CacheDispatcher extends Thread { // We have a cache hit; parse its data for delivery back to the request. request.addMarker("cache-hit"); - Response<?> response = request.parseNetworkResponse( - new NetworkResponse(entry.data, entry.responseHeaders)); + Response<?> response = + request.parseNetworkResponse( + new NetworkResponse(entry.data, entry.responseHeaders)); request.addMarker("cache-hit-parsed"); if (!entry.refreshNeeded()) { @@ -161,17 +168,20 @@ public class CacheDispatcher extends Thread { if (!mWaitingRequestManager.maybeAddToWaitingRequests(request)) { // Post the intermediate response back to the user and have // the delivery then forward the request along to the network. - mDelivery.postResponse(request, response, new Runnable() { - @Override - public void run() { - try { - mNetworkQueue.put(request); - } catch (InterruptedException e) { - // Restore the interrupted status - Thread.currentThread().interrupt(); - } - } - }); + mDelivery.postResponse( + request, + response, + new Runnable() { + @Override + public void run() { + try { + mNetworkQueue.put(request); + } catch (InterruptedException e) { + // Restore the interrupted status + Thread.currentThread().interrupt(); + } + } + }); } else { // request has been added to list of waiting requests // to receive the network response from the first request once it returns. @@ -186,10 +196,10 @@ public class CacheDispatcher extends Thread { * Staging area for requests that already have a duplicate request in flight. * * <ul> - * <li>containsKey(cacheKey) indicates that there is a request in flight for the given cache - * key.</li> - * <li>get(cacheKey) returns waiting requests for the given cache key. The in flight request - * is <em>not</em> contained in that list. Is null if no requests are staged.</li> + * <li>containsKey(cacheKey) indicates that there is a request in flight for the given + * cache key. + * <li>get(cacheKey) returns waiting requests for the given cache key. The in flight + * request is <em>not</em> contained in that list. Is null if no requests are staged. * </ul> */ private final Map<String, List<Request<?>>> mWaitingRequests = new HashMap<>(); @@ -214,7 +224,8 @@ public class CacheDispatcher extends Thread { } if (waitingRequests != null) { if (VolleyLog.DEBUG) { - VolleyLog.v("Releasing %d waiting requests for cacheKey=%s.", + VolleyLog.v( + "Releasing %d waiting requests for cacheKey=%s.", waitingRequests.size(), cacheKey); } // Process all queued up requests. @@ -231,7 +242,8 @@ public class CacheDispatcher extends Thread { List<Request<?>> waitingRequests = mWaitingRequests.remove(cacheKey); if (waitingRequests != null && !waitingRequests.isEmpty()) { if (VolleyLog.DEBUG) { - VolleyLog.v("%d waiting requests for cacheKey=%s; resend to network", + VolleyLog.v( + "%d waiting requests for cacheKey=%s; resend to network", waitingRequests.size(), cacheKey); } Request<?> nextInLine = waitingRequests.remove(0); @@ -250,11 +262,12 @@ public class CacheDispatcher extends Thread { } /** - * For cacheable requests, if a request for the same cache key is already in flight, - * add it to a queue to wait for that in-flight request to finish. + * For cacheable requests, if a request for the same cache key is already in flight, add it + * to a queue to wait for that in-flight request to finish. + * * @return whether the request was queued. If false, we should continue issuing the request - * over the network. If true, we should put the request on hold to be processed when - * the in-flight request finishes. + * over the network. If true, we should put the request on hold to be processed when the + * in-flight request finishes. */ private synchronized boolean maybeAddToWaitingRequests(Request<?> request) { String cacheKey = request.getCacheKey(); @@ -264,7 +277,7 @@ public class CacheDispatcher extends Thread { // There is already a request in flight. Queue up. List<Request<?>> stagedRequests = mWaitingRequests.get(cacheKey); if (stagedRequests == null) { - stagedRequests = new ArrayList<Request<?>>(); + stagedRequests = new ArrayList<>(); } request.addMarker("waiting-for-response"); stagedRequests.add(request); |