Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use FastThreadLocal for the Mac instances #621

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.security.SecureRandom;
import java.util.Arrays;

import io.netty.util.concurrent.FastThreadLocal;
import io.netty.util.internal.ObjectUtil;

import javax.crypto.Mac;
Expand All @@ -33,6 +34,13 @@
final class HmacSignQuicConnectionIdGenerator implements QuicConnectionIdGenerator {
static final QuicConnectionIdGenerator INSTANCE = new HmacSignQuicConnectionIdGenerator();

private static final FastThreadLocal<Mac> MACS = new FastThreadLocal<Mac>() {
@Override
protected Mac initialValue() {
return newMac();
}
};

private static final String ALGORITM = "HmacSHA256";
private static final byte[] randomKey = new byte[16];

Expand Down Expand Up @@ -66,11 +74,9 @@ public ByteBuffer newId(ByteBuffer buffer, int length) {
ObjectUtil.checkPositive(buffer.remaining(), "buffer");
ObjectUtil.checkInRange(length, 0, maxConnectionIdLength(), "length");


// TODO: Consider using a ThreadLocal.
Mac mac = newMac();
Mac mac = MACS.get();
mac.reset();
mac.update(buffer);

byte[] signBytes = mac.doFinal();
if (signBytes.length != length) {
signBytes = Arrays.copyOf(signBytes, length);
Expand Down