|
14 | 14 |
|
15 | 15 | package org.eclipse.dataspaceconnector.core.defaults;
|
16 | 16 |
|
| 17 | +import dev.failsafe.RetryPolicy; |
| 18 | +import okhttp3.EventListener; |
| 19 | +import okhttp3.OkHttpClient; |
17 | 20 | import org.eclipse.dataspaceconnector.common.concurrency.LockManager;
|
| 21 | +import org.eclipse.dataspaceconnector.core.base.OkHttpClientFactory; |
18 | 22 | import org.eclipse.dataspaceconnector.core.defaults.assetindex.InMemoryAssetIndex;
|
19 | 23 | import org.eclipse.dataspaceconnector.core.defaults.contractdefinition.InMemoryContractDefinitionStore;
|
20 | 24 | import org.eclipse.dataspaceconnector.core.defaults.negotiationstore.InMemoryContractNegotiationStore;
|
21 | 25 | import org.eclipse.dataspaceconnector.core.defaults.policystore.InMemoryPolicyDefinitionStore;
|
22 | 26 | import org.eclipse.dataspaceconnector.core.defaults.transferprocessstore.InMemoryTransferProcessStore;
|
23 | 27 | import org.eclipse.dataspaceconnector.dataloading.AssetLoader;
|
24 | 28 | import org.eclipse.dataspaceconnector.dataloading.ContractDefinitionLoader;
|
| 29 | +import org.eclipse.dataspaceconnector.spi.EdcSetting; |
25 | 30 | import org.eclipse.dataspaceconnector.spi.asset.AssetIndex;
|
26 | 31 | import org.eclipse.dataspaceconnector.spi.asset.DataAddressResolver;
|
27 | 32 | import org.eclipse.dataspaceconnector.spi.contract.negotiation.store.ContractNegotiationStore;
|
28 | 33 | import org.eclipse.dataspaceconnector.spi.contract.offer.store.ContractDefinitionStore;
|
29 | 34 | import org.eclipse.dataspaceconnector.spi.policy.store.PolicyDefinitionStore;
|
| 35 | +import org.eclipse.dataspaceconnector.spi.system.Inject; |
30 | 36 | import org.eclipse.dataspaceconnector.spi.system.Provider;
|
31 | 37 | import org.eclipse.dataspaceconnector.spi.system.ServiceExtension;
|
32 | 38 | import org.eclipse.dataspaceconnector.spi.system.ServiceExtensionContext;
|
33 | 39 | import org.eclipse.dataspaceconnector.spi.transaction.NoopTransactionContext;
|
34 | 40 | import org.eclipse.dataspaceconnector.spi.transaction.TransactionContext;
|
35 | 41 | import org.eclipse.dataspaceconnector.spi.transfer.store.TransferProcessStore;
|
36 | 42 |
|
| 43 | +import java.time.temporal.ChronoUnit; |
37 | 44 | import java.util.concurrent.locks.ReentrantReadWriteLock;
|
38 | 45 |
|
39 | 46 | /**
|
40 | 47 | * Provides (in-mem & no-op) defaults for various stores, registries etc.
|
41 | 48 | * Provider methods are only invoked if no other implementation was found on the classpath.
|
42 | 49 | */
|
43 | 50 | public class DefaultServicesExtension implements ServiceExtension {
|
| 51 | + @EdcSetting(value = "Maximum retries for the retry policy before a failure is propagated") |
| 52 | + public static final String MAX_RETRIES = "edc.core.retry.retries.max"; |
| 53 | + @EdcSetting(value = "Minimum number of milliseconds for exponential backoff") |
| 54 | + public static final String BACKOFF_MIN_MILLIS = "edc.core.retry.backoff.min"; |
| 55 | + @EdcSetting(value = "Maximum number of milliseconds for exponential backoff. ") |
| 56 | + public static final String BACKOFF_MAX_MILLIS = "edc.core.retry.backoff.max"; |
44 | 57 |
|
45 | 58 | private InMemoryAssetIndex assetIndex;
|
46 | 59 | private InMemoryContractDefinitionStore contractDefinitionStore;
|
| 60 | + /** |
| 61 | + * An optional OkHttp {@link EventListener} that can be used to instrument OkHttp client for collecting metrics. |
| 62 | + * Used by the optional {@code micrometer} module. |
| 63 | + */ |
| 64 | + @Inject(required = false) |
| 65 | + private EventListener okHttpEventListener; |
47 | 66 |
|
48 | 67 | public DefaultServicesExtension() {
|
49 | 68 | }
|
50 | 69 |
|
| 70 | + @Provider(isDefault = true) |
| 71 | + public RetryPolicy<?> retryPolicy(ServiceExtensionContext context) { |
| 72 | + var maxRetries = context.getSetting(MAX_RETRIES, 5); |
| 73 | + var minBackoff = context.getSetting(BACKOFF_MIN_MILLIS, 500); |
| 74 | + var maxBackoff = context.getSetting(BACKOFF_MAX_MILLIS, 10_000); |
| 75 | + |
| 76 | + return RetryPolicy.builder() |
| 77 | + .withMaxRetries(maxRetries) |
| 78 | + .withBackoff(minBackoff, maxBackoff, ChronoUnit.MILLIS) |
| 79 | + .build(); |
| 80 | + } |
| 81 | + |
51 | 82 | @Provider(isDefault = true)
|
52 | 83 | public AssetIndex defaultAssetIndex() {
|
53 | 84 | return getAssetIndex();
|
@@ -94,6 +125,12 @@ public TransactionContext defaultTransactionContext(ServiceExtensionContext cont
|
94 | 125 | return new NoopTransactionContext();
|
95 | 126 | }
|
96 | 127 |
|
| 128 | + |
| 129 | + @Provider(isDefault = true) |
| 130 | + public OkHttpClient okHttpClient(ServiceExtensionContext context) { |
| 131 | + return OkHttpClientFactory.create(context, okHttpEventListener); |
| 132 | + } |
| 133 | + |
97 | 134 | private ContractDefinitionStore getContractDefinitionStore() {
|
98 | 135 | if (contractDefinitionStore == null) {
|
99 | 136 | contractDefinitionStore = new InMemoryContractDefinitionStore();
|
|
0 commit comments