Glide: Make it easy to customize request timeouts

Created on 21 Apr 2015  Âˇ  11Comments  Âˇ  Source: bumptech/glide

I converted my entire app to use Glide as the image download engine and Ok as the network engine. Works great with far fewer memory issues, but having problems with what appears to be timeouts. In general downloads happen, but about 1 in 5 images just never get downloaded due to the following exception. It appears to be timeout issues, is there a way to adjust timeouts? I would think there would be as this is a problem that can come from slow hosts. Here is the exception log if that is something of interest:

04-21 00:09:50.296 E/com.newrelic.agent.androidīš• java.net.SocketTimeoutException
04-21 00:09:50.296 I/System.outīš• NOTIFY STREAM ERROR: java.net.SocketTimeoutException
04-21 00:09:50.296 W/System.errīš• java.net.SocketTimeoutException
04-21 00:09:50.296 E/com.newrelic.agent.androidīš• java.net.SocketTimeoutException
04-21 00:09:50.296 I/System.outīš• NOTIFY STREAM ERROR: java.net.SocketTimeoutException
04-21 00:09:50.296 W/System.errīš• java.net.SocketTimeoutException
04-21 00:09:50.308 W/System.errīš• at   at java.net.PlainSocketImpl.read(PlainSocketImpl.java:491)
04-21 00:09:50.308 W/System.errīš• at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
04-21 00:09:50.308 W/System.errīš• at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
04-21 00:09:50.308 W/System.errīš• at java.io.BufferedInputStream.read(BufferedInputStream.java:283)
04-21 00:09:50.308 W/System.errīš• at com.android.okhttp.internal.http.HttpTransport$FixedLengthInputStream.read(HttpTransport.java:386)
04-21 00:09:50.308 W/System.errīš• at com.newrelic.agent.android.instrumentation.io.CountingInputStream.read(CountingInputStream.java:118)
04-21 00:09:50.312 W/System.errīš• at com.bumptech.glide.load.resource.bitmap.RecyclableBufferedInputStream.fillbuf(RecyclableBufferedInputStream.java:132)
04-21 00:09:50.312 W/System.errīš• at com.bumptech.glide.load.resource.bitmap.RecyclableBufferedInputStream.read(RecyclableBufferedInputStream.java:309)
04-21 00:09:50.312 W/System.errīš• at com.bumptech.glide.load.resource.bitmap.RecyclableBufferedInputStream.fillbuf(RecyclableBufferedInputStream.java:166)
04-21 00:09:50.312 W/System.errīš• at com.bumptech.glide.load.resource.bitmap.RecyclableBufferedInputStream.read(RecyclableBufferedInputStream.java:309)
04-21 00:09:50.312 W/System.errīš• at com.bumptech.glide.util.ExceptionCatchingInputStream.read(ExceptionCatchingInputStream.java:84)
04-21 00:09:50.312 W/System.errīš• at java.io.FilterInputStream.read(FilterInputStream.java:118)
04-21 00:09:50.312 W/System.errīš• at com.bumptech.glide.util.MarkEnforcingInputStream.read(MarkEnforcingInputStream.java:44)
04-21 00:09:50.312 W/System.errīš• at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
04-21 00:09:50.312 W/System.errīš• at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:620)
04-21 00:09:50.312 W/System.errīš• at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
04-21 00:09:50.312 W/System.errīš• at com.newrelic.agent.android.instrumentation.BitmapFactoryInstrumentation.decodeStream(BitmapFactoryInstrumentation.java:91)
04-21 00:09:50.312 W/System.errīš• at com.bumptech.glide.load.resource.bitmap.Downsampler.decodeStream(Downsampler.java:325)
04-21 00:09:50.312 W/System.errīš• at com.bumptech.glide.load.resource.bitmap.Downsampler.downsampleWithSize(Downsampler.java:217)
04-21 00:09:50.312 W/System.errīš• at com.bumptech.glide.load.resource.bitmap.Downsampler.decode(Downsampler.java:151)
04-21 00:09:50.312 W/System.errīš• at com.bumptech.glide.load.resource.bitmap.StreamBitmapDecoder.decode(StreamBitmapDecoder.java:50)
04-21 00:09:50.312 W/System.errīš• at com.bumptech.glide.load.resource.bitmap.StreamBitmapDecoder.decode(StreamBitmapDecoder.java:19)
04-21 00:09:50.312 W/System.errīš• at com.bumptech.glide.load.resource.bitmap.ImageVideoBitmapDecoder.decode(ImageVideoBitmapDecoder.java:39)
04-21 00:09:50.312 W/System.errīš• at com.bumptech.glide.load.resource.bitmap.ImageVideoBitmapDecoder.decode(ImageVideoBitmapDecoder.java:20)
04-21 00:09:50.312 W/System.errīš• at com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapperResourceDecoder.decodeBitmapWrapper(GifBitmapWrapperResourceDecoder.java:121)
04-21 00:09:50.312 W/System.errīš• at com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapperResourceDecoder.decodeStream(GifBitmapWrapperResourceDecoder.java:94)
04-21 00:09:50.312 W/System.errīš• at com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapperResourceDecoder.decode(GifBitmapWrapperResourceDecoder.java:71)
04-21 00:09:50.312 W/System.errīš• at com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapperResourceDecoder.decode(GifBitmapWrapperResourceDecoder.java:61)
04-21 00:09:50.312 W/System.errīš• at com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapperResourceDecoder.decode(GifBitmapWrapperResourceDecoder.java:22)
04-21 00:09:50.312 W/System.errīš• at com.bumptech.glide.load.engine.DecodeJob.decodeFromSourceData(DecodeJob.java:190)
04-21 00:09:50.312 W/System.errīš• at com.bumptech.glide.load.engine.DecodeJob.decodeSource(DecodeJob.java:177)
04-21 00:09:50.312 W/System.errīš• at com.bumptech.glide.load.engine.DecodeJob.decodeFromSource(DecodeJob.java:128)
04-21 00:09:50.312 W/System.errīš• at com.bumptech.glide.load.engine.EngineRunnable.decodeFromSource(EngineRunnable.java:122)
04-21 00:09:50.312 W/System.errīš• at com.bumptech.glide.load.engine.EngineRunnable.decode(EngineRunnable.java:101)
04-21 00:09:50.312 W/System.errīš• at com.bumptech.glide.load.engine.EngineRunnable.run(EngineRunnable.java:58)
04-21 00:09:50.312 W/System.errīš• at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
04-21 00:09:50.312 W/System.errīš• at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-21 00:09:50.312 W/System.errīš• at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-21 00:09:50.312 W/System.errīš• at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-21 00:09:50.312 W/System.errīš• at java.lang.Thread.run(Thread.java:841)
04-21 00:09:50.312 W/System.errīš• at com.bumptech.glide.load.engine.executor.FifoPriorityThreadPoolExecutor$DefaultThreadFactory$1.run(FifoPriorityThreadPoolExecutor.java:52)
enhancement question

