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)
public class AsyncIOGroup extends AsyncGroup {
private boolean started;
private final AtomicBoolean started = new AtomicBoolean();
private boolean skipClose;
@@ -108,20 +108,18 @@ public class AsyncIOGroup extends AsyncGroup {
@Override
public AsyncGroup start() {
if (started) {
return this;
}
if (closed.get()) {
throw new RedkaleException("group is closed");
}
for (int i = 0; i < this.ioReadThreads.length; i++) {
this.ioReadThreads[i].start();
if (this.ioWriteThreads[i] != this.ioReadThreads[i]) {
this.ioWriteThreads[i].start();
if (started.compareAndSet(false, true)) {
for (int i = 0; i < this.ioReadThreads.length; i++) {
this.ioReadThreads[i].start();
if (this.ioWriteThreads[i] != this.ioReadThreads[i]) {
this.ioWriteThreads[i].start();
}
}
//connectThread用时才初始化
}
//connectThread用时才初始化
started = true;
return this;
}

View File

@@ -12,6 +12,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Predicate;
import org.redkale.convert.TextConvert;
import org.redkale.convert.json.JsonConvert;
import org.redkale.util.RedkaleException;
@@ -103,14 +104,21 @@ public class HttpHeaders implements RestHeaders, Serializable {
@Override
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) {
map.forEach((k, v) -> {
if (v instanceof Collection) {
for (Object item : (Collection) v) {
consumer.accept(k, item == null ? null : item.toString());
if (filter == null || filter.test(k)) {
if (v instanceof Collection) {
for (Object item : (Collection) v) {
consumer.accept(k, item == null ? null : item.toString());
}
} else {
consumer.accept(k, v == null ? null : v.toString());
}
} else {
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(("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")) {
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)) {
array.put((Rest.REST_HEADER_TRACEID + ": " + traceid + "\r\n").getBytes(StandardCharsets.UTF_8));
}
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');
if (body != null) {
@@ -336,7 +335,7 @@ public class HttpSimpleClient extends Client<HttpSimpleConnection, HttpSimpleReq
public void readInIOThread(CompletionHandler<Integer, ByteBuffer> handler) {
this.channel.readInIOThread(handler);
}
public void write(ByteTuple array, CompletionHandler<Integer, Void> handler) {
this.channel.write(array, handler);
}

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(contentLengthBytes(clientBody));
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');
//写body
@@ -160,7 +161,7 @@ public class HttpSimpleRequest extends ClientRequest implements java.io.Serializ
return headers != null && headers.containsIgnoreCase(name);
}
protected byte[] contentLengthBytes(byte[] clientBody) {
protected static byte[] contentLengthBytes(byte[] clientBody) {
int len = clientBody == null ? 0 : clientBody.length;
if (len < contentLengthArray.length) {
return contentLengthArray[len];

View File

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