Skip to content

Commit

Permalink
Merge pull request #730 from ybroeker/backport/fix/memoryLeak
Browse files Browse the repository at this point in the history
Fix #728: Use WeakHashMap to prevent memory leaks
  • Loading branch information
phillip-kruger authored Apr 6, 2021
2 parents fd29f11 + e85d74c commit 0126854
Showing 1 changed file with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.smallrye.graphql.execution.context;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

import org.dataloader.BatchLoaderContextProvider;
Expand All @@ -10,7 +9,14 @@
// hacky way to pass the SmallRyeContext from data from data fetchers to BatchLoaderEnvironment
public class SmallRyeBatchLoaderContextProvider implements BatchLoaderContextProvider {

static final Map<DataLoader, SmallRyeBatchLoaderContextProvider> INSTANCES = new ConcurrentHashMap<>();
/**
* Stores the respective SmallRyeBatchLoaderContextProvider for each DataLoader.
* <p>
* WeakHashMap is used to enable garbage collection from both once the request is complete and the DataLoader is no longer
* in use.
*/
static final Map<DataLoader, SmallRyeBatchLoaderContextProvider> INSTANCES = Collections
.synchronizedMap(new WeakHashMap<>());

public void setDataLoader(DataLoader dataLoader) {
INSTANCES.put(dataLoader, this);
Expand Down

0 comments on commit 0126854

Please sign in to comment.