Avoid too many retries when writing persistent sessions fails

Closes #35047

Signed-off-by: Alexander Schwartz <aschwart@redhat.com>
This commit is contained in:
Alexander Schwartz 2025-01-08 20:52:49 +01:00 committed by Pedro Igor
parent 507cd4407e
commit e3a213cc9c

View File

@ -20,6 +20,7 @@ package org.keycloak.models.sessions.infinispan.changes;
import org.jboss.logging.Logger;
import org.keycloak.common.util.Retry;
import org.keycloak.models.KeycloakSessionFactory;
import org.keycloak.models.ModelDuplicateException;
import org.keycloak.models.utils.KeycloakModelUtils;
import java.time.Duration;
@ -99,8 +100,18 @@ public class PersistentSessionsWorker {
change::perform);
change.complete();
performedChanges.add(change);
} catch (ModelDuplicateException ex) {
// duplicate exceptions are unlikely to succeed on a retry,
change.fail(ex);
performedChanges.add(change);
} catch (Throwable ex) {
throwables.add(ex);
if (iteration > 20) {
// never retry more than 20 times
change.fail(ex);
performedChanges.add(change);
} else {
throwables.add(ex);
}
}
});
batch.removeAll(performedChanges);