Skip to content

Commit

Permalink
Add TusRetryingClient
Browse files Browse the repository at this point in the history
  • Loading branch information
Acconut committed Mar 27, 2016
1 parent 9d07b59 commit b3db9b3
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/main/java/io/tus/java/client/ProtocolException.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
package io.tus.java.client;

import java.io.IOException;
import java.net.HttpURLConnection;

/**
* This exception is thrown if the server sends a request with an unexpected status code or
* missing/invalid headers.
*/
public class ProtocolException extends Exception {
private HttpURLConnection connection;

public ProtocolException(String message) {
super(message);
}

public ProtocolException(String message, HttpURLConnection connection) {
super(message);
this.connection = connection;
}

public HttpURLConnection getCausingConnection() {
return connection;
}

public boolean shouldRetry() {
if(connection == null) {
return false;
}

try {
int responseCode = connection.getResponseCode();

return responseCode >= 500 && responseCode < 600;
} catch(IOException e) {
return false;
}
}
}
99 changes: 99 additions & 0 deletions src/main/java/io/tus/java/client/TusRetryingClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
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);
}
}

0 comments on commit b3db9b3

Please sign in to comment.