Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public FlagdProvider(final FlagdOptions options) {
}
hooks.add(new SyncMetadataHook(this::getEnrichedContext));
contextEnricher = options.getContextEnricher();
errorExecutor = Executors.newSingleThreadScheduledExecutor();
errorExecutor = Executors.newSingleThreadScheduledExecutor(new FlagdThreadFactory("flagd-provider-thread"));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The string literal "flagd-provider-thread" is duplicated on line 108. To improve maintainability and avoid magic strings, consider extracting it into a private static final constant at the class level. For example: private static final String ERROR_EXECUTOR_THREAD_NAME_PREFIX = "flagd-provider-thread";

gracePeriod = options.getRetryGracePeriod();
deadline = options.getDeadline();
}
Expand All @@ -105,7 +105,7 @@ public FlagdProvider(final FlagdOptions options) {
deadline = Config.DEFAULT_DEADLINE;
gracePeriod = Config.DEFAULT_STREAM_RETRY_GRACE_PERIOD;
hooks.add(new SyncMetadataHook(this::getEnrichedContext));
errorExecutor = Executors.newSingleThreadScheduledExecutor();
errorExecutor = Executors.newSingleThreadScheduledExecutor(new FlagdThreadFactory("flagd-provider-thread"));
if (initialized) {
this.syncResources.initialize();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package dev.openfeature.contrib.providers.flagd;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
* Thread factory for the Flagd provider to allow named daemon threads to be created.
*/
class FlagdThreadFactory implements ThreadFactory {

private final AtomicInteger counter = new AtomicInteger();
private final String namePrefix;

/**
* {@link FlagdThreadFactory}'s constructor.
*
* @param namePrefix Prefix used for setting the new thread's name.
*/
FlagdThreadFactory(String namePrefix) {
this.namePrefix = namePrefix;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The constructor doesn't validate if namePrefix is null. If a null value is passed, it will lead to a NullPointerException later in the newThread method. It's good practice to validate arguments in the constructor to fail-fast by using Objects.requireNonNull.

Suggested change
this.namePrefix = namePrefix;
this.namePrefix = java.util.Objects.requireNonNull(namePrefix, "namePrefix must not be null");

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems reasonable to me, wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree, I have added the suggestion & covering test

}

@Override
public Thread newThread(Runnable runnable) {
final Thread thread = new Thread(runnable);
thread.setDaemon(true);
thread.setName(namePrefix + "-" + counter.incrementAndGet());
return thread;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package dev.openfeature.contrib.providers.flagd;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

class FlagdThreadFactoryTest {

private static final String THREAD_NAME = "testthread";
private final Runnable runnable = () -> {};

@Test
void verifyNewThreadHasNamePrefix() {

var flagdThreadFactory = new FlagdThreadFactory(THREAD_NAME);
var thread = flagdThreadFactory.newThread(runnable);

assertThat(thread.getName()).isEqualTo(THREAD_NAME + "-1");
assertThat(thread.isDaemon()).isTrue();
}

@Test
void verifyNewThreadHasNamePrefixWithIncrement() {

var flagdThreadFactory = new FlagdThreadFactory(THREAD_NAME);
var threadOne = flagdThreadFactory.newThread(runnable);
var threadTwo = flagdThreadFactory.newThread(runnable);

assertThat(threadOne.getName()).isEqualTo(THREAD_NAME + "-1");
assertThat(threadOne.isDaemon()).isTrue();
assertThat(threadTwo.getName()).isEqualTo(THREAD_NAME + "-2");
assertThat(threadTwo.isDaemon()).isTrue();
}
}