Skip to content
Merged
Show file tree
Hide file tree
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 @@ -53,6 +53,7 @@
import org.apache.dubbo.rpc.protocol.tri.call.UnaryClientCallListener;
import org.apache.dubbo.rpc.protocol.tri.compressor.Compressor;
import org.apache.dubbo.rpc.protocol.tri.compressor.Identity;
import org.apache.dubbo.rpc.protocol.tri.h12.grpc.GrpcUtils;
import org.apache.dubbo.rpc.protocol.tri.observer.ClientCallToObserverAdapter;
import org.apache.dubbo.rpc.protocol.tri.transport.TripleWriteQueue;
import org.apache.dubbo.rpc.service.ServiceDescriptorInternalCache;
Expand All @@ -65,6 +66,7 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

import io.netty.util.AsciiString;
Expand Down Expand Up @@ -326,7 +328,7 @@ RequestMetadata createRequest(MethodDescriptor methodDescriptor, Invocation invo
meta.version = url.getVersion();
meta.acceptEncoding = acceptEncodings;
if (timeout != null) {
meta.timeout = timeout + "m";
meta.timeout = GrpcUtils.getTimeoutHeaderValue(Long.valueOf(timeout), TimeUnit.MILLISECONDS);
}
String application = (String) invocation.getObjectAttachmentWithoutConvert(CommonConstants.APPLICATION_KEY);
if (application == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,43 @@ public static Long parseTimeoutToMills(String timeoutVal) {
}
}

/**
* Converts a timeout value to the gRPC `grpc-timeout` ASCII string format.
* <p>
* This method applies a greedy strategy: it chooses the largest possible unit
* (nanos, micros, millis, seconds, minutes, hours) such that the numeric part is
* less than 100_000_000, as required by the gRPC specification.
* For example, a 1-second timeout will be encoded as "1000000u" (in microseconds).
* </p>
*
* @param timeout the timeout value
* @param unit the time unit of the timeout
* @return a string suitable for use as the value of the gRPC `grpc-timeout` header
* @throws IllegalArgumentException if the timeout too small
*/
public static String getTimeoutHeaderValue(Long timeout, TimeUnit unit) {
long timeoutNanos = timeout;
if (unit != TimeUnit.NANOSECONDS) {
timeoutNanos = unit.toNanos(timeout);
}
final long cutoff = 100_000_000L;
if (timeoutNanos < 0) {
throw new IllegalArgumentException("Timeout too small");
} else if (timeoutNanos < cutoff) {
return timeoutNanos + "n";
} else if (timeoutNanos < cutoff * 1_000L) {
return TimeUnit.NANOSECONDS.toMicros(timeoutNanos) + "u";
} else if (timeoutNanos < cutoff * 1_000_000L) {
return TimeUnit.NANOSECONDS.toMillis(timeoutNanos) + "m";
} else if (timeoutNanos < cutoff * 1_000_000_000L) {
return TimeUnit.NANOSECONDS.toSeconds(timeoutNanos) + "S";
} else if (timeoutNanos < cutoff * 1_000_000_000L * 60L) {
return TimeUnit.NANOSECONDS.toMinutes(timeoutNanos) + "M";
} else {
return TimeUnit.NANOSECONDS.toHours(timeoutNanos) + "H";
}
}

public static boolean isGrpcRequest(String contentType) {
return contentType != null && contentType.startsWith(MediaType.APPLICATION_GRPC.getName());
}
Expand Down
Loading