Cache Chaturbate stream info for 2 seconds to reduce requests
This commit is contained in:
parent
ff0864bbf7
commit
27caa0098a
|
@ -8,6 +8,8 @@ import java.io.ByteArrayInputStream;
|
|||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
@ -45,6 +47,7 @@ public class ChaturbateModel extends AbstractModel { // NOSONAR
|
|||
private static final Logger LOG = LoggerFactory.getLogger(ChaturbateModel.class);
|
||||
private int[] resolution = new int[2];
|
||||
private transient StreamInfo streamInfo;
|
||||
private transient Instant lastStreamInfoRequest = Instant.EPOCH;
|
||||
|
||||
/**
|
||||
* This constructor exists only for deserialization. Please don't call it directly
|
||||
|
@ -59,7 +62,7 @@ public class ChaturbateModel extends AbstractModel { // NOSONAR
|
|||
@Override
|
||||
public boolean isOnline(boolean ignoreCache) throws IOException, ExecutionException, InterruptedException {
|
||||
String roomStatus;
|
||||
if(ignoreCache) {
|
||||
if (ignoreCache) {
|
||||
StreamInfo info = loadStreamInfo();
|
||||
roomStatus = Optional.ofNullable(info).map(i -> i.room_status).orElse("");
|
||||
LOG.trace("Model {} room status: {}", getName(), info.room_status);
|
||||
|
@ -99,16 +102,11 @@ public class ChaturbateModel extends AbstractModel { // NOSONAR
|
|||
|
||||
@Override
|
||||
public State getOnlineState(boolean failFast) throws IOException, ExecutionException {
|
||||
if(failFast) {
|
||||
if (failFast) {
|
||||
setOnlineStateByRoomStatus(Optional.ofNullable(streamInfo).map(si -> si.room_status).orElse("Unknown"));
|
||||
} else {
|
||||
try {
|
||||
streamInfo = loadStreamInfo();
|
||||
setOnlineStateByRoomStatus(streamInfo.room_status);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new ExecutionException(e);
|
||||
}
|
||||
streamInfo = loadStreamInfo();
|
||||
setOnlineStateByRoomStatus(streamInfo.room_status);
|
||||
}
|
||||
return onlineState;
|
||||
}
|
||||
|
@ -259,7 +257,10 @@ public class ChaturbateModel extends AbstractModel { // NOSONAR
|
|||
}
|
||||
}
|
||||
|
||||
private StreamInfo loadStreamInfo() throws IOException, InterruptedException {
|
||||
private StreamInfo loadStreamInfo() throws IOException {
|
||||
if (Duration.between(lastStreamInfoRequest, Instant.now()).getSeconds() < 2) {
|
||||
return streamInfo;
|
||||
}
|
||||
RequestBody body = new FormBody.Builder()
|
||||
.add("room_slug", getName())
|
||||
.add("bandwidth", "high")
|
||||
|
@ -271,6 +272,7 @@ public class ChaturbateModel extends AbstractModel { // NOSONAR
|
|||
.header(X_REQUESTED_WITH, XML_HTTP_REQUEST)
|
||||
.build();
|
||||
try (Response response = getSite().getHttpClient().execute(req)) {
|
||||
lastStreamInfoRequest = Instant.now();
|
||||
if (response.isSuccessful()) {
|
||||
String content = response.body().string();
|
||||
LOG.trace("Raw stream info: {}", content);
|
||||
|
|
Loading…
Reference in New Issue