Skip to content

Commit 3610b6e

Browse files
committed
Optimize connections, HttpURLConnection to HttpClient
1 parent e2cdc17 commit 3610b6e

7 files changed

Lines changed: 190 additions & 196 deletions

File tree

src/main/java/me/Logicism/JavaHordeBridge/HordeBridge.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
public class HordeBridge {
2020

21-
public static int BRIDGE_VERSION = 3;
21+
public static int BRIDGE_VERSION = 4;
2222
public static String BRIDGE_AGENT = "Java Horde Bridge:" + BRIDGE_VERSION + ":https://github.com/LogicismDev/Java-Horde-Bridge";
2323

2424
public static HordeBridge INSTANCE;

src/main/java/me/Logicism/JavaHordeBridge/core/InterrogationGenerator.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
import me.Logicism.JavaHordeBridge.HordeBridge;
44
import me.Logicism.JavaHordeBridge.network.BrowserClient;
55
import me.Logicism.JavaHordeBridge.network.BrowserData;
6-
import org.json.JSONArray;
76
import org.json.JSONException;
87
import org.json.JSONObject;
98

109
import java.io.IOException;
10+
import java.net.URISyntaxException;
1111
import java.net.URL;
1212
import java.util.HashMap;
1313
import java.util.Map;
@@ -31,10 +31,10 @@ public JSONObject startCaptionGeneration(String imageURL) {
3131
headers.put("User-Agent", "Java 11 / Java Horde Bridge " + HordeBridge.BRIDGE_VERSION);
3232
while (retryCount < 5) {
3333
try {
34-
BrowserData generationData = BrowserClient.executePOSTRequest(new URL(kaiURL + "/caption"), new JSONObject().put("url", imageURL).toString(), headers);
34+
BrowserData generationData = BrowserClient.executePOSTRequestString(new URL(kaiURL + "/caption"), new JSONObject().put("url", imageURL).toString(), headers);
3535

3636
if (generationData.getResponseCode() == 200) {
37-
generationObject = new JSONObject(BrowserClient.requestToString(generationData.getResponse()));
37+
generationObject = new JSONObject(generationData.getResponseString());
3838

3939
break;
4040
} else if (generationData.getResponseCode() == 500) {
@@ -59,7 +59,7 @@ public JSONObject startCaptionGeneration(String imageURL) {
5959
TimeUnit.SECONDS.sleep(5);
6060
} catch (InterruptedException ignored) {
6161
}
62-
} catch (IOException e) {
62+
} catch (IOException | InterruptedException | URISyntaxException e) {
6363
bridge.getLogger().debug("Client is unavailable (attempt " + retryCount++ + "), retrying generation in 5 seconds...");
6464

6565
try {
@@ -73,6 +73,7 @@ public JSONObject startCaptionGeneration(String imageURL) {
7373
generationObject = new JSONObject().put("error", "The generation could not be completed due to an unknown error");
7474
}
7575

76+
bridge.getLogger().info("Generated: " + generationObject);
7677
return generationObject;
7778
}
7879

@@ -84,10 +85,10 @@ public JSONObject startSafetyCheckGeneration(String imageURL) {
8485
headers.put("User-Agent", "Java 11 / Java Horde Bridge " + HordeBridge.BRIDGE_VERSION);
8586
while (retryCount < 5) {
8687
try {
87-
BrowserData generationData = BrowserClient.executePOSTRequest(new URL(kaiURL + "/safetycheck"), new JSONObject().put("url", imageURL).toString(), headers);
88+
BrowserData generationData = BrowserClient.executePOSTRequestString(new URL(kaiURL + "/safetycheck"), new JSONObject().put("url", imageURL).toString(), headers);
8889

8990
if (generationData.getResponseCode() == 200) {
90-
generationObject = new JSONObject(BrowserClient.requestToString(generationData.getResponse()));
91+
generationObject = new JSONObject(generationData.getResponseString());
9192

9293
break;
9394
} else if (generationData.getResponseCode() == 500) {
@@ -112,7 +113,7 @@ public JSONObject startSafetyCheckGeneration(String imageURL) {
112113
TimeUnit.SECONDS.sleep(5);
113114
} catch (InterruptedException ignored) {
114115
}
115-
} catch (IOException e) {
116+
} catch (IOException | InterruptedException | URISyntaxException e) {
116117
bridge.getLogger().debug("Client is unavailable (attempt " + retryCount++ + "), retrying generation in 5 seconds...");
117118

118119
try {
@@ -126,6 +127,7 @@ public JSONObject startSafetyCheckGeneration(String imageURL) {
126127
generationObject = new JSONObject().put("error", "The generation could not be completed due to an unknown error");
127128
}
128129

130+
bridge.getLogger().info("Generated: " + generationObject);
129131
return generationObject;
130132
}
131133

@@ -136,11 +138,12 @@ public JSONObject startInterrogationGeneration(String imageURL) {
136138
headers.put("Content-Type", "application/json");
137139
headers.put("User-Agent", "Java 11 / Java Horde Bridge " + HordeBridge.BRIDGE_VERSION);
138140
while (retryCount < 5) {
141+
bridge.getLogger().info("Image: " + imageURL);
139142
try {
140143
BrowserData generationData = BrowserClient.executePOSTRequest(new URL(kaiURL + "/interrogate"), new JSONObject().put("url", imageURL).toString(), headers);
141144

142145
if (generationData.getResponseCode() == 200) {
143-
generationObject = new JSONObject(BrowserClient.requestToString(generationData.getResponse()));
146+
generationObject = new JSONObject(generationData.getResponseString());
144147

145148
break;
146149
} else if (generationData.getResponseCode() == 500) {
@@ -165,7 +168,7 @@ public JSONObject startInterrogationGeneration(String imageURL) {
165168
TimeUnit.SECONDS.sleep(5);
166169
} catch (InterruptedException ignored) {
167170
}
168-
} catch (IOException e) {
171+
} catch (IOException | InterruptedException | URISyntaxException e) {
169172
bridge.getLogger().debug("Client is unavailable (attempt " + retryCount++ + "), retrying generation in 5 seconds...");
170173

171174
try {
@@ -179,6 +182,7 @@ public JSONObject startInterrogationGeneration(String imageURL) {
179182
generationObject = new JSONObject().put("error", "The generation could not be completed due to an unknown error");
180183
}
181184

185+
bridge.getLogger().info("Generated: " + generationObject);
182186
return generationObject;
183187
}
184188

@@ -193,7 +197,7 @@ public JSONObject startStripBackgroundGeneration(String imageURL) {
193197
BrowserData generationData = BrowserClient.executePOSTRequest(new URL(kaiURL + "/stripbackground"), new JSONObject().put("url", imageURL).toString(), headers);
194198

195199
if (generationData.getResponseCode() == 200) {
196-
generationObject = new JSONObject(BrowserClient.requestToString(generationData.getResponse()));
200+
generationObject = new JSONObject(generationData.getResponseString());
197201

198202
break;
199203
} else if (generationData.getResponseCode() == 500) {
@@ -218,7 +222,7 @@ public JSONObject startStripBackgroundGeneration(String imageURL) {
218222
TimeUnit.SECONDS.sleep(5);
219223
} catch (InterruptedException ignored) {
220224
}
221-
} catch (IOException e) {
225+
} catch (IOException | InterruptedException | URISyntaxException e) {
222226
bridge.getLogger().debug("Client is unavailable (attempt " + retryCount++ + "), retrying generation in 5 seconds...");
223227

224228
try {
@@ -232,11 +236,12 @@ public JSONObject startStripBackgroundGeneration(String imageURL) {
232236
generationObject = new JSONObject().put("error", "The generation could not be completed due to an unknown error");
233237
}
234238

239+
bridge.getLogger().info("Generated: " + generationObject);
235240
return generationObject;
236241
}
237242

238-
public boolean validateClient() throws IOException {
239-
BrowserData bd = BrowserClient.executeGETRequest(new URL(kaiURL + "/health"), null);
243+
public boolean validateClient() throws IOException, URISyntaxException, InterruptedException {
244+
BrowserData bd = BrowserClient.executeGETRequestDiscard(new URL(kaiURL + "/health"), new HashMap<>());
240245

241246
return bd.getResponseCode() == 200;
242247
}

src/main/java/me/Logicism/JavaHordeBridge/core/KAIGenerator.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.json.JSONObject;
99

1010
import java.io.IOException;
11+
import java.net.URISyntaxException;
1112
import java.net.URL;
1213
import java.util.HashMap;
1314
import java.util.Map;
@@ -34,10 +35,9 @@ public String startGeneration(JSONObject payload) {
3435
BrowserData generationData = BrowserClient.executePOSTRequest(new URL(kaiURL + "/api/latest/generate"), payload.toString(), headers);
3536

3637
if (generationData.getResponseCode() == 200) {
37-
JSONObject generationObject = new JSONObject(BrowserClient.requestToString(generationData.getResponse()));
38+
JSONObject generationObject = new JSONObject(generationData.getResponseString());
3839

3940
generation = generationObject.getJSONArray("results").getJSONObject(0).getString("text");
40-
4141
break;
4242
} else if (generationData.getResponseCode() == 422) {
4343
generation = "payload validation error";
@@ -65,7 +65,7 @@ public String startGeneration(JSONObject payload) {
6565
TimeUnit.SECONDS.sleep(5);
6666
} catch (InterruptedException ignored) {
6767
}
68-
} catch (IOException e) {
68+
} catch (IOException | InterruptedException | URISyntaxException e) {
6969
bridge.getLogger().debug("Client is unavailable (attempt " + retryCount++ + "), retrying generation in 5 seconds...");
7070

7171
try {
@@ -78,17 +78,17 @@ public String startGeneration(JSONObject payload) {
7878
return generation;
7979
}
8080

81-
public JSONObject validateClient(String kaiName, String[] priorityUsernames) throws IOException {
81+
public JSONObject validateClient(String kaiName, String[] priorityUsernames) throws IOException, URISyntaxException, InterruptedException {
8282
JSONObject clientData = null;
8383
while (clientData == null) {
84-
BrowserData bd = BrowserClient.executeGETRequest(new URL(kaiURL + "/api/latest/model/"), null);
85-
JSONObject modelObject = new JSONObject(BrowserClient.requestToString(bd.getResponse()));
84+
BrowserData bd = BrowserClient.executeGETRequestString(new URL(kaiURL + "/api/latest/model/"), new HashMap<>());
85+
JSONObject modelObject = new JSONObject(bd.getResponseString());
8686

87-
bd = BrowserClient.executeGETRequest(new URL(kaiURL + "/api/latest/config/max_context_length/"), null);
88-
JSONObject maxContextLengthObject = new JSONObject(BrowserClient.requestToString(bd.getResponse()));
87+
bd = BrowserClient.executeGETRequestString(new URL(kaiURL + "/api/latest/config/max_context_length/"), new HashMap<>());
88+
JSONObject maxContextLengthObject = new JSONObject(bd.getResponseString());
8989

90-
bd = BrowserClient.executeGETRequest(new URL(kaiURL + "/api/latest/config/max_length/"), null);
91-
JSONObject maxLengthObject = new JSONObject(BrowserClient.requestToString(bd.getResponse()));
90+
bd = BrowserClient.executeGETRequestString(new URL(kaiURL + "/api/latest/config/max_length/"), new HashMap<>());
91+
JSONObject maxLengthObject = new JSONObject(bd.getResponseString());
9292

9393
String model = modelObject.getString("result");
9494
if (!model.contains("/")) {
@@ -107,8 +107,8 @@ public JSONObject validateClient(String kaiName, String[] priorityUsernames) thr
107107
clientData.put("priority_usernames", priorityUsernameArrays);
108108
}
109109

110-
bd = BrowserClient.executeGETRequest(new URL(kaiURL + "/api/latest/config/soft_prompts_list/"), null);
111-
JSONObject softPromptsListObject = new JSONObject(BrowserClient.requestToString(bd.getResponse()));
110+
bd = BrowserClient.executeGETRequestString(new URL(kaiURL + "/api/latest/config/soft_prompts_list/"), new HashMap<>());
111+
JSONObject softPromptsListObject = new JSONObject(bd.getResponseString());
112112

113113
clientData.put("softprompts", softPromptsListObject.has(modelObject.getString("result")) ? softPromptsListObject.getJSONArray(modelObject.getString("result")) : new JSONArray());
114114

0 commit comments

Comments
 (0)