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

Expose concurrency config for subscriptions in GraphQLWebSocketServer #2018

Open
GergKllai1 opened this issue Jul 18, 2024 · 0 comments
Open
Labels
type: enhancement New feature or request

Comments

@GergKllai1
Copy link

GergKllai1 commented Jul 18, 2024

The ktor subscription solution uses Flow::flatMapMerge which has a default concurrency of 16. This lead to the websocket hanging when I sent more than 16 different subscription messages in one connection.

@FlowPreview
public val DEFAULT_CONCURRENCY: Int = systemProp(
    DEFAULT_CONCURRENCY_PROPERTY_NAME,
    16, 1, Int.MAX_VALUE
)

@ExperimentalCoroutinesApi
public fun <T, R> Flow<T>.flatMapMerge(
    concurrency: Int = DEFAULT_CONCURRENCY,
    transform: suspend (value: T) -> Flow<R>
): Flow<R> =
    map(transform).flattenMerge(concurrency)

As a workaround in my project I copied the GraphQLWebSocketServer implementation with a very small modification for the SubscriptionMessageSubscribe

if (subscriptions.size >= CONCURRENCY) {
  logger.warn("Too many subscriptions")
  cancelSubscription(session, GraphQLSubscriptionStatus.TOO_MANY_REQUESTS, subscriptions)
  return@channelFlow
}

return parseRequestFlow(session, subscriptions)
  .flatMapMerge(concurrency = CONCURRENCY) { rawMessage ->
  // rest of the implementation
}

To reproduce this issue just start up a subscription and send more than 16 subscription messages with different ids. The 17th will not get any response back, also all other message types will stop responding.

It would be great if there would be an option to configure the concurrency number and also to have a cancel when reaching the number to avoid these hangs.

@GergKllai1 GergKllai1 added the type: enhancement New feature or request label Jul 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement New feature or request
Development

No branches or pull requests

1 participant