Skip to content

Commit

Permalink
added retrying and increased client timeout thresholds
Browse files Browse the repository at this point in the history
  • Loading branch information
smashyalts committed Feb 29, 2024
1 parent 2c36d00 commit c369e11
Showing 1 changed file with 33 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.funniray.chunkydedicated;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
Expand Down Expand Up @@ -80,18 +81,23 @@ public void onEnable() {
});
}

public void multipartUploadWithS3Client(String filePath) {
AmazonS3 s3 = AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("https://" + getConfig().getString("cloudflare-account-id") + ".r2.cloudflarestorage.com" + "/" + getConfig().getString("bucket-name"), "auto"))
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(getConfig().getString("cloudflare-access-key"), getConfig().getString("cloudflare-secret-key"))))
.build();
public void multipartUploadWithS3Client(String filePath) {
ClientConfiguration clientConfiguration = new ClientConfiguration();
clientConfiguration.setSocketTimeout(60000); // 60-second socket timeout
clientConfiguration.setConnectionTimeout(60000); // 60-second connection timeout

AmazonS3 s3 = AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("https://" + getConfig().getString("cloudflare-account-id") + ".r2.cloudflarestorage.com" + "/" + getConfig().getString("bucket-name"), "auto"))
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(getConfig().getString("cloudflare-access-key"), getConfig().getString("cloudflare-secret-key"))))
.withClientConfiguration(clientConfiguration) // Pass the client configuration here
.build();

InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(bucketName, "World." + key + ".zip");
InitiateMultipartUploadResult initResponse = s3.initiateMultipartUpload(initRequest);

File file = new File(filePath);
long contentLength = file.length();
long partSize = 5 * 1024 * 1024;
long partSize = 10 * 1024 * 1024; // Increased part size to 10MB

try {

Expand All @@ -101,7 +107,6 @@ public void multipartUploadWithS3Client(String filePath) {

partSize = Math.min(partSize, (contentLength - filePosition));


UploadPartRequest uploadRequest = new UploadPartRequest()
.withBucketName(bucketName)
.withKey("World." + key + ".zip")
Expand All @@ -111,18 +116,34 @@ public void multipartUploadWithS3Client(String filePath) {
.withFile(file)
.withPartSize(partSize);


UploadPartResult uploadResult = s3.uploadPart(uploadRequest);
partETags.add(uploadResult.getPartETag());
// Retry logic
int retryCount = 0;
while (true) {
try {
UploadPartResult uploadResult = s3.uploadPart(uploadRequest);
partETags.add(uploadResult.getPartETag());
break;
} catch (Exception e) {
if (++retryCount > 3) { // Maximum of 3 retries
throw e; // If still failing after 3 retries, rethrow the exception
}
// Wait before retrying
try {
Thread.sleep(1000 * retryCount); // Wait for an increasing amount of time before retrying
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw new RuntimeException(ie);
}
}
}

filePosition += partSize;
}


CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest(getConfig().getString("bucket-name"), "World." + key + ".zip",
initResponse.getUploadId(), partETags);

s3.completeMultipartUpload(compRequest);//Handle exception
s3.completeMultipartUpload(compRequest);
getLogger().info("File uploaded successfully");
} catch (Exception e) {
s3.abortMultipartUpload(new AbortMultipartUploadRequest(bucketName, "World." + key + ".zip", initResponse.getUploadId()));
Expand Down

0 comments on commit c369e11

Please sign in to comment.