|
14 | 14 | import java.util.Optional; |
15 | 15 | import java.util.UUID; |
16 | 16 | import java.util.concurrent.TimeUnit; |
| 17 | +import java.util.function.IntFunction; |
17 | 18 | import okhttp3.HttpUrl; |
18 | 19 | import okhttp3.MediaType; |
19 | 20 | import okhttp3.OkHttpClient; |
@@ -298,6 +299,63 @@ public GetTransactionResponse getTransaction(String hash) { |
298 | 299 | "getTransaction", params, new TypeToken<SorobanRpcResponse<GetTransactionResponse>>() {}); |
299 | 300 | } |
300 | 301 |
|
| 302 | + /** |
| 303 | + * An alias for {@link #pollTransaction(String, int, SleepStrategy)} with default parameters, |
| 304 | + * which {@code maxAttempts} is set to 30, and the sleep strategy is set to a default strategy |
| 305 | + * that sleeps for 1 second between attempts. |
| 306 | + * |
| 307 | + * @param hash The hash of the transaction to poll for. |
| 308 | + * @return A {@link GetTransactionResponse} object after a "found" response, (which may be success |
| 309 | + * or failure) or the last response obtained after polling the maximum number of specified |
| 310 | + * attempts. |
| 311 | + * @throws InterruptedException If the thread is interrupted while sleeping between attempts. |
| 312 | + */ |
| 313 | + public GetTransactionResponse pollTransaction(String hash) throws InterruptedException { |
| 314 | + return pollTransaction( |
| 315 | + hash, |
| 316 | + 30, |
| 317 | + attempt -> { |
| 318 | + return 1_000L; |
| 319 | + }); |
| 320 | + } |
| 321 | + |
| 322 | + /** |
| 323 | + * Polls the transaction status until it is completed or the maximum number of attempts is |
| 324 | + * reached. |
| 325 | + * |
| 326 | + * <p>After submitting a transaction, clients can use this to poll for transaction completion and |
| 327 | + * return a definitive state of success or failure. |
| 328 | + * |
| 329 | + * @param hash The hash of the transaction to poll for. |
| 330 | + * @param maxAttempts The number of attempts to make before returning the last-seen status. |
| 331 | + * @param sleepStrategy A strategy to determine the sleep duration between attempts. It should |
| 332 | + * take the current attempt number and return the sleep duration in milliseconds. |
| 333 | + * @return A {@link GetTransactionResponse} object after a "found" response, (which may be success |
| 334 | + * or failure) or the last response obtained after polling the maximum number of specified |
| 335 | + * attempts. |
| 336 | + * @throws IllegalArgumentException If maxAttempts is less than or equal to 0. |
| 337 | + * @throws InterruptedException If the thread is interrupted while sleeping between attempts. |
| 338 | + */ |
| 339 | + public GetTransactionResponse pollTransaction( |
| 340 | + String hash, int maxAttempts, SleepStrategy sleepStrategy) throws InterruptedException { |
| 341 | + if (maxAttempts <= 0) { |
| 342 | + throw new IllegalArgumentException("maxAttempts must be greater than 0"); |
| 343 | + } |
| 344 | + |
| 345 | + int attempts = 0; |
| 346 | + GetTransactionResponse getTransactionResponse = null; |
| 347 | + while (attempts < maxAttempts) { |
| 348 | + getTransactionResponse = getTransaction(hash); |
| 349 | + if (!GetTransactionResponse.GetTransactionStatus.NOT_FOUND.equals( |
| 350 | + getTransactionResponse.getStatus())) { |
| 351 | + return getTransactionResponse; |
| 352 | + } |
| 353 | + attempts++; |
| 354 | + TimeUnit.MILLISECONDS.sleep(sleepStrategy.apply(attempts)); |
| 355 | + } |
| 356 | + return getTransactionResponse; |
| 357 | + } |
| 358 | + |
301 | 359 | /** |
302 | 360 | * Gets a detailed list of transactions starting from the user specified starting point that you |
303 | 361 | * can paginate as long as the pages fall within the history retention of their corresponding RPC |
@@ -754,4 +812,11 @@ public enum Durability { |
754 | 812 | TEMPORARY, |
755 | 813 | PERSISTENT |
756 | 814 | } |
| 815 | + |
| 816 | + /** Strategy for sleeping between retries in a retry loop. */ |
| 817 | + @FunctionalInterface |
| 818 | + public interface SleepStrategy extends IntFunction<Long> { |
| 819 | + // apply as in apply(iterationNumber) -> millisecondsToSleep |
| 820 | + Long apply(int iteration); |
| 821 | + } |
757 | 822 | } |
0 commit comments