enqueue most post/put/delete requests

Instead of using the global SingleThreadedExecutor for some requests,
use enqueue method of okhttp.
This should hopefully help with some plugins, and more specifically the
ui from freezing while the online api is down.
This commit is contained in:
Jeremy Plsek
2018-05-28 12:54:36 +01:00
parent 794ead0007
commit 025ed157f3
8 changed files with 117 additions and 101 deletions

View File

@@ -30,6 +30,8 @@ import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.UUID; import java.util.UUID;
import net.runelite.http.api.RuneLiteAPI; import net.runelite.http.api.RuneLiteAPI;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.HttpUrl; import okhttp3.HttpUrl;
import okhttp3.MediaType; import okhttp3.MediaType;
import okhttp3.Request; import okhttp3.Request;
@@ -75,7 +77,7 @@ public class ConfigClient
} }
} }
public void set(String key, String value) throws IOException public void set(String key, String value)
{ {
HttpUrl url = RuneLiteAPI.getApiBase().newBuilder() HttpUrl url = RuneLiteAPI.getApiBase().newBuilder()
.addPathSegment("config") .addPathSegment("config")
@@ -90,13 +92,24 @@ public class ConfigClient
.url(url) .url(url)
.build(); .build();
try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute()) RuneLiteAPI.CLIENT.newCall(request).enqueue(new Callback()
{ {
logger.debug("Set configuration value '{}' to '{}'", key, value); @Override
} public void onFailure(Call call, IOException e)
{
logger.warn("Unable to synchronize configuration item", e);
}
@Override
public void onResponse(Call call, Response response)
{
response.close();
logger.debug("Synchronized configuration value '{}' to '{}'", key, value);
}
});
} }
public void unset(String key) throws IOException public void unset(String key)
{ {
HttpUrl url = RuneLiteAPI.getApiBase().newBuilder() HttpUrl url = RuneLiteAPI.getApiBase().newBuilder()
.addPathSegment("config") .addPathSegment("config")
@@ -111,9 +124,20 @@ public class ConfigClient
.url(url) .url(url)
.build(); .build();
try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute()) RuneLiteAPI.CLIENT.newCall(request).enqueue(new Callback()
{ {
logger.debug("Unset configuration value '{}'", key); @Override
} public void onFailure(Call call, IOException e)
{
logger.warn("Unable to unset configuration item", e);
}
@Override
public void onResponse(Call call, Response response)
{
response.close();
logger.debug("Unset configuration value '{}'", key);
}
});
} }
} }

View File

@@ -26,6 +26,8 @@ package net.runelite.http.api.examine;
import java.io.IOException; import java.io.IOException;
import net.runelite.http.api.RuneLiteAPI; import net.runelite.http.api.RuneLiteAPI;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.HttpUrl; import okhttp3.HttpUrl;
import okhttp3.MediaType; import okhttp3.MediaType;
import okhttp3.Request; import okhttp3.Request;
@@ -40,22 +42,22 @@ public class ExamineClient
private static final MediaType TEXT = MediaType.parse("text"); private static final MediaType TEXT = MediaType.parse("text");
public void submitObject(int id, String text) throws IOException public void submitObject(int id, String text)
{ {
submit("object", id, text); submit("object", id, text);
} }
public void submitNpc(int id, String text) throws IOException public void submitNpc(int id, String text)
{ {
submit("npc", id, text); submit("npc", id, text);
} }
public void submitItem(int id, String text) throws IOException public void submitItem(int id, String text)
{ {
submit("item", id, text); submit("item", id, text);
} }
private void submit(String type, int id, String text) throws IOException private void submit(String type, int id, String text)
{ {
HttpUrl url = RuneLiteAPI.getApiBase().newBuilder() HttpUrl url = RuneLiteAPI.getApiBase().newBuilder()
.addPathSegment("examine") .addPathSegment("examine")
@@ -70,10 +72,20 @@ public class ExamineClient
.post(RequestBody.create(TEXT, text)) .post(RequestBody.create(TEXT, text))
.build(); .build();
try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute()) RuneLiteAPI.CLIENT.newCall(request).enqueue(new Callback()
{ {
logger.debug("Submitted examine info for {} {}: {}", @Override
type, id, text); public void onFailure(Call call, IOException e)
} {
logger.warn("Error submitting examine", e);
}
@Override
public void onResponse(Call call, Response response)
{
response.close();
logger.debug("Submitted examine info for {} {}: {}", type, id, text);
}
});
} }
} }

