@Override protected void closeThisActor() { closeQuietly(internalTokensSupplier); } }
private void scheduleNextTokenFetchFromSphere(final Tokens tokens) { final Long delayInSecondsToFetchNewToken = Optional.ofNullable(tokens.getExpiresIn()) .map(ttlInSeconds -> selectNextRetryTime(ttlInSeconds)) .orElse(60L); schedule(new FetchTokenFromSphereMessage(), delayInSecondsToFetchNewToken, TimeUnit.SECONDS); }
private void process(final SuccessfulTokenFetchMessage m) { isWaitingForToken = false; tokensCache = Optional.of(m.tokens); subscribers.forEach(subscriber -> tellSubscriberNewTokens(m.tokens, subscriber)); scheduleNextTokenFetchFromSphere(m.tokens); }
private void process(final SubscribeMessage m) { subscribers.add(m.subscriber); if (tokensCache.isPresent()) { tellSubscriberNewTokens(tokensCache.get(), m.subscriber); } else if (!isWaitingForToken) { tell(new FetchTokenFromSphereMessage()); } else { //fetching token is in progress } }
@Override protected void receive(final Object message) { receiveBuilder(message) .when(FetchTokenFromSphereMessage.class, this::process) .when(SuccessfulTokenFetchMessage.class, this::process) .when(FailedTokenFetchMessage.class, this::process); }
private void process(final FetchTokenFromSphereMessage m) { if (!isWaitingForToken) { isWaitingForToken = true; //for users it is fail fast but in the background it will be attempted again final CompletionStage<Tokens> future = m.attempt > 0 ? supervisedTokenSupplier.apply(() -> internalTokensSupplier.get()) : internalTokensSupplier.get(); onSuccess(future, tokens -> tell(new SuccessfulTokenFetchMessage(tokens))); onFailure(future, e -> { requestUpdateFailedStatus.accept(e); tell(new FailedTokenFetchMessage(e)); tell(new FetchTokenFromSphereMessage(1)); }); } }
private AutoRefreshSphereAccessTokenSupplierImpl(final SphereAuthConfig config, final HttpClient httpClient, final boolean closeHttpClient) { final TokensSupplier internalTokensSupplier = TokensSupplierImpl.of(config, httpClient, closeHttpClient); authActor = new AuthActor(internalTokensSupplier); authActor.tell(new SubscribeMessage(tokenActor)); }
private void scheduleNextTokenFetchFromSphere(final Tokens tokens) { final Long delayInSecondsToFetchNewToken = Optional.ofNullable(tokens.getExpiresIn()).map(ttlInSeconds -> ttlInSeconds - 60 * 60).orElse(60 * 30L); schedule(new FetchTokenFromSphereMessage(), delayInSecondsToFetchNewToken, TimeUnit.SECONDS); }
private void process(final SuccessfulTokenFetchMessage m) { isWaitingForToken = false; requestUpdateTokens.accept(m.tokens); scheduleNextTokenFetchFromSphere(m.tokens); }
@Test public void selectNextRetryTime() { final long aDay = 60 * 60 * 24L; assertThat(AuthActor.selectNextRetryTime(20 * aDay)).as("max one day").isEqualTo(aDay); assertThat(AuthActor.selectNextRetryTime((long)(0.5 * aDay))).as("half the time of the expire time").isEqualTo(aDay / 4); assertThat(AuthActor.selectNextRetryTime(-100L)).as("at least a second").isEqualTo(1L); }
private void process(final SubscribeMessage m) { subscribers.add(m.subscriber); if (tokensCache.isPresent()) { tellSubscriberNewTokens(tokensCache.get(), m.subscriber); } else if (!isWaitingForToken) { tell(new FetchTokenFromSphereMessage()); } else { //fetching token is in progress } }
@Override protected void receive(final Object message) { receiveBuilder(message) .when(FetchTokenFromSphereMessage.class, this::process) .when(SuccessfulTokenFetchMessage.class, this::process) .when(FailedTokenFetchMessage.class, this::process); }
private void process(final FetchTokenFromSphereMessage m) { if (!isWaitingForToken) { isWaitingForToken = true; //for users it is fail fast but in the background it will be attempted again final CompletionStage<Tokens> future = m.attempt > 0 ? supervisedTokenSupplier.apply(() -> internalTokensSupplier.get()) : internalTokensSupplier.get(); onSuccess(future, tokens -> tell(new SuccessfulTokenFetchMessage(tokens))); onFailure(future, e -> { requestUpdateFailedStatus.accept(e); tell(new FailedTokenFetchMessage(e)); tell(new FetchTokenFromSphereMessage(1)); }); } }
private AutoRefreshSphereAccessTokenSupplierImpl(final SphereAuthConfig config, final HttpClient httpClient, final boolean closeHttpClient) { tokensSupplier = TokensSupplierImpl.of(config, httpClient, closeHttpClient); authActor = new AuthActor(tokensSupplier, this::supervisedTokenSupplier, this::requestUpdateTokens, this::requestUpdateFailedStatus); authActor.tell(new AuthActorProtocol.FetchTokenFromSphereMessage()); }
private void scheduleNextTokenFetchFromSphere(final Tokens tokens) { final Long delayInSecondsToFetchNewToken = tokens.getExpiresIn().map(ttlInSeconds -> ttlInSeconds - 60 * 60).orElse(60 * 30L); schedule(new FetchTokenFromSphereMessage(), delayInSecondsToFetchNewToken, TimeUnit.SECONDS); }
private void process(final SuccessfulTokenFetchMessage m) { isWaitingForToken = false; requestUpdateTokens.accept(m.tokens); scheduleNextTokenFetchFromSphere(m.tokens); }
private void scheduleNextTokenFetchFromSphere(final Tokens tokens) { final Long delayInSecondsToFetchNewToken = Optional.ofNullable(tokens.getExpiresIn()) .map(ttlInSeconds -> selectNextRetryTime(ttlInSeconds)) .orElse(60L); schedule(new FetchTokenFromSphereMessage(), delayInSecondsToFetchNewToken, TimeUnit.SECONDS); }
private void process(final SuccessfulTokenFetchMessage m) { isWaitingForToken = false; tokensCache = Optional.of(m.tokens); subscribers.forEach(subscriber -> tellSubscriberNewTokens(m.tokens, subscriber)); scheduleNextTokenFetchFromSphere(m.tokens); }
@Override protected void closeThisActor() { closeQuietly(internalTokensSupplier); } }
@Override protected void receive(final Object message) { receiveBuilder(message) .when(SubscribeMessage.class, this::process) .when(FetchTokenFromSphereMessage.class, this::process) .when(SuccessfulTokenFetchMessage.class, this::process) .when(FailedTokenFetchMessage.class, this::process); }