Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix item wrongly removed from http cache when error in subscriptions #4537

Merged
merged 5 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ class CachingHttpInterceptor(
*/
const val CACHE_KEY_HEADER = "X-APOLLO-CACHE-KEY"

internal const val REQUEST_UUID_HEADER = "X-APOLLO-REQUEST-UUID"

/**
* Cache fetch strategy http header
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import com.apollographql.apollo3.api.Query
import com.apollographql.apollo3.api.Subscription
import com.apollographql.apollo3.api.http.HttpRequest
import com.apollographql.apollo3.api.http.HttpResponse
import com.apollographql.apollo3.api.http.valueOf
import com.apollographql.apollo3.interceptor.ApolloInterceptor
import com.apollographql.apollo3.interceptor.ApolloInterceptorChain
import com.apollographql.apollo3.network.http.HttpInfo
import com.apollographql.apollo3.network.http.HttpInterceptor
import com.apollographql.apollo3.network.http.HttpInterceptorChain
import com.apollographql.apollo3.network.http.HttpNetworkTransport
import com.benasher44.uuid.Uuid
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.onEach
Expand Down Expand Up @@ -74,18 +74,19 @@ fun ApolloClient.Builder.httpCache(
maxSize = maxSize,
)
val apolloRequestToCacheKey = mutableMapOf<String, String>()
var apolloRequestUuid: Uuid? = null
return addHttpInterceptor(object : HttpInterceptor {
override suspend fun intercept(request: HttpRequest, chain: HttpInterceptorChain): HttpResponse {
val cacheKey = CachingHttpInterceptor.cacheKey(request)
apolloRequestToCacheKey[apolloRequestUuid!!.toString()] = cacheKey
val requestUuid = request.headers.valueOf(CachingHttpInterceptor.REQUEST_UUID_HEADER)!!
synchronized(apolloRequestToCacheKey) {
apolloRequestToCacheKey[requestUuid] = cacheKey
}
return chain.proceed(request.newBuilder().addHeader(CachingHttpInterceptor.CACHE_KEY_HEADER, cacheKey).build())
martinbonnin marked this conversation as resolved.
Show resolved Hide resolved
}
}).addHttpInterceptor(
cachingHttpInterceptor
).addInterceptor(object : ApolloInterceptor {
override fun <D : Operation.Data> intercept(request: ApolloRequest<D>, chain: ApolloInterceptorChain): Flow<ApolloResponse<D>> {
apolloRequestUuid = request.requestUuid
val policy = request.executionContext[HttpFetchPolicyContext]?.httpFetchPolicy ?: defaultPolicy(request.operation)
val policyStr = when (policy) {
HttpFetchPolicy.CacheFirst -> CachingHttpInterceptor.CACHE_FIRST
Expand All @@ -106,11 +107,12 @@ fun ApolloClient.Builder.httpCache(
}
)
.addHttpHeader(CachingHttpInterceptor.CACHE_FETCH_POLICY_HEADER, policyStr)
.addHttpHeader(CachingHttpInterceptor.REQUEST_UUID_HEADER, request.requestUuid.toString())
.build()
)
.run {
if (request.operation is Query<*> || request.operation is Mutation<*>) {
val cacheKey = apolloRequestToCacheKey.remove(request.requestUuid.toString())
val cacheKey = synchronized(apolloRequestToCacheKey) { apolloRequestToCacheKey.remove(request.requestUuid.toString()) }
catch { throwable ->
// Revert caching of responses with errors
cacheKey?.let { cachingHttpInterceptor.cache.remove(it) }
Expand Down