Skip to content

Commit 7b97f75

Browse files
authored
Merge pull requests #320
* Improve performance * Fix get headers issue
1 parent 58e1c71 commit 7b97f75

File tree

9 files changed

+130
-67
lines changed

9 files changed

+130
-67
lines changed

joylive-core/joylive-core-api/src/main/java/com/jd/live/agent/core/util/StringUtils.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public class StringUtils {
114114
/**
115115
* The empty String {@code ""}.
116116
*/
117-
public static final String EMPTY = "";
117+
public static final String EMPTY_STRING = "";
118118

119119
/**
120120
* Checks if the given character sequence is empty (null or has a length of 0).
@@ -601,7 +601,7 @@ public static String join(String[] values) {
601601
*/
602602
public static String join(String[] values, char separator) {
603603
if (values == null || values.length == 0) {
604-
return EMPTY;
604+
return EMPTY_STRING;
605605
}
606606
return join(Arrays.asList(values), null, separator, (char) 0, (char) 0, false);
607607
}
@@ -686,7 +686,7 @@ public static <T> String join(Iterable<T> values, char separator, char prefix, c
686686
*/
687687
public static <T> String join(Iterable<T> values, Function<T, String> converter, char separator, char prefix, char suffix, boolean singleSurrounding) {
688688
if (values == null) {
689-
return EMPTY;
689+
return EMPTY_STRING;
690690
} else if (values instanceof List) {
691691
return joinList((List<T>) values, converter, separator, prefix, suffix, singleSurrounding);
692692
} else if (values instanceof Collection) {
@@ -716,7 +716,7 @@ public static <T> String join(Iterable<T> values, Function<T, String> converter,
716716
*/
717717
private static <T> String joinList(List<T> values, Function<T, String> converter, char separator, char prefix, char suffix, boolean singleSurrounding) {
718718
if (values.isEmpty()) {
719-
return EMPTY;
719+
return EMPTY_STRING;
720720
} else if (values.size() == 1) {
721721
Object value = values.get(0);
722722
String str = value.toString();
@@ -746,7 +746,7 @@ private static <T> String joinList(List<T> values, Function<T, String> converter
746746
*/
747747
private static <T> String joinCollection(Collection<T> values, Function<T, String> converter, char separator, char prefix, char suffix, boolean singleSurrounding) {
748748
if (values.isEmpty()) {
749-
return EMPTY;
749+
return EMPTY_STRING;
750750
} else if (values.size() == 1) {
751751
Object value = values.iterator().next();
752752
String str = value.toString();
@@ -779,7 +779,7 @@ private static <T> String joinIterable(Iterable<T> values, Function<T, String> c
779779
int right = suffix == 0 ? 0 : 1;
780780
int counter = 0;
781781
StringBuilder sb = new StringBuilder();
782-
sb.append(left == 0 ? EMPTY : prefix);
782+
sb.append(left == 0 ? EMPTY_STRING : prefix);
783783
String str;
784784
for (T value : values) {
785785
str = converter == null ? (value == null ? null : value.toString()) : converter.apply(value);
@@ -790,10 +790,10 @@ private static <T> String joinIterable(Iterable<T> values, Function<T, String> c
790790
sb.append(str);
791791
}
792792
}
793-
sb.append(right == 0 ? EMPTY : suffix);
793+
sb.append(right == 0 ? EMPTY_STRING : suffix);
794794
switch (counter) {
795795
case 0:
796-
return EMPTY;
796+
return EMPTY_STRING;
797797
case 1:
798798
return singleSurrounding ? sb.toString() : sb.substring(left, sb.length() - right);
799799
default:

joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/registry/ServiceId.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,12 @@ public static String getNacosServiceName(String serviceNameWithGroup) {
178178
*/
179179
public static String getServiceName(String serviceNameWithGroup, String splitter, int index) {
180180
if (serviceNameWithGroup == null || serviceNameWithGroup.isEmpty()) {
181-
return StringUtils.EMPTY;
181+
return StringUtils.EMPTY_STRING;
182182
} else if (splitter == null || splitter.isEmpty()) {
183183
return serviceNameWithGroup;
184184
}
185185
List<String> parts = splitList(serviceNameWithGroup, splitter);
186-
return index < 0 || index >= parts.size() ? StringUtils.EMPTY : parts.get(index);
186+
return index < 0 || index >= parts.size() ? StringUtils.EMPTY_STRING : parts.get(index);
187187
}
188188

189189
}

joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/request/AbstractHttpRequest.java

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package com.jd.live.agent.governance.request;
1717

18-
import com.jd.live.agent.core.util.cache.CacheObject;
1918
import com.jd.live.agent.core.util.http.HttpHeader;
2019
import com.jd.live.agent.core.util.http.HttpUtils;
2120
import com.jd.live.agent.core.util.network.Ipv4;
@@ -28,8 +27,10 @@
2827
import java.util.Map;
2928
import java.util.Set;
3029

30+
import static com.jd.live.agent.core.util.StringUtils.EMPTY_STRING;
3131
import static com.jd.live.agent.core.util.http.HttpHeader.HOST;
3232
import static com.jd.live.agent.core.util.http.HttpUtils.newURI;
33+
import static java.util.Collections.EMPTY_MAP;
3334

3435
/**
3536
* Provides an abstract base class for HTTP requests, implementing the {@link HttpRequest} interface.
@@ -50,17 +51,17 @@ public abstract class AbstractHttpRequest<T> extends AbstractServiceRequest<T> i
5051
/**
5152
* Lazily evaluated, parsed cookies from the HTTP request.
5253
*/
53-
protected CacheObject<Map<String, List<String>>> cookies;
54+
protected Map<String, List<String>> cookies;
5455

5556
/**
5657
* Lazily evaluated, parsed query parameters from the HTTP request URL.
5758
*/
58-
protected CacheObject<Map<String, List<String>>> queries;
59+
protected Map<String, List<String>> queries;
5960

6061
/**
6162
* Lazily evaluated HTTP headers from the request.
6263
*/
63-
protected CacheObject<Map<String, List<String>>> headers;
64+
protected Map<String, List<String>> headers;
6465

6566
/**
6667
* The URI of the HTTP request.
@@ -70,12 +71,12 @@ public abstract class AbstractHttpRequest<T> extends AbstractServiceRequest<T> i
7071
/**
7172
* Lazily evaluated host of the request URI.
7273
*/
73-
protected CacheObject<Address> address;
74+
protected Address address;
7475

7576
/**
7677
* Lazily evaluated scheme of the request URI.
7778
*/
78-
protected CacheObject<String> schema;
79+
protected String schema;
7980

8081
/**
8182
* Constructs an instance of {@code AbstractHttpRequest} with the original request object.
@@ -99,22 +100,20 @@ public URI getURI() {
99100
@Override
100101
public String getSchema() {
101102
if (schema == null) {
102-
schema = new CacheObject<>(parseScheme());
103+
String v = parseScheme();
104+
schema = v == null ? EMPTY_STRING : v;
103105
}
104-
String result = schema.get();
105-
return result == null || result.isEmpty() ? null : result;
106+
return schema.isEmpty() ? null : schema;
106107
}
107108

108109
@Override
109110
public Integer getPort() {
110-
Address addr = getAddress();
111-
return addr == null ? null : addr.getPort();
111+
return getAddress().getPort();
112112
}
113113

114114
@Override
115115
public String getHost() {
116-
Address addr = getAddress();
117-
return addr == null ? null : addr.getHost();
116+
return getAddress().getHost();
118117
}
119118

120119
@Override
@@ -123,27 +122,33 @@ public String getPath() {
123122
}
124123

125124
@Override
125+
@SuppressWarnings("unchecked")
126126
public Map<String, List<String>> getHeaders() {
127127
if (headers == null) {
128-
headers = new CacheObject<>(parseHeaders());
128+
Map<String, List<String>> map = parseHeaders();
129+
headers = map == null ? EMPTY_MAP : map;
129130
}
130-
return headers.get();
131+
return headers;
131132
}
132133

133134
@Override
135+
@SuppressWarnings("unchecked")
134136
public Map<String, List<String>> getQueries() {
135137
if (queries == null) {
136-
queries = new CacheObject<>(parseQueries());
138+
Map<String, List<String>> map = parseQueries();
139+
queries = map == null ? EMPTY_MAP : map;
137140
}
138-
return queries.get();
141+
return queries;
139142
}
140143

141144
@Override
145+
@SuppressWarnings("unchecked")
142146
public Map<String, List<String>> getCookies() {
143147
if (cookies == null) {
144-
cookies = new CacheObject<>(parseCookies());
148+
Map<String, List<String>> map = parseCookies();
149+
cookies = map == null ? EMPTY_MAP : map;
145150
}
146-
return cookies.get();
151+
return cookies;
147152
}
148153

149154
@Override
@@ -163,9 +168,10 @@ public String getMethod() {
163168

164169
protected Address getAddress() {
165170
if (address == null) {
166-
address = new CacheObject<>(parseAddress());
171+
Address v = parseAddress();
172+
address = v == null ? Address.EMPTY : v;
167173
}
168-
return address.get();
174+
return address;
169175
}
170176

171177
/**
@@ -288,6 +294,8 @@ protected Map<String, List<String>> parseQueries() {
288294
@Getter
289295
protected static class Address {
290296

297+
public static final Address EMPTY = new Address(null, null);
298+
291299
private final String host;
292300

293301
private final Integer port;

joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/response/AbstractHttpResponse.java

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package com.jd.live.agent.governance.response;
1717

18-
import com.jd.live.agent.core.util.cache.CacheObject;
1918
import com.jd.live.agent.core.util.http.HttpHeader;
2019
import com.jd.live.agent.governance.exception.ErrorPredicate;
2120
import com.jd.live.agent.governance.exception.ServiceError;
@@ -25,6 +24,9 @@
2524
import java.util.Map;
2625
import java.util.function.Supplier;
2726

27+
import static com.jd.live.agent.core.util.StringUtils.EMPTY_STRING;
28+
import static java.util.Collections.EMPTY_MAP;
29+
2830
/**
2931
* AbstractHttpResponse
3032
*
@@ -45,12 +47,12 @@ public abstract class AbstractHttpResponse<T> extends AbstractServiceResponse<T>
4547
/**
4648
* Lazily evaluated, parsed cookies from the HTTP request.
4749
*/
48-
protected CacheObject<Map<String, List<String>>> cookies;
50+
protected Map<String, List<String>> cookies;
4951

5052
/**
5153
* Lazily evaluated HTTP headers from the request.
5254
*/
53-
protected CacheObject<Map<String, List<String>>> headers;
55+
protected Map<String, List<String>> headers;
5456

5557
/**
5658
* The URI of the HTTP request.
@@ -60,19 +62,19 @@ public abstract class AbstractHttpResponse<T> extends AbstractServiceResponse<T>
6062
/**
6163
* Lazily evaluated port number of the request URI.
6264
*/
63-
protected CacheObject<Integer> port;
65+
protected Integer port;
6466

6567
/**
6668
* Lazily evaluated host of the request URI.
6769
*/
68-
protected CacheObject<String> host;
70+
protected String host;
6971

7072
/**
7173
* Lazily evaluated scheme of the request URI.
7274
*/
73-
protected CacheObject<String> schema;
75+
protected String schema;
7476

75-
protected CacheObject<String> contentType;
77+
protected String contentType;
7678

7779
/**
7880
* Constructs an instance of {@code AbstractHttpResponse} with the original response object.
@@ -123,27 +125,27 @@ public URI getURI() {
123125
@Override
124126
public String getSchema() {
125127
if (schema == null) {
126-
schema = new CacheObject<>(parseScheme());
128+
String v = parseScheme();
129+
schema = v == null ? EMPTY_STRING : v;
127130
}
128-
String result = schema.get();
129-
return result == null || result.isEmpty() ? null : result;
131+
return schema.isEmpty() ? null : schema;
130132
}
131133

132134
@Override
133135
public int getPort() {
134136
if (port == null) {
135-
port = new CacheObject<>(parsePort());
137+
port = parsePort();
136138
}
137-
return port.get();
139+
return port;
138140
}
139141

140142
@Override
141143
public String getHost() {
142144
if (host == null) {
143-
host = new CacheObject<>(parseHost());
145+
String v = parseHost();
146+
host = v == null ? EMPTY_STRING : v;
144147
}
145-
String result = host.get();
146-
return result == null || result.isEmpty() ? null : result;
148+
return host.isEmpty() ? null : host;
147149
}
148150

149151
@Override
@@ -152,35 +154,41 @@ public String getPath() {
152154
}
153155

154156
@Override
157+
@SuppressWarnings("unchecked")
155158
public Map<String, List<String>> getHeaders() {
156159
if (headers == null) {
157-
headers = new CacheObject<>(parseHeaders());
160+
Map<String, List<String>> map = parseHeaders();
161+
// not null
162+
headers = map == null ? EMPTY_MAP : map;
158163
}
159-
return headers.get();
164+
return headers;
160165
}
161166

162167
@Override
168+
@SuppressWarnings("unchecked")
163169
public Map<String, List<String>> getCookies() {
164170
if (cookies == null) {
165-
cookies = new CacheObject<>(parseCookies());
171+
Map<String, List<String>> map = parseCookies();
172+
// not null
173+
cookies = map == null ? EMPTY_MAP : map;
166174
}
167-
return cookies.get();
175+
return cookies;
168176
}
169177

170178
@Override
171179
public String getCookie(String key) {
172180
if (key == null) return null;
173-
Map<String, List<String>> cookies = getCookies();
174-
List<String> values = cookies == null ? null : cookies.get(key);
181+
List<String> values = getCookies().get(key);
175182
return values == null || values.isEmpty() ? null : values.get(0);
176183
}
177184

178185
@Override
179186
public String getContentType() {
180187
if (contentType == null) {
181-
contentType = new CacheObject<>(getHeader(HttpHeader.CONTENT_TYPE));
188+
String header = getHeader(HttpHeader.CONTENT_TYPE);
189+
contentType = header == null ? EMPTY_STRING : header;
182190
}
183-
return contentType.get();
191+
return contentType.isEmpty() ? null : contentType;
184192
}
185193

186194
/**

0 commit comments

Comments
 (0)