From 27caa0098a5dfb437bd746e165e5368432ee813b Mon Sep 17 00:00:00 2001 From: 0xb00bface <0xboobface@gmail.com> Date: Sun, 25 Jul 2021 14:56:48 +0200 Subject: [PATCH] Cache Chaturbate stream info for 2 seconds to reduce requests --- .../sites/chaturbate/ChaturbateModel.java | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/common/src/main/java/ctbrec/sites/chaturbate/ChaturbateModel.java b/common/src/main/java/ctbrec/sites/chaturbate/ChaturbateModel.java index 9587f4c5..b3e472f0 100644 --- a/common/src/main/java/ctbrec/sites/chaturbate/ChaturbateModel.java +++ b/common/src/main/java/ctbrec/sites/chaturbate/ChaturbateModel.java @@ -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);