-
Notifications
You must be signed in to change notification settings - Fork 4
feat: parallelize requests by default #102
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...ql-context/src/main/java/org/hypertrace/core/graphql/context/AsyncDataFetcherFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.hypertrace.core.graphql.context; | ||
|
||
import static java.util.concurrent.TimeUnit.MILLISECONDS; | ||
|
||
import com.google.common.util.concurrent.ThreadFactoryBuilder; | ||
import com.google.inject.Injector; | ||
import graphql.schema.DataFetcher; | ||
import graphql.schema.DataFetchingEnvironment; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
import lombok.AllArgsConstructor; | ||
import org.hypertrace.core.graphql.spi.config.GraphQlServiceConfig; | ||
|
||
@Singleton | ||
class AsyncDataFetcherFactory { | ||
|
||
private final Injector injector; | ||
private final GraphQlServiceConfig config; | ||
private final ExecutorService requestExecutor; | ||
|
||
@Inject | ||
AsyncDataFetcherFactory(Injector injector, GraphQlServiceConfig config) { | ||
this.injector = injector; | ||
this.config = config; | ||
this.requestExecutor = | ||
Executors.newFixedThreadPool( | ||
config.getMaxRequestThreads(), | ||
new ThreadFactoryBuilder().setDaemon(true).setNameFormat("request-handler-%d").build()); | ||
} | ||
|
||
<T> DataFetcher<CompletableFuture<T>> buildDataFetcher( | ||
Class<? extends DataFetcher<CompletableFuture<T>>> dataFetcherClass) { | ||
return new AsyncForwardingDataFetcher<>( | ||
this.injector.getInstance(dataFetcherClass), requestExecutor, config); | ||
} | ||
|
||
@AllArgsConstructor | ||
private static class AsyncForwardingDataFetcher<T> implements DataFetcher<CompletableFuture<T>> { | ||
private final DataFetcher<CompletableFuture<T>> delegate; | ||
private final ExecutorService executorService; | ||
private final GraphQlServiceConfig config; | ||
|
||
@Override | ||
public CompletableFuture<T> get(DataFetchingEnvironment dataFetchingEnvironment) | ||
throws Exception { | ||
// Really all we're doing here is changing the thread that the future is run on by default | ||
return CompletableFuture.supplyAsync( | ||
() -> { | ||
try { | ||
return delegate | ||
.get(dataFetchingEnvironment) | ||
.get(config.getGraphQlTimeout().toMillis(), MILLISECONDS); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
}, | ||
executorService); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...ontext/src/test/java/org/hypertrace/core/graphql/context/AsyncDataFetcherFactoryTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.hypertrace.core.graphql.context; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.google.inject.Guice; | ||
import graphql.schema.DataFetcher; | ||
import graphql.schema.DataFetchingEnvironment; | ||
import java.util.concurrent.CompletableFuture; | ||
import org.hypertrace.core.graphql.spi.config.GraphQlServiceConfig; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class AsyncDataFetcherFactoryTest { | ||
@Mock GraphQlServiceConfig graphQlServiceConfig; | ||
@Mock DataFetchingEnvironment dataFetchingEnvironment; | ||
|
||
@Test | ||
void canBuildAsyncDataFetcher() throws Exception { | ||
when(graphQlServiceConfig.getMaxRequestThreads()).thenReturn(1); | ||
DataFetcher<CompletableFuture<Thread>> fetcher = | ||
new AsyncDataFetcherFactory(Guice.createInjector(), graphQlServiceConfig) | ||
.buildDataFetcher(ThreadEchoingDataFetcher.class); | ||
|
||
Thread fetcherThread = fetcher.get(dataFetchingEnvironment).get(); | ||
|
||
assertNotEquals(Thread.currentThread(), fetcherThread); | ||
assertNotNull(fetcherThread); | ||
} | ||
|
||
private static class ThreadEchoingDataFetcher implements DataFetcher<CompletableFuture<Thread>> { | ||
@Override | ||
public CompletableFuture<Thread> get(DataFetchingEnvironment environment) { | ||
return CompletableFuture.completedFuture(Thread.currentThread()); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No change here, just exposing existing pool for config with same default.