Ratelimiter update to the library

This commit is contained in:
Parvathi Mallampalli 2021-08-11 11:07:16 -07:00
parent 6d4c1b71b5
commit 83d2bcff44
6 changed files with 124 additions and 2 deletions

View File

@ -74,6 +74,23 @@ com.squareup.okhttp.Request signedRequest = new
## LWAAccessTokenCache
Interface to implement cache for access token that is returned in LWAClient and reuse the access token until time to live.
##RateLimitConfiguration
Interface to set and get rateLimit configurations that are used with RateLimiter. RateLimiter is used on client side to restrict the rate at which requests are made. RateLimiter Configuration takes Permit, rate which requests are made and TimeOut
*Example*
```
com.squareup.okhttp.Request request = new Request.Builder()
.url(...)
...
.build();
RateLimitConfiguration rateLimitOption = RateLimitConfigurationOnRequests.builder()
.rateLimitPermit(...)
.waitTimeOutInMilliSeconds(...)
.build();
```
## Resources
This package features Mustache templates designed for use with [swagger codegen](https://swagger.io/tools/swagger-codegen/).

View File

@ -107,13 +107,19 @@
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
<version>4.5.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-sts -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-sts</artifactId>
<version>1.11.236</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1-jre</version>
</dependency>
</dependencies>
</project>

View File

@ -53,6 +53,8 @@ import {{invokerPackage}}.auth.OAuth;
import com.amazon.SellingPartnerAPIAA.AWSSigV4Signer;
import com.amazon.SellingPartnerAPIAA.LWAAuthorizationSigner;
import com.google.common.util.concurrent.RateLimiter;
import com.amazon.SellingPartnerAPIAA.RateLimitConfiguration;
public class ApiClient {
@ -79,6 +81,8 @@ public class ApiClient {
private LWAAuthorizationSigner lwaAuthorizationSigner;
private AWSSigV4Signer awsSigV4Signer;
private RateLimiter rateLimiter;
private RateLimitConfiguration rateLimitConfiguration;
/*
* Constructor for ApiClient
@ -530,6 +534,23 @@ public class ApiClient {
return this;
}
/**
* Sets the RateLimiter
* A rate limiter is used to manage a high volume of traffic allowing N requests per second
* @return Api client
*/
public ApiClient setRateLimiter(RateLimitConfiguration rateLimitConfiguration) {
if (rateLimitConfiguration != null) {
rateLimiter = RateLimiter.create(rateLimitConfiguration.getRateLimitPermit());
//Add rateLimiter to httpclient interceptor for execute
RateLimitInterceptor rateLimiterInterceptor = new RateLimitInterceptor(rateLimiter, rateLimitConfiguration);
httpClient.interceptors().add(rateLimiterInterceptor);
}
return this;
}
/**
* Format the given parameter object into string.
*
@ -1256,3 +1277,29 @@ public class ApiClient {
}
}
}
class RateLimitInterceptor implements Interceptor {
RateLimiter rateLimiter;
RateLimitConfiguration rateLimitConfiguration;
public RateLimitInterceptor(RateLimiter rateLimiter, RateLimitConfiguration rateLimitConfiguration) {
this.rateLimiter = rateLimiter;
this.rateLimitConfiguration = rateLimitConfiguration;
}
@Override
public Response intercept(Chain chain) throws IOException {
if (rateLimitConfiguration.getTimeOut() == Long.MAX_VALUE) {
rateLimiter.acquire();
} else {
try {
if (!rateLimiter.tryAcquire(rateLimitConfiguration.getTimeOut(), TimeUnit.MILLISECONDS)) {
throw new ApiException("Throttled as per the ratelimiter on client");
}
} catch (ApiException e) {
e.printStackTrace();
}
}
return chain.proceed(chain.request());
}
}

View File

@ -50,6 +50,7 @@ import com.amazon.SellingPartnerAPIAA.LWAAccessTokenCache;
import com.amazon.SellingPartnerAPIAA.LWAAccessTokenCacheImpl;
import com.amazon.SellingPartnerAPIAA.LWAAuthorizationCredentials;
import com.amazon.SellingPartnerAPIAA.LWAAuthorizationSigner;
import com.amazon.SellingPartnerAPIAA.RateLimitConfiguration;
{{#operations}}
public class {{classname}} {
@ -281,6 +282,7 @@ public class {{classname}} {
private LWAAccessTokenCache lwaAccessTokenCache;
private Boolean disableAccessTokenCache = false;
private AWSAuthenticationCredentialsProvider awsAuthenticationCredentialsProvider;
private RateLimitConfiguration rateLimitConfiguration;
public Builder awsAuthenticationCredentials(AWSAuthenticationCredentials awsAuthenticationCredentials) {
this.awsAuthenticationCredentials = awsAuthenticationCredentials;
@ -312,6 +314,16 @@ public class {{classname}} {
return this;
}
public Builder rateLimitConfigurationOnRequests(RateLimitConfiguration rateLimitConfiguration){
this.rateLimitConfiguration = rateLimitConfiguration;
return this;
}
public Builder disableRateLimitOnRequests() {
this.rateLimitConfiguration = null;
return this;
}
public {{classname}} build() {
if (awsAuthenticationCredentials == null) {
@ -348,7 +360,8 @@ public class {{classname}} {
return new {{classname}}(new ApiClient()
.setAWSSigV4Signer(awsSigV4Signer)
.setLWAAuthorizationSigner(lwaAuthorizationSigner)
.setBasePath(endpoint));
.setBasePath(endpoint)
.setRateLimiter(rateLimitConfiguration));
}
}
}

View File

@ -0,0 +1,9 @@
package com.amazon.SellingPartnerAPIAA;
public interface RateLimitConfiguration {
Double getRateLimitPermit();
Long getTimeOut();
}

View File

@ -0,0 +1,30 @@
package com.amazon.SellingPartnerAPIAA;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class RateLimitConfigurationOnRequests implements RateLimitConfiguration {
/**
* RateLimiter Permit
*/
private Double rateLimitPermit;
/**
* Timeout for RateLimiter
*/
private Long waitTimeOutInMilliSeconds;
@Override
public Long getTimeOut() {
return waitTimeOutInMilliSeconds;
}
@Override
public Double getRateLimitPermit() {
return rateLimitPermit;
}
}