Skip to content

Commit

Permalink
Better retry policy for API Gateway. Fixes #9
Browse files Browse the repository at this point in the history
  • Loading branch information
cagataygurturk committed Jul 12, 2016
1 parent fc24578 commit 5f80549
Showing 1 changed file with 38 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package org.lambadaframework.aws;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonWebServiceRequest;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.retry.PredefinedRetryPolicies;
import com.amazonaws.retry.RetryPolicy;
import com.amazonaws.services.apigateway.AmazonApiGateway;
import com.amazonaws.services.apigateway.AmazonApiGatewayClient;
import com.amazonaws.services.apigateway.model.*;
Expand All @@ -15,7 +20,9 @@
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.util.*;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


public class ApiGateway extends AWSTools {
Expand Down Expand Up @@ -86,7 +93,27 @@ protected AmazonApiGateway getApiGatewayClient() {
return apiGatewayClient;
}

return apiGatewayClient = new AmazonApiGatewayClient(getAWSCredentialsProvideChain()).withRegion(Region.getRegion(Regions.fromName(deployment.getRegion())));
RetryPolicy.RetryCondition retryCondition = new RetryPolicy.RetryCondition() {

@Override
public boolean shouldRetry(AmazonWebServiceRequest amazonWebServiceRequest, AmazonClientException amazonClientException, int i) {
if (amazonClientException instanceof TooManyRequestsException) {
return true;
}
return PredefinedRetryPolicies.DEFAULT_RETRY_CONDITION.shouldRetry(amazonWebServiceRequest,
amazonClientException, i);
}
};

RetryPolicy retryPolicy = new RetryPolicy(retryCondition,
PredefinedRetryPolicies.DEFAULT_BACKOFF_STRATEGY,
10, true);

ClientConfiguration clientConfig = new ClientConfiguration()
.withRetryPolicy(retryPolicy);

apiGatewayClient = new AmazonApiGatewayClient(getAWSCredentialsProvideChain(), clientConfig).withRegion(Region.getRegion(Regions.fromName(deployment.getRegion())));
return apiGatewayClient;
}

/**
Expand All @@ -97,14 +124,16 @@ protected AmazonApiGateway getApiGatewayClient() {
*/
public void deployEndpoints()
throws IOException {
if (log != null)

if (log != null) {
log.info("API Gateway deployment is being initialized.");
}

List<Resource> resources = getResources();

if (log != null)
if (log != null) {
log.info(resources.size() + " resources found in JAR File.");

}

createOrUpdateApi();
walkThroughResources(resources);
Expand Down Expand Up @@ -242,8 +271,9 @@ protected List<Resource> getResources()
throws IOException {
String jarFileLocation = deployment.getJarFileLocationOnLocalFileSystem();

if (log != null)
if (log != null) {
log.info("JAR File is being scanned. Used JAR File location: " + jarFileLocation + " Package: " + deployment.getPackageName());
}

JAXRSParser parser = new JAXRSParser().withJarFile(jarFileLocation, deployment.getPackageName());
return parser.scan();
Expand All @@ -252,7 +282,7 @@ protected List<Resource> getResources()

protected void walkThroughResources(List<Resource> resources) {

if (resources.size() == 0) {
if (resources.isEmpty()) {
if (log != null) {
log.info("Not found any resources to deploy");
}
Expand Down Expand Up @@ -345,7 +375,6 @@ protected String createResource(Resource jerseyResource) {

protected boolean deployResource(Resource jerseyResource) {

com.amazonaws.services.apigateway.model.Resource amazonApiResource;
String fullPath = jerseyResource.getPath();

if (log != null) {
Expand All @@ -358,7 +387,7 @@ protected boolean deployResource(Resource jerseyResource) {
log.info("Resource created: " + fullPath + " (" + createdId + ")");
}

amazonApiResource = getResourceByPath(fullPath);
com.amazonaws.services.apigateway.model.Resource amazonApiResource = getResourceByPath(fullPath);
deployMethods(jerseyResource, amazonApiResource);
return true;
}
Expand Down Expand Up @@ -397,25 +426,13 @@ protected void deployMethods(Resource jerseyResource, com.amazonaws.services.api
.withResourceId(apiGatewayResource.getId()));


/**
* To prevent TooManyRequestsException errors from API Gateway
*/
Thread.sleep(1000);

if (log != null) {
log.info(methodToDelete + " method deleted on resource id " + apiGatewayResource.getId());
}
} catch (NotFoundException e) {
/**
* Do nothing, continue
*/
} catch (InterruptedException e) {

if (log != null) {
log.error("A system error occured. Recovering");
}

deployMethods(jerseyResource, apiGatewayResource);
}
}

Expand Down

0 comments on commit 5f80549

Please sign in to comment.