View File

@@ -26,12 +26,19 @@ package net.runelite.http.api.xp;
import java.io.IOException; import java.io.IOException;
import net.runelite.http.api.RuneLiteAPI; import net.runelite.http.api.RuneLiteAPI;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.HttpUrl; import okhttp3.HttpUrl;
import okhttp3.Request; import okhttp3.Request;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class XpClient public class XpClient
{ {
public void update(String username) throws IOException private static final Logger logger = LoggerFactory.getLogger(XpClient.class);
public void update(String username)
{ {
HttpUrl url = RuneLiteAPI.getApiBase().newBuilder() HttpUrl url = RuneLiteAPI.getApiBase().newBuilder()
.addPathSegment("xp") .addPathSegment("xp")
@@ -43,6 +50,20 @@ public class XpClient
.url(url) .url(url)
.build(); .build();
RuneLiteAPI.CLIENT.newCall(request).execute().close(); RuneLiteAPI.CLIENT.newCall(request).enqueue(new Callback()
{
@Override
public void onFailure(Call call, IOException e)
{
logger.warn("Error submitting xp track", e);
}
@Override
public void onResponse(Call call, Response response)
{
response.close();
logger.debug("Submitted xp track for {}", username);
}
});
} }
} }

View File

@@ -31,6 +31,8 @@ import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.List; import java.util.List;
import net.runelite.http.api.RuneLiteAPI; import net.runelite.http.api.RuneLiteAPI;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.HttpUrl; import okhttp3.HttpUrl;
import okhttp3.MediaType; import okhttp3.MediaType;
import okhttp3.Request; import okhttp3.Request;
@@ -45,7 +47,7 @@ public class XteaClient
private static final Logger logger = LoggerFactory.getLogger(XteaClient.class); private static final Logger logger = LoggerFactory.getLogger(XteaClient.class);
public Response submit(int revision, int region, int[] keys) throws IOException public void submit(int revision, int region, int[] keys)
{ {
XteaRequest xteaRequest = new XteaRequest(); XteaRequest xteaRequest = new XteaRequest();
xteaRequest.setRevision(revision); xteaRequest.setRevision(revision);
@@ -69,7 +71,30 @@ public class XteaClient
.url(url) .url(url)
.build(); .build();
return RuneLiteAPI.CLIENT.newCall(request).execute(); RuneLiteAPI.CLIENT.newCall(request).enqueue(new Callback()
{
@Override
public void onFailure(Call call, IOException e)
{
logger.warn("unable to submit xtea keys", e);
}
@Override
public void onResponse(Call call, Response response)
{
try
{
if (!response.isSuccessful())
{
logger.debug("unsuccessful xtea response");
}
}
finally
{
response.close();
}
}
});
} }
public List<XteaKey> get() throws IOException public List<XteaKey> get() throws IOException

View File

@@ -265,19 +265,7 @@ public class ConfigManager
if (client != null) if (client != null)
{ {
Runnable task = () -> client.set(groupName + "." + key, value);
{
try
{
client.set(groupName + "." + key, value);
}
catch (IOException ex)
{
log.warn("unable to set configuration item", ex);
}
};
executor.execute(task);
} }
Runnable task = () -> Runnable task = () ->
@@ -315,19 +303,7 @@ public class ConfigManager
if (client != null) if (client != null)
{ {
final Runnable task = () -> client.unset(groupName + "." + key);
{
try
{
client.unset(groupName + "." + key);
}
catch (IOException ex)
{
log.warn("unable to set configuration item", ex);
}
};
executor.execute(task);
} }
Runnable task = () -> Runnable task = () ->

