@@ -11,19 +11,29 @@ import java.util.concurrent.TimeUnit
1111import kotlinx.coroutines.CoroutineScope
1212import kotlinx.coroutines.Dispatchers
1313import kotlinx.coroutines.SupervisorJob
14+ import okhttp3.ConnectionPool
1415import okhttp3.OkHttpClient
1516
1617internal class DependencyContainer private constructor(
1718 private val application : Application
1819) {
1920 private val okHttpClient: OkHttpClient by lazy {
21+ // Connection pool: max 50 idle connections, keep alive for 5 minutes
22+ // This allows many concurrent chunk downloads across multiple files
23+ val connectionPool = ConnectionPool (50 , 5 , TimeUnit .MINUTES )
24+
2025 OkHttpClient .Builder ()
2126 .connectTimeout(Constants .DEFAULT_CONNECT_TIMEOUT_SECONDS , TimeUnit .SECONDS )
22- .readTimeout(Constants .DEFAULT_READ_TIMEOUT_SECONDS , TimeUnit .SECONDS )
23- .callTimeout(
24- Constants .DEFAULT_READ_TIMEOUT_SECONDS * 2 ,
25- TimeUnit .SECONDS
26- )
27+ // Very generous read timeout for large chunks and slow connections
28+ // For 8MB chunk: at 0.1MB/s = 80s, at 0.05MB/s = 160s
29+ // 10 minutes provides ample margin for very slow connections and network hiccups
30+ .readTimeout(10 , TimeUnit .MINUTES )
31+ // Write timeout for slow uploads (though we're downloading)
32+ .writeTimeout(5 , TimeUnit .MINUTES )
33+ // Call timeout should be much longer than read timeout to allow for retries
34+ // 30 minutes allows for multiple retries even on very slow connections
35+ .callTimeout(30 , TimeUnit .MINUTES )
36+ .connectionPool(connectionPool)
2737 .retryOnConnectionFailure(true )
2838 .build()
2939 }
0 commit comments