Move request throttle for Chaturbate to ChaturbateHttpClient
This commit is contained in:
parent
602c81d18d
commit
dbb44e1374
|
@ -5,6 +5,7 @@ import static ctbrec.io.HttpConstants.*;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.concurrent.Semaphore;
|
||||||
|
|
||||||
import org.jsoup.nodes.Element;
|
import org.jsoup.nodes.Element;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
@ -24,6 +25,9 @@ public class ChaturbateHttpClient extends HttpClient {
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(ChaturbateHttpClient.class);
|
private static final Logger LOG = LoggerFactory.getLogger(ChaturbateHttpClient.class);
|
||||||
protected String token;
|
protected String token;
|
||||||
|
|
||||||
|
private static Semaphore requestThrottle = new Semaphore(2, true);
|
||||||
|
private static long lastRequest = 0;
|
||||||
|
|
||||||
public ChaturbateHttpClient() {
|
public ChaturbateHttpClient() {
|
||||||
super("chaturbate");
|
super("chaturbate");
|
||||||
}
|
}
|
||||||
|
@ -125,8 +129,40 @@ public class ChaturbateHttpClient extends HttpClient {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Response execute(Request req) throws IOException {
|
public Response execute(Request req) throws IOException {
|
||||||
|
boolean throttled = req.url().host().contains("chaturbate.com");
|
||||||
|
return executeThrottled(req, throttled);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Response executeThrottled(Request req, boolean throttle) throws IOException {
|
||||||
|
try {
|
||||||
|
if (throttle) {
|
||||||
|
acquireSlot();
|
||||||
|
}
|
||||||
Response resp = super.execute(req);
|
Response resp = super.execute(req);
|
||||||
extractCsrfToken(req);
|
extractCsrfToken(req);
|
||||||
return resp;
|
return resp;
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
throw new IOException("INterrupted during request", e);
|
||||||
|
} finally {
|
||||||
|
if (throttle) {
|
||||||
|
releaseSlot();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void acquireSlot() throws InterruptedException {
|
||||||
|
long pauseBetweenRequests = 500;
|
||||||
|
requestThrottle.acquire();
|
||||||
|
long now = System.currentTimeMillis();
|
||||||
|
long millisSinceLastRequest = now - lastRequest;
|
||||||
|
if (millisSinceLastRequest < pauseBetweenRequests) {
|
||||||
|
Thread.sleep(pauseBetweenRequests - millisSinceLastRequest);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void releaseSlot() {
|
||||||
|
lastRequest = System.currentTimeMillis();
|
||||||
|
requestThrottle.release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,6 @@ import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.Semaphore;
|
|
||||||
|
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
@ -47,8 +46,6 @@ public class ChaturbateModel extends AbstractModel { // NOSONAR
|
||||||
private int[] resolution = new int[2];
|
private int[] resolution = new int[2];
|
||||||
private transient StreamInfo streamInfo;
|
private transient StreamInfo streamInfo;
|
||||||
private long streamInfoTimestamp = 0;
|
private long streamInfoTimestamp = 0;
|
||||||
private static Semaphore requestThrottle = new Semaphore(2, true);
|
|
||||||
private static long lastRequest = 0;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This constructor exists only for deserialization. Please don't call it directly
|
* This constructor exists only for deserialization. Please don't call it directly
|
||||||
|
@ -270,8 +267,6 @@ public class ChaturbateModel extends AbstractModel { // NOSONAR
|
||||||
return streamInfo;
|
return streamInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
acquireSlot();
|
|
||||||
try {
|
|
||||||
RequestBody body = new FormBody.Builder()
|
RequestBody body = new FormBody.Builder()
|
||||||
.add("room_slug", getName())
|
.add("room_slug", getName())
|
||||||
.add("bandwidth", "high")
|
.add("bandwidth", "high")
|
||||||
|
@ -297,9 +292,6 @@ public class ChaturbateModel extends AbstractModel { // NOSONAR
|
||||||
throw new HttpException(code, message);
|
throw new HttpException(code, message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
releaseSlot();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private int[] getResolution() throws IOException, ParseException, PlaylistException, InterruptedException {
|
private int[] getResolution() throws IOException, ParseException, PlaylistException, InterruptedException {
|
||||||
|
@ -362,21 +354,4 @@ public class ChaturbateModel extends AbstractModel { // NOSONAR
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void acquireSlot() throws InterruptedException {
|
|
||||||
//LOG.debug("Acquire: {}", requestThrottle.availablePermits());
|
|
||||||
requestThrottle.acquire();
|
|
||||||
long now = System.currentTimeMillis();
|
|
||||||
long millisSinceLastRequest = now - lastRequest;
|
|
||||||
if(millisSinceLastRequest < 500) {
|
|
||||||
//LOG.debug("Sleeping: {}", (500-millisSinceLastRequest));
|
|
||||||
Thread.sleep(500 - millisSinceLastRequest);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void releaseSlot() {
|
|
||||||
lastRequest = System.currentTimeMillis();
|
|
||||||
requestThrottle.release();
|
|
||||||
//LOG.debug("Release: {}", requestThrottle.availablePermits());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue