Skip to content

Commit

Permalink
feat: execute graphQLRequest out of reactor netty threads ( cherry pick
Browse files Browse the repository at this point in the history
#1943) (#1945)

### 📝 Description
as soon as request was deserialized, move execution of operation out of
the thread that routed the request (reactor netty http epoll thread in
case of spring).
  • Loading branch information
samuelAndalon committed Apr 2, 2024
1 parent 65aa3f0 commit a937b8d
Showing 1 changed file with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ package com.expediagroup.graphql.server.execution
import com.expediagroup.graphql.server.types.GraphQLResponse
import com.expediagroup.graphql.server.types.GraphQLServerResponse
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.withContext
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext

Expand All @@ -46,19 +48,21 @@ open class GraphQLServer<Request>(
): GraphQLServerResponse? =
coroutineScope {
requestParser.parseRequest(request)?.let { graphQLRequest ->
val deprecatedContext = contextFactory.generateContext(request)
val contextMap = contextFactory.generateContextMap(request)
withContext(Dispatchers.Default) {
val deprecatedContext = contextFactory.generateContext(request)
val contextMap = contextFactory.generateContextMap(request)

val customCoroutineContext = (deprecatedContext?.graphQLCoroutineContext() ?: EmptyCoroutineContext) +
(contextMap[CoroutineContext::class] as? CoroutineContext ?: EmptyCoroutineContext)
val graphQLExecutionScope = CoroutineScope(
coroutineContext + customCoroutineContext + SupervisorJob()
)
val graphQLContext = contextMap + mapOf(
CoroutineScope::class to graphQLExecutionScope
)
val customCoroutineContext = (deprecatedContext?.graphQLCoroutineContext() ?: EmptyCoroutineContext) +
(contextMap[CoroutineContext::class] as? CoroutineContext ?: EmptyCoroutineContext)
val graphQLExecutionScope = CoroutineScope(
coroutineContext + customCoroutineContext + SupervisorJob()
)
val graphQLContext = contextMap + mapOf(
CoroutineScope::class to graphQLExecutionScope
)

requestHandler.executeRequest(graphQLRequest, deprecatedContext, graphQLContext)
requestHandler.executeRequest(graphQLRequest, deprecatedContext, graphQLContext)
}
}
}
}

0 comments on commit a937b8d

Please sign in to comment.