HttpSimpleClient优化

This commit is contained in:
redkale
2023-11-27 22:50:09 +08:00
parent 252a45fbb4
commit e192d37bf0
5 changed files with 32 additions and 23 deletions

View File

@@ -27,7 +27,7 @@ import org.redkale.util.*;
@ResourceType(AsyncGroup.class) @ResourceType(AsyncGroup.class)
public class AsyncIOGroup extends AsyncGroup { public class AsyncIOGroup extends AsyncGroup {
private boolean started; private final AtomicBoolean started = new AtomicBoolean();
private boolean skipClose; private boolean skipClose;
@@ -108,12 +108,10 @@ public class AsyncIOGroup extends AsyncGroup {
@Override @Override
public AsyncGroup start() { public AsyncGroup start() {
if (started) {
return this;
}
if (closed.get()) { if (closed.get()) {
throw new RedkaleException("group is closed"); throw new RedkaleException("group is closed");
} }
if (started.compareAndSet(false, true)) {
for (int i = 0; i < this.ioReadThreads.length; i++) { for (int i = 0; i < this.ioReadThreads.length; i++) {
this.ioReadThreads[i].start(); this.ioReadThreads[i].start();
if (this.ioWriteThreads[i] != this.ioReadThreads[i]) { if (this.ioWriteThreads[i] != this.ioReadThreads[i]) {
@@ -121,7 +119,7 @@ public class AsyncIOGroup extends AsyncGroup {
} }
} }
//connectThread用时才初始化 //connectThread用时才初始化
started = true; }
return this; return this;
} }

View File

@@ -12,6 +12,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.function.Predicate;
import org.redkale.convert.TextConvert; import org.redkale.convert.TextConvert;
import org.redkale.convert.json.JsonConvert; import org.redkale.convert.json.JsonConvert;
import org.redkale.util.RedkaleException; import org.redkale.util.RedkaleException;
@@ -103,8 +104,14 @@ public class HttpHeaders implements RestHeaders, Serializable {
@Override @Override
public void forEach(BiConsumer<String, String> consumer) { public void forEach(BiConsumer<String, String> consumer) {
forEach((Predicate) null, consumer);
}
@Override
public void forEach(Predicate<String> filter, BiConsumer<String, String> consumer) {
if (map != null) { if (map != null) {
map.forEach((k, v) -> { map.forEach((k, v) -> {
if (filter == null || filter.test(k)) {
if (v instanceof Collection) { if (v instanceof Collection) {
for (Object item : (Collection) v) { for (Object item : (Collection) v) {
consumer.accept(k, item == null ? null : item.toString()); consumer.accept(k, item == null ? null : item.toString());
@@ -112,6 +119,7 @@ public class HttpHeaders implements RestHeaders, Serializable {
} else { } else {
consumer.accept(k, v == null ? null : v.toString()); consumer.accept(k, v == null ? null : v.toString());
} }
}
}); });
} }
} }

View File

@@ -246,18 +246,17 @@ public class HttpSimpleClient extends Client<HttpSimpleConnection, HttpSimpleReq
array.put((method.toUpperCase() + " " + (urlpos > 0 ? url.substring(urlpos) : "/") + " HTTP/1.1\r\n").getBytes(StandardCharsets.UTF_8)); array.put((method.toUpperCase() + " " + (urlpos > 0 ? url.substring(urlpos) : "/") + " HTTP/1.1\r\n").getBytes(StandardCharsets.UTF_8));
array.put(("Host: " + uri.getHost() + "\r\n").getBytes(StandardCharsets.UTF_8)); array.put(("Host: " + uri.getHost() + "\r\n").getBytes(StandardCharsets.UTF_8));
array.put(("Content-Length: " + (body == null ? 0 : body.length) + "\r\n").getBytes(StandardCharsets.UTF_8)); array.put(HttpSimpleRequest.contentLengthBytes(body));
if (headers == null || !headers.containsIgnoreCase("User-Agent")) { if (headers == null || !headers.containsIgnoreCase("User-Agent")) {
array.put(header_bytes_useragent); array.put(header_bytes_useragent);
} }
if (headers == null || !headers.containsIgnoreCase("Connection")) {
array.put(header_bytes_connclose); array.put(header_bytes_connclose);
}
if (headers == null || !headers.containsIgnoreCase(Rest.REST_HEADER_TRACEID)) { if (headers == null || !headers.containsIgnoreCase(Rest.REST_HEADER_TRACEID)) {
array.put((Rest.REST_HEADER_TRACEID + ": " + traceid + "\r\n").getBytes(StandardCharsets.UTF_8)); array.put((Rest.REST_HEADER_TRACEID + ": " + traceid + "\r\n").getBytes(StandardCharsets.UTF_8));
} }
if (headers != null) { if (headers != null) {
headers.forEach((k, v) -> array.put((k + ": " + v + "\r\n").getBytes(StandardCharsets.UTF_8))); headers.forEach(k -> !k.equalsIgnoreCase("Connection") && !k.equalsIgnoreCase("Content-Length"),
(k, v) -> array.put((k + ": " + v + "\r\n").getBytes(StandardCharsets.UTF_8)));
} }
array.put((byte) '\r', (byte) '\n'); array.put((byte) '\r', (byte) '\n');
if (body != null) { if (body != null) {

View File

@@ -147,7 +147,8 @@ public class HttpSimpleRequest extends ClientRequest implements java.io.Serializ
array.put(("Content-Type: " + contentType0 + "\r\n").getBytes(StandardCharsets.UTF_8)); array.put(("Content-Type: " + contentType0 + "\r\n").getBytes(StandardCharsets.UTF_8));
array.put(contentLengthBytes(clientBody)); array.put(contentLengthBytes(clientBody));
if (headers != null) { if (headers != null) {
headers.forEach((k, v) -> array.put((k + ": " + v + "\r\n").getBytes(StandardCharsets.UTF_8))); headers.forEach(k -> !k.equalsIgnoreCase("Content-Type") && !k.equalsIgnoreCase("Content-Length"),
(k, v) -> array.put((k + ": " + v + "\r\n").getBytes(StandardCharsets.UTF_8)));
} }
array.put((byte) '\r', (byte) '\n'); array.put((byte) '\r', (byte) '\n');
//写body //写body
@@ -160,7 +161,7 @@ public class HttpSimpleRequest extends ClientRequest implements java.io.Serializ
return headers != null && headers.containsIgnoreCase(name); return headers != null && headers.containsIgnoreCase(name);
} }
protected byte[] contentLengthBytes(byte[] clientBody) { protected static byte[] contentLengthBytes(byte[] clientBody) {
int len = clientBody == null ? 0 : clientBody.length; int len = clientBody == null ? 0 : clientBody.length;
if (len < contentLengthArray.length) { if (len < contentLengthArray.length) {
return contentLengthArray[len]; return contentLengthArray[len];

View File

@@ -9,6 +9,7 @@ import java.io.Serializable;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.function.Predicate;
/** /**
* 用于RestService类的方法的参数获取HttpHeaders * 用于RestService类的方法的参数获取HttpHeaders
@@ -30,6 +31,8 @@ public interface RestHeaders {
public void forEach(BiConsumer<String, String> consumer); public void forEach(BiConsumer<String, String> consumer);
public void forEach(Predicate<String> filter, BiConsumer<String, String> consumer);
public String[] names(); public String[] names();
public boolean contains(String name); public boolean contains(String name);