Rate Limiter(1)OnFlight Control and Rate Limiter
From my understanding, there are 2 ways to control the speed.
On my case, I send requests too fast to SOLR7. I guess there is jetty there on SOLR7 server side.
And it will put the pending requests in queue or something, finally, it will time out if the queue is too large.
Control Message In Flight
import java.util.concurrent.Semaphore;
private final Semaphore inflightMsg;
So when I start to send the message, I will do
this.inflightMsg.acquire();
And after I get response back from the remote server, I will release the resource I acquire
logger.trace("Worker {} - Request for more SQS messages", workerId);
SqsFutureReceiver<ReceiveMessageRequest, ReceiveMessageResult> processSqsMessageFuture = new SqsFutureReceiver<>();
sqsClient.receiveMessageAsync(receiveMessageRequest, processSqsMessageFuture);
logger.trace("Worker {} - Block thread for response", workerId);
ReceiveMessageResult response = processSqsMessageFuture.get();
logger.trace("Worker {} - Process response in a non-blocking fashion", workerId);
processSqsMessages(sqsClient, solrClient, response).thenAccept(batchMessageProcessedCount -> {
logger.trace("Update message count");
long totalMessageProcessed = messageCount.addAndGet(batchMessageProcessedCount);
// log rates
if (totalMessageProcessed % 100 == 0) {
long currentTime = System.currentTimeMillis();
long duration = (currentTime - STARTTIME) / 1000;
logger.info("Worker {} - Message count {}; {} messages/second", workerId, totalMessageProcessed,
totalMessageProcessed * 10 / duration);
}
}).whenComplete((value, ex)->{
this.inflightMsg.release();
});
Here is how I init the messages in flight
new Semaphore(inflightLimit)
Permits Per Second
pom.xml configuration
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.2-jre</version>
</dependency>
Code Sample
package com.sillycat.jobssolrconsumer.service;
import java.time.ZonedDateTime;
import org.junit.Assert;
import org.junit.Test;
import com.google.common.util.concurrent.RateLimiter;
public class RateLimitServiceTest {
@Test
public void dummy() {
Assert.assertTrue(true);
}
@Test
public void givenLimitedResource(){
//100 permits per second
RateLimiter rateLimiter = RateLimiter.create(100);
long startTime = ZonedDateTime.now().getSecond();
rateLimiter.acquire(100);
System.out.println(ZonedDateTime.now() + " - I am flying like a funny bird 1.");
long elaspedTimeSeconds = ZonedDateTime.now().getSecond() - startTime;
Assert.assertTrue(elaspedTimeSeconds <= 1);
rateLimiter.acquire(100);
System.out.println(ZonedDateTime.now() + " - I am flying like a funny bird 2.");
elaspedTimeSeconds = ZonedDateTime.now().getSecond() - startTime;
Assert.assertTrue(elaspedTimeSeconds >= 1);
}
}
Reference:
http://www.baeldung.com/guava-rate-limiter
https://stackoverflow.com/questions/31883739/throttling-method-calls-using-guava-ratelimiter-class
From my understanding, there are 2 ways to control the speed.
On my case, I send requests too fast to SOLR7. I guess there is jetty there on SOLR7 server side.
And it will put the pending requests in queue or something, finally, it will time out if the queue is too large.
Control Message In Flight
import java.util.concurrent.Semaphore;
private final Semaphore inflightMsg;
So when I start to send the message, I will do
this.inflightMsg.acquire();
And after I get response back from the remote server, I will release the resource I acquire
logger.trace("Worker {} - Request for more SQS messages", workerId);
SqsFutureReceiver<ReceiveMessageRequest, ReceiveMessageResult> processSqsMessageFuture = new SqsFutureReceiver<>();
sqsClient.receiveMessageAsync(receiveMessageRequest, processSqsMessageFuture);
logger.trace("Worker {} - Block thread for response", workerId);
ReceiveMessageResult response = processSqsMessageFuture.get();
logger.trace("Worker {} - Process response in a non-blocking fashion", workerId);
processSqsMessages(sqsClient, solrClient, response).thenAccept(batchMessageProcessedCount -> {
logger.trace("Update message count");
long totalMessageProcessed = messageCount.addAndGet(batchMessageProcessedCount);
// log rates
if (totalMessageProcessed % 100 == 0) {
long currentTime = System.currentTimeMillis();
long duration = (currentTime - STARTTIME) / 1000;
logger.info("Worker {} - Message count {}; {} messages/second", workerId, totalMessageProcessed,
totalMessageProcessed * 10 / duration);
}
}).whenComplete((value, ex)->{
this.inflightMsg.release();
});
Here is how I init the messages in flight
new Semaphore(inflightLimit)
Permits Per Second
pom.xml configuration
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.2-jre</version>
</dependency>
Code Sample
package com.sillycat.jobssolrconsumer.service;
import java.time.ZonedDateTime;
import org.junit.Assert;
import org.junit.Test;
import com.google.common.util.concurrent.RateLimiter;
public class RateLimitServiceTest {
@Test
public void dummy() {
Assert.assertTrue(true);
}
@Test
public void givenLimitedResource(){
//100 permits per second
RateLimiter rateLimiter = RateLimiter.create(100);
long startTime = ZonedDateTime.now().getSecond();
rateLimiter.acquire(100);
System.out.println(ZonedDateTime.now() + " - I am flying like a funny bird 1.");
long elaspedTimeSeconds = ZonedDateTime.now().getSecond() - startTime;
Assert.assertTrue(elaspedTimeSeconds <= 1);
rateLimiter.acquire(100);
System.out.println(ZonedDateTime.now() + " - I am flying like a funny bird 2.");
elaspedTimeSeconds = ZonedDateTime.now().getSecond() - startTime;
Assert.assertTrue(elaspedTimeSeconds >= 1);
}
}
Reference:
http://www.baeldung.com/guava-rate-limiter
https://stackoverflow.com/questions/31883739/throttling-method-calls-using-guava-ratelimiter-class
本文介绍了一种针对SOLR7服务的请求限速策略,通过使用Guava库中的RateLimiter来实现对请求速率的有效控制,并探讨了另一种通过控制消息飞行中的数量来避免请求队列过载的方法。
269

被折叠的 条评论
为什么被折叠?



