Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package io.split.engine.common;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.split.engine.SDKReadinessGates;
import io.split.engine.experiments.FetchResult;
import io.split.engine.experiments.SplitFetcher;
import io.split.engine.experiments.SplitSynchronizationTask;
import io.split.engine.segments.SegmentFetcher;
import io.split.engine.segments.SegmentSynchronizationTask;
import io.split.storages.SegmentCacheProducer;
import io.split.storages.SplitCacheProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import static com.google.common.base.Preconditions.checkNotNull;
public class SynchronizerImp implements Synchronizer {
// The boxing here IS necessary, so that the constants are not inlined by the compiler
// and can be modified for the test (we don't want to wait that much in an UT)
private static final long ON_DEMAND_FETCH_BACKOFF_BASE_MS = new Long(10000); //backoff base starting at 10 seconds (!)
private static final long ON_DEMAND_FETCH_BACKOFF_MAX_WAIT_MS = new Long(60000); // don't sleep for more than 1 second
private static final int ON_DEMAND_FETCH_BACKOFF_MAX_RETRIES = 10;
private static final Logger _log = LoggerFactory.getLogger(Synchronizer.class);
private final SplitSynchronizationTask _splitSynchronizationTask;
private final SplitFetcher _splitFetcher;
private final SegmentSynchronizationTask _segmentSynchronizationTaskImp;
private final SplitCacheProducer _splitCacheProducer;
private final SegmentCacheProducer segmentCacheProducer;
private final int _onDemandFetchRetryDelayMs;
private final int _onDemandFetchMaxRetries;
private final int _failedAttemptsBeforeLogging;
private final boolean _cdnResponseHeadersLogging;
private final Gson gson = new GsonBuilder().create();
public SynchronizerImp(SplitSynchronizationTask splitSynchronizationTask,
SplitFetcher splitFetcher,
SegmentSynchronizationTask segmentSynchronizationTaskImp,
SplitCacheProducer splitCacheProducer,
SegmentCacheProducer segmentCacheProducer,
int onDemandFetchRetryDelayMs,
int onDemandFetchMaxRetries,
int failedAttemptsBeforeLogging,
boolean cdnResponseHeadersLogging,
SDKReadinessGates gates) {
_splitSynchronizationTask = checkNotNull(splitSynchronizationTask);
_splitFetcher = checkNotNull(splitFetcher);
_segmentSynchronizationTaskImp = checkNotNull(segmentSynchronizationTaskImp);
_splitCacheProducer = checkNotNull(splitCacheProducer);
this.segmentCacheProducer = checkNotNull(segmentCacheProducer);
_onDemandFetchRetryDelayMs = checkNotNull(onDemandFetchRetryDelayMs);
_cdnResponseHeadersLogging = cdnResponseHeadersLogging;
_onDemandFetchMaxRetries = onDemandFetchMaxRetries;
_failedAttemptsBeforeLogging = failedAttemptsBeforeLogging;
}
@Override
public boolean syncAll() {
FetchResult fetchResult = _splitFetcher.forceRefresh(new FetchOptions.Builder().cacheControlHeaders(true).build());
return fetchResult.isSuccess() && _segmentSynchronizationTaskImp.fetchAllSynchronous();
}
@Override
public void startPeriodicFetching() {
_log.debug("Starting Periodic Fetching ...");
_splitSynchronizationTask.startPeriodicFetching();
_segmentSynchronizationTaskImp.startPeriodicFetching();
}
@Override
public void stopPeriodicFetching() {
_log.debug("Stop Periodic Fetching ...");
_splitSynchronizationTask.stop();
_segmentSynchronizationTaskImp.stop();
}
private static class SyncResult {
/* package private */ SyncResult(boolean success, int remainingAttempts, FetchResult fetchResult) {
_success = success;
_remainingAttempts = remainingAttempts;
_fetchResult = fetchResult;
}
public boolean success() { return _success; }
public int remainingAttempts() { return _remainingAttempts; }
private final boolean _success;
private final int _remainingAttempts;
private final FetchResult _fetchResult;
}
private SyncResult attemptSplitsSync(long targetChangeNumber,
FetchOptions opts,
Function nextWaitMs,
int maxRetries) {
int remainingAttempts = maxRetries;
while(true) {
remainingAttempts--;
FetchResult fetchResult = _splitFetcher.forceRefresh(opts);
if (targetChangeNumber <= _splitCacheProducer.getChangeNumber()) {
return new SyncResult(true, remainingAttempts, fetchResult);
} else if (remainingAttempts <= 0) {
return new SyncResult(false, remainingAttempts, fetchResult);
}
try {
long howLong = nextWaitMs.apply(null);
Thread.sleep(howLong);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
_log.debug("Error trying to sleep current Thread.");
}
}
}
private void logCdnHeaders(String prefix, int maxRetries, int remainingAttempts, List