coil-kt / coil

Image loading for Android and Compose Multiplatform.
https://coil-kt.github.io/coil/
Apache License 2.0
10.86k stars 668 forks source link

Handle http error 429 too many requests #1227

Closed LunarX closed 2 years ago

LunarX commented 2 years ago

Describe the bug We're using coil version 2.0.0-rc02. We use coil to display thumbnails in an image gallery. When we use the fast scrollbar to browse images, we sometimes receive an http error 429 "too many requests" from our backend because we loaded too many thumbnails in a short amount of time.

To Reproduce

Logs/Screenshots

I/RealImageLoader: 🚨 Failed - https://... - coil.network.HttpException: HTTP 429: 
2022-04-06 14:43:56.627 E/RealImageLoader: coil.network.HttpException: HTTP 429: 
        at coil.fetch.HttpUriFetcher.executeNetworkRequest(HttpUriFetcher.kt:227)
        at coil.fetch.HttpUriFetcher.access$executeNetworkRequest(HttpUriFetcher.kt:30)
        at coil.fetch.HttpUriFetcher$executeNetworkRequest$1.invokeSuspend(Unknown Source:15)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:39)
        at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
        at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)

illustration

The failed pictures display our "default thumbnail"

Version 2.0.0-rc02

colinrtwhite commented 2 years ago

There's no clear action to take here as this is an error triggered by your backend due to launching too many image requests in a short period of time. What's the expected behaviour here?

sirambd commented 2 years ago

@colinrtwhite Normally coil is supposed to stop being effective when working with the recyclerview. In case of normal use with a recyclerview there is no problem. But here when we scroll very quickly, coil launches several calls in a row, hence the exception, so this management is not managed by coil at all. So what would be interesting would be to have a way for coil to avoid launching several calls in a recyclerview that is scrolling, and that simply launches when you stay on the view. That way there won't be any problem.

colinrtwhite commented 2 years ago

@sirambd This sounds like a good candidate for a custom Interceptor. You can suspend, delay, or cancel calls based on custom logic.

sirambd commented 2 years ago

Okay I understand @colinrtwhite but I think it's a pity that coil doesn't do it automatically, as I didn't have this problem with glide. It would be interesting to have an extension for recyclerview that limits the loading from remote when scrolling.

JorisBodin commented 2 years ago

Okay I understand @colinrtwhite but I think it's a pity that coil doesn't do it automatically, as I didn't have this problem with glide. It would be interesting to have an extension for recyclerview that limits the loading from remote when scrolling.

I also find it interesting