<commit_before>package io.tus.java.client;


import java.io.IOException;

public class TusRetryingClient extends TusClient {
    private int maxRetries = 5;

    public void setMaxRetries(int retries) {
        maxRetries = retries;
    }

    @Override
    public TusUploader createUpload(TusUpload upload) throws ProtocolException, IOException {
        int attempt = 0;
        while(true) {
            attempt++;

            try {
                return super.createUpload(upload);
            } catch(IOException e) {
                if(attempt < maxRetries) {
                    continue;
                }

                throw e;
            } catch(ProtocolException e) {
                if(e.shouldRetry()) {
                    continue;
                }

                if(attempt < maxRetries) {
                    continue;
                }

                throw e;
            }
        }
    }

    @Override
    public TusUploader resumeUpload(TusUpload upload) throws FingerprintNotFoundException, ResumingNotEnabledException, ProtocolException, IOException {
        int attempt = 0;
        while(true) {
            attempt++;

            try {
                return super.resumeUpload(upload);
            } catch(IOException e) {
                if(attempt < maxRetries) {
                    continue;
                }

                throw e;
            } catch(ProtocolException e) {
                if(e.shouldRetry()) {
                    continue;
                }

                if(attempt < maxRetries) {
                    continue;
                }

                throw e;
            }
        }
    }

    @Override
    public TusUploader resumeOrCreateUpload(TusUpload upload) throws IOException, ProtocolException {
        int attempt = 0;
        while(true) {
            attempt++;

            try {
                return super.resumeOrCreateUpload(upload);
            } catch(IOException e) {
                if(attempt < maxRetries) {
                    continue;
                }

                break;
            } catch(ProtocolException e) {
                if(e.shouldRetry()) {
                    continue;
                }

                if(attempt < maxRetries) {
                    continue;
                }

                break;
            }
        }

        // Fall back to creating an upload if resuming failed for too long.
        return createUpload(upload);
    }
}<commit_msg>Add flexible delays to TusRetryingClient<commit_after>package io.tus.java.client;


import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;

public class TusRetryingClient extends TusClient {
    private int[] delays = new int[]{500, 1000, 2000, 3000};

    public void setDelays(int[] delays) {
        this.delays = delays;
    }

    public int[] getDelays() {
        return delays;
    }

    @Override
    @Nullable
    public TusUploader createUpload(@NotNull TusUpload upload) throws ProtocolException, IOException {
        int attempt = 0;
        while(true) {
            attempt++;

            try {
                return super.createUpload(upload);
            } catch(ProtocolException e) {
                // Do not attempt a retry, if the Exception suggests so.
                if(!e.shouldRetry()) throw e;

                if(attempt >= delays.length) {
                    // We exceeds the number of maximum retries. In this case the latest exception
                    // is thrown.
                    throw e;
                }
            }  catch(IOException e) {
                if(attempt >= delays.length) {
                    // We exceeds the number of maximum retries. In this case the latest exception
                    // is thrown.
                    throw e;
                }
            }

            try {
                // Sleep for the specified delay before attempting the next retry.
                Thread.sleep(delays[attempt]);
            } catch(InterruptedException e) {
                // If we get interrupted while waiting for the next retry, the user has cancelled
                // the upload willingly and we return null as a signal.
                return null;
            }
        }
    }

    @Override
    @Nullable
    public TusUploader resumeUpload(@NotNull TusUpload upload) throws FingerprintNotFoundException, ResumingNotEnabledException, ProtocolException, IOException {
        int attempt = 0;
        while(true) {
            attempt++;

            try {
                return super.resumeUpload(upload);
            } catch(ProtocolException e) {
                // Do not attempt a retry, if the Exception suggests so.
                if(!e.shouldRetry()) throw e;

                if(attempt >= delays.length) {
                    // We exceeds the number of maximum retries. In this case the latest exception
                    // is thrown.
                    throw e;
                }
            }  catch(IOException e) {
                if(attempt >= delays.length) {
                    // We exceeds the number of maximum retries. In this case the latest exception
                    // is thrown.
                    throw e;
                }
            }

            try {
                // Sleep for the specified delay before attempting the next retry.
                Thread.sleep(delays[attempt]);
            } catch(InterruptedException e) {
                // If we get interrupted while waiting for the next retry, the user has cancelled
                // the upload willingly and we return null as a signal.
                return null;
            }
        }
    }

    // TODO resumeOrCreateUpload will return null if resumeUpload does so without trying to create one.
}
{'fraction_non_alphanumeric': 0.0716125708397733, 'fraction_numerical': 0.0036063884595569293, 'mean_word_length': 1.301066772026867, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2138558', 'n_tokens_mistral': 1451, 'n_tokens_neox': 1299, 'n_words': 407}