-
Notifications
You must be signed in to change notification settings - Fork 42
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
fix!: add rw locks to client/api, hook accessor name #131
Changes from 7 commits
b968e43
4ff9296
8de427c
7ed974c
03485fa
1e6c182
5ca9663
bf96c76
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,41 @@ | ||
package dev.openfeature.sdk; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import javax.annotation.Nullable; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import dev.openfeature.sdk.internal.AutoCloseableLock; | ||
import dev.openfeature.sdk.internal.AutoCloseableReentrantReadWriteLock; | ||
|
||
/** | ||
* A global singleton which holds base configuration for the OpenFeature library. | ||
* Configuration here will be shared across all {@link Client}s. | ||
*/ | ||
public class OpenFeatureAPI { | ||
private static OpenFeatureAPI api; | ||
@Getter | ||
@Setter | ||
// package-private multi-read/single-write lock | ||
static AutoCloseableReentrantReadWriteLock hooksLock = new AutoCloseableReentrantReadWriteLock(); | ||
static AutoCloseableReentrantReadWriteLock providerLock = new AutoCloseableReentrantReadWriteLock(); | ||
static AutoCloseableReentrantReadWriteLock contextLock = new AutoCloseableReentrantReadWriteLock(); | ||
private FeatureProvider provider; | ||
@Getter | ||
@Setter | ||
private EvaluationContext evaluationContext; | ||
@Getter | ||
private List<Hook> apiHooks; | ||
|
||
public OpenFeatureAPI() { | ||
private OpenFeatureAPI() { | ||
this.apiHooks = new ArrayList<>(); | ||
} | ||
|
||
private static class SingletonHolder { | ||
private static final OpenFeatureAPI INSTANCE = new OpenFeatureAPI(); | ||
} | ||
|
||
/** | ||
* Provisions the {@link OpenFeatureAPI} singleton (if needed) and returns it. | ||
* @return The singleton instance. | ||
*/ | ||
public static OpenFeatureAPI getInstance() { | ||
synchronized (OpenFeatureAPI.class) { | ||
if (api == null) { | ||
api = new OpenFeatureAPI(); | ||
} | ||
} | ||
return api; | ||
return SingletonHolder.INSTANCE; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a more performant singleton. The I've used the "Bill Pugh" approach here. This takes advantage of basic Java spec requirements that guarantee classes are loaded only once. There's no synchronization concerns here and no performance penalty. |
||
} | ||
|
||
public Metadata getProviderMetadata() { | ||
|
@@ -56,11 +54,66 @@ public Client getClient(@Nullable String name, @Nullable String version) { | |
return new OpenFeatureClient(this, name, version); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public void setEvaluationContext(EvaluationContext evaluationContext) { | ||
try (AutoCloseableLock __ = contextLock.writeLockAutoCloseable()) { | ||
this.evaluationContext = evaluationContext; | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public EvaluationContext getEvaluationContext() { | ||
try (AutoCloseableLock __ = contextLock.readLockAutoCloseable()) { | ||
return this.evaluationContext; | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public void setProvider(FeatureProvider provider) { | ||
try (AutoCloseableLock __ = providerLock.writeLockAutoCloseable()) { | ||
this.provider = provider; | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public FeatureProvider getProvider() { | ||
try (AutoCloseableLock __ = providerLock.readLockAutoCloseable()) { | ||
return this.provider; | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public void addHooks(Hook... hooks) { | ||
this.apiHooks.addAll(Arrays.asList(hooks)); | ||
try (AutoCloseableLock __ = hooksLock.writeLockAutoCloseable()) { | ||
this.apiHooks.addAll(Arrays.asList(hooks)); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public List<Hook> getHooks() { | ||
try (AutoCloseableLock __ = hooksLock.readLockAutoCloseable()) { | ||
return this.apiHooks; | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public void clearHooks() { | ||
this.apiHooks.clear(); | ||
try (AutoCloseableLock __ = hooksLock.writeLockAutoCloseable()) { | ||
this.apiHooks.clear(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package dev.openfeature.sdk.internal; | ||
|
||
public interface AutoCloseableLock extends AutoCloseable { | ||
|
||
/** | ||
* Override the exception in AutoClosable. | ||
*/ | ||
@Override | ||
void close(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package dev.openfeature.sdk.internal; | ||
|
||
import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
|
||
/** | ||
* A utility class that wraps a multi-read/single-write lock construct as AutoCloseable, so it can | ||
* be used in a try-with-resources. | ||
*/ | ||
public class AutoCloseableReentrantReadWriteLock extends ReentrantReadWriteLock { | ||
|
||
/** | ||
* Get the single write lock as an AutoCloseableLock. | ||
* @return unlock method ref | ||
*/ | ||
public AutoCloseableLock writeLockAutoCloseable() { | ||
this.writeLock().lock(); | ||
return this.writeLock()::unlock; | ||
} | ||
|
||
/** | ||
* Get the multi read lock as an AutoCloseableLock. | ||
* @return unlock method ref | ||
*/ | ||
public AutoCloseableLock readLockAutoCloseable() { | ||
this.readLock().lock(); | ||
return this.readLock()::unlock; | ||
} | ||
} |
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.
I don't think we want anyone constructing this?