Most helpful comment

If you're using OkHttp:
https://github.com/bumptech/glide/wiki/Integration-Libraries#okhttp

You can set custom connection and read timeouts:
http://stackoverflow.com/questions/25953819/how-to-set-connection-timeout-with-okhttp

You can pass in a custom OkHttpClient to a OkHttpUrlLoader.Factory here: https://github.com/bumptech/glide/blob/3.0/integration/okhttp/src/main/java/com/bumptech/glide/integration/okhttp/OkHttpUrlLoader.java#L47.

You can then register your factory in a custom GlideModule, per the instructions here: https://github.com/bumptech/glide/wiki/Configuration#including-a-glidemodule

Note that if you're using Gradle, you will have to exclude the metadata for the default OkHttp module:
https://github.com/bumptech/glide/wiki/Configuration#conflicting-glidemodules

All 11 comments

If you're using OkHttp:
https://github.com/bumptech/glide/wiki/Integration-Libraries#okhttp

You can set custom connection and read timeouts:
http://stackoverflow.com/questions/25953819/how-to-set-connection-timeout-with-okhttp

You can pass in a custom OkHttpClient to a OkHttpUrlLoader.Factory here: https://github.com/bumptech/glide/blob/3.0/integration/okhttp/src/main/java/com/bumptech/glide/integration/okhttp/OkHttpUrlLoader.java#L47.

You can then register your factory in a custom GlideModule, per the instructions here: https://github.com/bumptech/glide/wiki/Configuration#including-a-glidemodule

Note that if you're using Gradle, you will have to exclude the metadata for the default OkHttp module:
https://github.com/bumptech/glide/wiki/Configuration#conflicting-glidemodules

If anyone wants to take this on, it would be pretty easy in the master branch (Glide v4) to add timeout Options to the various networking integrations.

Can't find OkHttpUrlLoader.Factory in Glide v3.7.0.

I got to include compile 'com.github.bumptech.glide:okhttp3-integration:1.4.0@aar'. Then i did

public class MyGlideModule implements GlideModule {
    @Override public void applyOptions(Context context, GlideBuilder builder) {
        // Apply options to the builder here.
    }

    @Override public void registerComponents(Context context, Glide glide) {
        // register ModelLoaders here.
        OkHttpClient.Builder client = new OkHttpClient.Builder()
                .connectTimeout(45, TimeUnit.SECONDS)
                .readTimeout(45, TimeUnit.SECONDS)
                .writeTimeout(45, TimeUnit.SECONDS);

        OkHttpUrlLoader.Factory factory=new OkHttpUrlLoader.Factory(client.build());

        glide.register(GlideUrl.class, InputStream.class, factory);
    }
}

 <meta-data
            android:name="com.my.package.helper.MyGlideModule"
            android:value="GlideModule" />

It does not seem working

@jemshit keep looking, it's there. Make sure you're pairing okhttp3-integration with okhttp3 classes (double-check your imports). If you can't find it please specifc what "Can't find" means, either: compile error for import, compile error for constructor, dexing error, ClassNotFoundException, GlideModule not executed, IDEA not seeing classes, whatever else your symptom is...

I also recommend using @jar instead of @aar or explicitly excluding the module inside, see Integration libraries and Configuration wikis.

Hi,

Is the issue is resolved , i am also facing same timeout exception.

@AshishBansalCompiler No, it's not. It's in open state. Current solution is to configure a networking lib: https://github.com/bumptech/glide/issues/432#issuecomment-94830529. See also related issues for more info.

Hi @TWiStErRob, how long is the default timeout for Glide? I couldn't find anything on the documentation.

@yolapop https://github.com/bumptech/glide/blob/3.0/library/src/main/java/com/bumptech/glide/load/data/HttpUrlFetcher.java#L66-L67 or whatever OkHttp's default is when using an integration library, in that case it's not configured by Glide, hence the need for custom module like https://github.com/bumptech/glide/issues/432#issuecomment-242045406.

@TWiStErRob
Without using libraries like OkHttp or Volley, is there a way to override connection timeout values?

It looks like most libraries set read timeouts per client, rather than per request. We've added an Option in Glide v4 to the default HttpGlideUrlLoader which will let you set timeouts if you don't use a networking library.

Was this page helpful?
0 / 5 - 0 ratings