Skip to content

Commit

Permalink
Fix smallrye#728: Use WeakHashMap to prevent memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
ybroeker committed Apr 6, 2021
1 parent fd29f11 commit e85d74c
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 e85d74c

Please sign in to comment.