Skip to content

Commit

Permalink
Make ContextSnapshotFactory nullable in VirtualThreadTaskExecutor to …
Browse files Browse the repository at this point in the history
…support frameworks that wrap the whole task executor for context propagation (#1878)
  • Loading branch information
paulbakker authored Apr 9, 2024
1 parent 4cf5b19 commit 07e36f8
Showing 1 changed file with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

package com.netflix.graphql.dgs.internal;

import io.micrometer.context.ContextSnapshot;
import io.micrometer.context.ContextSnapshotFactory;
import org.jetbrains.annotations.NotNull;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.lang.Nullable;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;
Expand All @@ -32,18 +34,24 @@
@SuppressWarnings("unused")
public class VirtualThreadTaskExecutor implements AsyncTaskExecutor {
private final ThreadFactory threadFactory;

@Nullable
private final ContextSnapshotFactory contextSnapshotFactory;

public VirtualThreadTaskExecutor(ContextSnapshotFactory contextSnapshotFactory) {
public VirtualThreadTaskExecutor(@Nullable ContextSnapshotFactory contextSnapshotFactory) {
this.contextSnapshotFactory = contextSnapshotFactory;
this.threadFactory = Thread.ofVirtual().name("dgs-virtual-thread-", 0).factory();
}

@Override
public void execute(@NotNull Runnable task) {
var contextSnapshot = contextSnapshotFactory.captureAll();
var wrapped = contextSnapshot.wrap(task);
threadFactory.newThread(wrapped).start();
if (contextSnapshotFactory != null) {
ContextSnapshot contextSnapshot = contextSnapshotFactory.captureAll();
var wrapped = contextSnapshot.wrap(task);
threadFactory.newThread(wrapped).start();
} else {
threadFactory.newThread(task).start();
}
}

@Override
Expand Down

0 comments on commit 07e36f8

Please sign in to comment.