Skip to content

Commit c303dc0

Browse files
authored
fix: for perf improvement using fast random (#49)
* fix: for perf improvemet using fast random * fix spotless issues * addressed the latest comments * need to extend supress warning for jackson lib * addressed the comments * pushing refactored code
1 parent cb3600a commit c303dc0

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.hypertrace.core.grpcutils.context;
2+
3+
import java.util.UUID;
4+
import java.util.concurrent.ThreadLocalRandom;
5+
6+
public class FastUUIDGenerator {
7+
/**
8+
* This function generates UUIDs using ThreadLocalRandom, which is faster and doesn't block like
9+
* the default randomUUID method that relies on /dev/random. It's suitable for most random UUID
10+
* needs.
11+
*/
12+
public static UUID randomUUID() {
13+
long mostSigBits = ThreadLocalRandom.current().nextLong();
14+
long leastSigBits = ThreadLocalRandom.current().nextLong();
15+
16+
// Set the version (4) For random UUID
17+
mostSigBits &= 0xFFFFFFFFFFFF0FFFL;
18+
mostSigBits |= 0x0000000000004000L;
19+
// Set variant to RFC 4122
20+
leastSigBits &= 0x3FFFFFFFFFFFFFFFL;
21+
leastSigBits |= 0x8000000000000000L;
22+
23+
return new UUID(mostSigBits, leastSigBits);
24+
}
25+
}

grpc-context-utils/src/main/java/org/hypertrace/core/grpcutils/context/RequestContext.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.util.Map;
1919
import java.util.Optional;
2020
import java.util.Set;
21-
import java.util.UUID;
2221
import java.util.concurrent.Callable;
2322
import java.util.stream.Collectors;
2423
import javax.annotation.Nonnull;
@@ -34,7 +33,9 @@ public class RequestContext {
3433
public static RequestContext forTenantId(String tenantId) {
3534
return new RequestContext()
3635
.put(RequestContextConstants.TENANT_ID_HEADER_KEY, tenantId)
37-
.put(RequestContextConstants.REQUEST_ID_HEADER_KEY, UUID.randomUUID().toString());
36+
.put(
37+
RequestContextConstants.REQUEST_ID_HEADER_KEY,
38+
FastUUIDGenerator.randomUUID().toString());
3839
}
3940

4041
public static RequestContext fromMetadata(Metadata metadata) {

owasp-suppressions.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<packageUrl regex="true">^pkg:maven/org\.hypertrace\..*@.*$</packageUrl>
88
<cpe>cpe:/a:grpc:grpc</cpe>
99
</suppress>
10-
<suppress until="2023-08-31Z">
10+
<suppress until="2023-09-30Z">
1111
<notes><![CDATA[
1212
file name: jackson-databind-2.14.2.jar
1313
This is currently disputed.

0 commit comments

Comments
 (0)