mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
perf(sqs): hash message MD5s with the optimized Digests helper (#7563)
This commit is contained in:
1 parent
c767d2deb5
commit
4adde48324
2 files changed
+5
-41
No files matched your search
@@ -14,6 +14,7 @@ import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsEndpoints;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.AwsRegions;
|
||||
import io.github.hectorvent.floci.core.common.Digests;
|
||||
import io.github.hectorvent.floci.core.common.PaginatedResult;
|
||||
import io.github.hectorvent.floci.core.common.Pagination;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
@@ -37,8 +38,6 @@ import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.time.Clock;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
@@ -47,7 +46,6 @@ import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.HexFormat;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -112,7 +110,6 @@ public class SqsService implements Resettable, ResourceProvider {
|
||||
return t;
|
||||
});
|
||||
private final AtomicLong sequenceCounter = new AtomicLong(0);
|
||||
private static final HexFormat HEX = HexFormat.of();
|
||||
|
||||
private record RedrivePolicy(int maxReceiveCount, String deadLetterTargetArn) {
|
||||
}
|
||||
@@ -1071,7 +1068,7 @@ public class SqsService implements Resettable, ResourceProvider {
|
||||
String dedupId = messageDeduplicationId;
|
||||
if (dedupId == null || dedupId.isEmpty()) {
|
||||
if ("true".equalsIgnoreCase(queue.getAttributes().get("ContentBasedDeduplication"))) {
|
||||
dedupId = computeMd5(body);
|
||||
dedupId = Digests.md5Hex(body.getBytes(StandardCharsets.UTF_8));
|
||||
} else {
|
||||
throw new AwsException("InvalidParameterValue",
|
||||
"The queue should either have ContentBasedDeduplication enabled or " +
|
||||
@@ -1446,17 +1443,6 @@ public class SqsService implements Resettable, ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
private static String computeMd5(String input) {
|
||||
try {
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
byte[] digest = md.digest(input.getBytes(StandardCharsets.UTF_8));
|
||||
return HEX.formatHex(digest);
|
||||
} catch (NoSuchAlgorithmException ignored) {
|
||||
// MD5 is guaranteed to be available in the standard JDK runtime
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public List<Message> receiveMessage(String queueUrl, int maxMessages, int visibilityTimeout,
|
||||
Integer waitTimeSeconds, String region) {
|
||||
String storageKey = regionKey(region, queueUrl);
|
||||
|
||||
@@ -2,13 +2,12 @@ package io.github.hectorvent.floci.services.sqs.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.github.hectorvent.floci.core.common.Digests;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
@@ -66,7 +65,7 @@ public class Message {
|
||||
this.messageAttributes = new HashMap<>();
|
||||
this.sentTimestamp = Instant.now();
|
||||
this.receiveCount = 0;
|
||||
this.md5OfBody = computeMd5(body);
|
||||
this.md5OfBody = Digests.md5Hex(body.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
/** A detached snapshot that retains this message's identity and delivery state. */
|
||||
@@ -196,7 +195,6 @@ public class Message {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
DataOutputStream dos = new DataOutputStream(bos);
|
||||
|
||||
@@ -225,30 +223,10 @@ public class Message {
|
||||
}
|
||||
}
|
||||
|
||||
byte[] digest = md.digest(bos.toByteArray());
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (byte b : digest) {
|
||||
sb.append(String.format("%02x", b));
|
||||
}
|
||||
return sb.toString();
|
||||
return Digests.md5Hex(bos.toByteArray());
|
||||
} catch (Exception ignored) {
|
||||
// Attribute serialization failure falls back to null MD5
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static String computeMd5(String input) {
|
||||
try {
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
byte[] digest = md.digest(input.getBytes(StandardCharsets.UTF_8));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (byte b : digest) {
|
||||
sb.append(String.format("%02x", b));
|
||||
}
|
||||
return sb.toString();
|
||||
} catch (NoSuchAlgorithmException ignored) {
|
||||
// MD5 is always available in the standard JDK runtime
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user