View File

@@ -231,7 +231,7 @@ public class ExaminePlugin extends Plugin
} }
cache.put(key, Boolean.TRUE); cache.put(key, Boolean.TRUE);
executor.submit(() -> submitExamine(pendingExamine, event.getMessage())); submitExamine(pendingExamine, event.getMessage());
} }
private void findExamineItem(PendingExamine pendingExamine) private void findExamineItem(PendingExamine pendingExamine)
@@ -404,24 +404,17 @@ public class ExaminePlugin extends Plugin
{ {
int id = examine.getId(); int id = examine.getId();
try switch (examine.getType())
{ {
switch (examine.getType()) case ITEM:
{ examineClient.submitItem(id, text);
case ITEM: break;
examineClient.submitItem(id, text); case OBJECT:
break; examineClient.submitObject(id, text);
case OBJECT: break;
examineClient.submitObject(id, text); case NPC:
break; examineClient.submitNpc(id, text);
case NPC: break;
examineClient.submitNpc(id, text);
break;
}
}
catch (IOException ex)
{
log.warn("Error submitting examine", ex);
} }
} }

View File

@@ -32,7 +32,6 @@ import java.awt.image.BufferedImage;
import java.io.IOException; import java.io.IOException;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.ScheduledExecutorService;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.inject.Inject; import javax.inject.Inject;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -71,9 +70,6 @@ public class XpTrackerPlugin extends Plugin
@Inject @Inject
private SkillIconManager skillIconManager; private SkillIconManager skillIconManager;
@Inject
private ScheduledExecutorService executor;
private NavigationButton navButton; private NavigationButton navButton;
private XpPanel xpPanel; private XpPanel xpPanel;
@@ -164,19 +160,7 @@ public class XpTrackerPlugin extends Plugin
String username = local != null ? local.getName() : null; String username = local != null ? local.getName() : null;
if (username != null) if (username != null)
{ {
log.debug("Submitting xp track for {}", username); xpClient.update(username);
executor.submit(() ->
{
try
{
xpClient.update(username);
}
catch (IOException ex)
{
log.warn("error submitting xp track", ex);
}
});
} }
} }
} }

View File

@@ -25,10 +25,8 @@
package net.runelite.client.plugins.xtea; package net.runelite.client.plugins.xtea;
import com.google.common.eventbus.Subscribe; import com.google.common.eventbus.Subscribe;
import java.io.IOException;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ScheduledExecutorService;
import javax.inject.Inject; import javax.inject.Inject;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.runelite.api.Client; import net.runelite.api.Client;
@@ -36,7 +34,6 @@ import net.runelite.api.events.MapRegionChanged;
import net.runelite.client.plugins.Plugin; import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDescriptor; import net.runelite.client.plugins.PluginDescriptor;
import net.runelite.http.api.xtea.XteaClient; import net.runelite.http.api.xtea.XteaClient;
import okhttp3.Response;
@PluginDescriptor( @PluginDescriptor(
name = "Xtea", name = "Xtea",
@@ -52,9 +49,6 @@ public class XteaPlugin extends Plugin
@Inject @Inject
private Client client; private Client client;
@Inject
private ScheduledExecutorService executor;
@Subscribe @Subscribe
public void onMapRegionChanged(MapRegionChanged event) public void onMapRegionChanged(MapRegionChanged event)
{ {
@@ -82,19 +76,6 @@ public class XteaPlugin extends Plugin
sentRegions.add(region); sentRegions.add(region);
executor.execute(() -> xteaClient.submit(revision, region, keys);
{
try (Response response = xteaClient.submit(revision, region, keys))
{
if (!response.isSuccessful())
{
log.debug("unsuccessful xtea response");
}
}
catch (IOException ex)
{
log.debug("unable to submit xtea keys", ex);
}
});
} }
} }