-
- All Implemented Interfaces:
-
com.facebook.imagepipeline.producers.NetworkFetcher
public class PriorityNetworkFetcher<FETCH_STATE extends FetchState> implements NetworkFetcher<PriorityNetworkFetcher.PriorityFetchState<FETCH_STATE>>
PriorityNetworkFetcher fetches images using a priority queue.
Fetches are delegated to another fetcher.
- Two queues are maintained, one for each priority.
- High-priority images (e.g, on-screen) are handled FIFO or LIFO, depending on a flag.
- Low-priority images (e.g., prefetches) are handled FIFO.
- Dequeuing is done thusly:
- If there's an enqueued hi-pri requests, and there are less than 'maxOutstandingHiPri' currently active downloads, it is dequeued; then,
- If there's an enqueued low-pri requests, and there are less than 'maxOutstandingLowPri' currently active downloads, it is dequeued.
- When a request's priority changes, it is taken out of the queue and re-enqueued according to the rules above.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public classPriorityNetworkFetcher.PriorityFetchStatepublic classPriorityNetworkFetcher.NonrecoverableExceptionThe delegate fetcher may pass an instance of this exception to its callback's onFailure tosignal to a PriorityNetworkFetcher that it shouldn't retry that request.
This is useful for e.g., requests that fail due to HTTP 403: there's no point in retryingthem, usually.
public classPriorityNetworkFetcher.RetriableIOExceptionRetriableIOException should be returned by NetworkFetcher's that would like to signal toPriorityNetworkFetcher that a failure is
-
Constructor Summary
Constructors Constructor Description PriorityNetworkFetcher(NetworkFetcher<FETCH_STATE> delegate, boolean isHiPriFifo, int maxOutstandingHiPri, int maxOutstandingLowPri, boolean inflightFetchesCanBeCancelled, int maxNumberOfRequeue, boolean doNotCancelRequests, int immediateRequeueCount, int requeueDelayTimeInMillis, boolean multipleDequeue, boolean nonRecoverableExceptionPreventsRequeue, int maxConnectAttemptCount, int maxAttemptCount, boolean retryLowPriAll, boolean retryLowPriUnknownHostException, boolean retryLowPriConnectionException)PriorityNetworkFetcher(NetworkFetcher<FETCH_STATE> delegate, boolean isHiPriFifo, int maxOutstandingHiPri, int maxOutstandingLowPri, boolean inflightFetchesCanBeCancelled, int maxNumberOfRequeue, boolean doNotCancelRequests, int immediateRequeueCount, int requeueDelayTimeInMillis, boolean multipleDequeue, boolean nonRecoverableExceptionPreventsRequeue, int maxConnectAttemptCount, int maxAttemptCount, boolean retryLowPriAll, boolean retryLowPriUnknownHostException, boolean retryLowPriConnectionException, MonotonicClock clock)
-
Method Summary
Modifier and Type Method Description voidpause()Stop dequeuing requests until resume is called. voidresume()Resume dequeuing requests. voidfetch(PriorityNetworkFetcher.PriorityFetchState<FETCH_STATE> fetchState, NetworkFetcher.Callback callback)Initiates the network fetch and informs the producer when a response is received via theprovided callback. voidonFetchCompletion(PriorityNetworkFetcher.PriorityFetchState<FETCH_STATE> fetchState, int byteSize)Called after the fetch completes. PriorityNetworkFetcher.PriorityFetchState<FETCH_STATE>createFetchState(Consumer<EncodedImage> consumer, ProducerContext producerContext)Creates a new instance of the FetchState-derived object used to store state. booleanshouldPropagate(PriorityNetworkFetcher.PriorityFetchState<FETCH_STATE> fetchState)Gets whether the intermediate results should be propagated. Map<String, String>getExtraMap(PriorityNetworkFetcher.PriorityFetchState<FETCH_STATE> fetchState, int byteSize)Gets a map containing extra parameters to pass to the listeners. static booleanshouldRetry(Throwable e, int attemptNumber, int maxConnectAttemptCount, int maxAttemptCount, boolean retryLowPriAll, boolean retryLowPriUnknownHostException, boolean retryLowPriConnectionException, Priority priority)-
-
Constructor Detail
-
PriorityNetworkFetcher
PriorityNetworkFetcher(NetworkFetcher<FETCH_STATE> delegate, boolean isHiPriFifo, int maxOutstandingHiPri, int maxOutstandingLowPri, boolean inflightFetchesCanBeCancelled, int maxNumberOfRequeue, boolean doNotCancelRequests, int immediateRequeueCount, int requeueDelayTimeInMillis, boolean multipleDequeue, boolean nonRecoverableExceptionPreventsRequeue, int maxConnectAttemptCount, int maxAttemptCount, boolean retryLowPriAll, boolean retryLowPriUnknownHostException, boolean retryLowPriConnectionException)
- Parameters:
isHiPriFifo- if true, hi-pri requests are dequeued in the order they were enqueued.Otherwise, they're dequeued in reverse order.inflightFetchesCanBeCancelled- if false, the fetcher waits for the completion of requeststhat have been delegated to 'delegate' even if they were cancelled by Fresco.maxNumberOfRequeue- requests that fail are re-queued up to maxNumberOfRequeue times,potentially retrying immediately.doNotCancelRequests- if true, requests will not be removed from the queue on cancellationimmediateRequeueCount- number of immediate requeue attempts, when a request is re-queuedmore then this number, it will get re-queued again after at most requeueDelayTimeInMillis.NO_DELAYED_REQUESTS(-1) means no delay, no matter how many times the request is re-queued.requeueDelayTimeInMillis- the amount of time in ms a request needs to wait until gettingre-queued again after it gets re-queued more than immediateRequeueCount times.
-
PriorityNetworkFetcher
PriorityNetworkFetcher(NetworkFetcher<FETCH_STATE> delegate, boolean isHiPriFifo, int maxOutstandingHiPri, int maxOutstandingLowPri, boolean inflightFetchesCanBeCancelled, int maxNumberOfRequeue, boolean doNotCancelRequests, int immediateRequeueCount, int requeueDelayTimeInMillis, boolean multipleDequeue, boolean nonRecoverableExceptionPreventsRequeue, int maxConnectAttemptCount, int maxAttemptCount, boolean retryLowPriAll, boolean retryLowPriUnknownHostException, boolean retryLowPriConnectionException, MonotonicClock clock)
-
-
Method Detail
-
resume
void resume()
Resume dequeuing requests.
Note: a request is immediately dequeued and the delegate's fetch() method is called usingthe current thread.
-
fetch
void fetch(PriorityNetworkFetcher.PriorityFetchState<FETCH_STATE> fetchState, NetworkFetcher.Callback callback)
Initiates the network fetch and informs the producer when a response is received via theprovided callback.
- Parameters:
fetchState- the fetch-specific statecallback- the callback used to inform the network fetch producer
-
onFetchCompletion
void onFetchCompletion(PriorityNetworkFetcher.PriorityFetchState<FETCH_STATE> fetchState, int byteSize)
Called after the fetch completes.
Implementing this method is optional and is useful for instrumentation purposes.
- Parameters:
fetchState- the fetch-specific statebyteSize- size of the data in bytes
-
createFetchState
PriorityNetworkFetcher.PriorityFetchState<FETCH_STATE> createFetchState(Consumer<EncodedImage> consumer, ProducerContext producerContext)
Creates a new instance of the FetchState-derived object used to store state.
- Parameters:
consumer- the consumerproducerContext- the producer's context
-
shouldPropagate
boolean shouldPropagate(PriorityNetworkFetcher.PriorityFetchState<FETCH_STATE> fetchState)
Gets whether the intermediate results should be propagated.
In addition to the requirements of this method, intermediate results are throttled sothat a maximum of one every 100 ms is propagated. This is to conserve CPU and other resources.
Not applicable if progressive rendering is disabled or not supported for this image.
- Parameters:
fetchState- the fetch-specific state
-
getExtraMap
@Nullable() Map<String, String> getExtraMap(PriorityNetworkFetcher.PriorityFetchState<FETCH_STATE> fetchState, int byteSize)
Gets a map containing extra parameters to pass to the listeners.
Returning map is optional and is useful for instrumentation purposes.
This map won't be modified by the caller.
- Parameters:
fetchState- the fetch-specific statebyteSize- size of the data in bytes
-
shouldRetry
static boolean shouldRetry(Throwable e, int attemptNumber, int maxConnectAttemptCount, int maxAttemptCount, boolean retryLowPriAll, boolean retryLowPriUnknownHostException, boolean retryLowPriConnectionException, Priority priority)
-
-
-
-