优化HttpRequest

This commit is contained in:
Redkale
2023-01-06 16:05:48 +08:00
parent 158ea68265
commit 159c7f9e1b
3 changed files with 29 additions and 17 deletions

View File

@@ -14,7 +14,7 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.LongAdder; import java.util.concurrent.atomic.LongAdder;
import java.util.function.Consumer; import java.util.function.Consumer;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import org.redkale.util.ByteBufferWriter; import org.redkale.util.*;
/** /**
* *
@@ -27,7 +27,7 @@ import org.redkale.util.ByteBufferWriter;
*/ */
abstract class AsyncNioConnection extends AsyncConnection { abstract class AsyncNioConnection extends AsyncConnection {
protected static final int MAX_INVOKER_ONSTACK = Integer.getInteger("redkale.net.invoker.max.onstack", 8); protected static final int MAX_INVOKER_ONSTACK = Integer.getInteger("redkale.net.invoker.max.onstack", Utility.cpus());
final AsyncIOThread connectThread; final AsyncIOThread connectThread;

View File

@@ -79,7 +79,9 @@ public class ClientAddress implements java.io.Serializable {
private static SocketAddress[] createAddressArray(List<WeightAddress> ws) { private static SocketAddress[] createAddressArray(List<WeightAddress> ws) {
int min = 0; int min = 0;
int size = 0; //20,35,45去掉最大公约数数组长度为:4+7+9=20
for (WeightAddress w : ws) { for (WeightAddress w : ws) {
size += w.getWeight();
if (min == 0 || w.getWeight() < min) { if (min == 0 || w.getWeight() < min) {
min = w.getWeight(); min = w.getWeight();
} }
@@ -97,10 +99,7 @@ public class ClientAddress implements java.io.Serializable {
divisor = i; divisor = i;
} }
} }
int size = 0; //20,35,45去掉最大公约数数组长度为:4+7+9=20 size /= divisor;
for (WeightAddress w : ws) {
size += w.getWeight() / divisor;
}
SocketAddress[] newAddrs = new SocketAddress[size]; SocketAddress[] newAddrs = new SocketAddress[size];
int index = -1; int index = -1;
for (int i = 0; i < ws.size(); i++) { for (int i = 0; i < ws.size(); i++) {

View File

@@ -484,14 +484,26 @@ public class HttpRequest extends Request<HttpContext> {
} }
size = bytes.length(); size = bytes.length();
byte[] content = bytes.content(); byte[] content = bytes.content();
if (size == 3 && content[0] == 'G' && content[1] == 'E' && content[2] == 'T') { if (size == 3) {
this.method = KEY_GET; if (content[0] == 'G' && content[1] == 'E' && content[2] == 'T') {
this.getmethod = true; this.method = KEY_GET;
} else if (size == 4 && content[0] == 'P' && content[1] == 'O' && content[2] == 'S' && content[3] == 'T') { this.getmethod = true;
this.method = KEY_POST; } else if (content[0] == 'P' && content[1] == 'U' && content[2] == 'T') {
this.method = "PUT";
this.getmethod = false;
} else {
this.method = bytes.toString(true, charset);
this.getmethod = false;
}
} else if (size == 4) {
this.getmethod = false; this.getmethod = false;
if (content[0] == 'P' && content[1] == 'O' && content[2] == 'S' && content[3] == 'T') {
this.method = KEY_POST;
} else {
this.method = bytes.toString(true, charset);
}
} else { } else {
this.method = bytes.toString(charset); this.method = bytes.toString(true, charset);
this.getmethod = false; this.getmethod = false;
} }
bytes.clear(); bytes.clear();
@@ -522,11 +534,12 @@ public class HttpRequest extends Request<HttpContext> {
size = bytes.length(); size = bytes.length();
if (qst > 0) { if (qst > 0) {
this.requestURI = decodeable ? toDecodeString(bytes, 0, qst, charset) : bytes.toString(latin1, 0, qst, charset); this.requestURI = decodeable ? toDecodeString(bytes, 0, qst, charset) : bytes.toString(latin1, 0, qst, charset);
this.queryBytes = bytes.getBytes(qst + 1, size - qst - 1); int qlen = size - qst - 1;
this.queryBytes = bytes.getBytes(qst + 1, qlen);
this.lastRequestURIString = null; this.lastRequestURIString = null;
this.lastRequestURIBytes = null; this.lastRequestURIBytes = null;
try { try {
addParameter(bytes, qst + 1, size - qst - 1); addParameter(bytes, qst + 1, qlen);
} catch (Exception e) { } catch (Exception e) {
this.context.getLogger().log(Level.WARNING, "HttpRequest.addParameter error: " + bytes.toString(), e); this.context.getLogger().log(Level.WARNING, "HttpRequest.addParameter error: " + bytes.toString(), e);
} }
@@ -582,7 +595,7 @@ public class HttpRequest extends Request<HttpContext> {
} else if (size == 8 && content[0] == 'H' && content[5] == '2' && content[7] == '0') { } else if (size == 8 && content[0] == 'H' && content[5] == '2' && content[7] == '0') {
this.protocol = KEY_HTTP_2_0; this.protocol = KEY_HTTP_2_0;
} else { } else {
this.protocol = bytes.toString(charset); this.protocol = bytes.toString(true, charset);
} }
bytes.clear(); bytes.clear();
return 0; return 0;
@@ -985,9 +998,9 @@ public class HttpRequest extends Request<HttpContext> {
byte[] content = array.content(); byte[] content = array.content();
if (len == 1) { if (len == 1) {
return Character.toString(content[offset]); return Character.toString(content[offset]);
} else if (len == 2 && content[offset] >= '0' && content[offset] <= '9') { } else if (len == 2 && content[offset] >= 0x20 && content[offset] < 0x80) {
return new String(content, 0, offset, len); return new String(content, 0, offset, len);
} else if (len == 3 && content[offset + 1] >= '0' && content[offset + 1] <= '9') { } else if (len == 3 && content[offset + 1] >= 0x20 && content[offset + 1] < 0x80) {
return new String(content, 0, offset, len); return new String(content, 0, offset, len);
} }
int start = offset; int start = offset;