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

opt: client supports requests to server-streaming endpoints without parameters #15029

Merged
merged 17 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat: server stream supports requests without parameters
  • Loading branch information
funky-eyes committed Dec 30, 2024
commit c1c47caf37ead8479bfcaf7ee08ad3013a8b5db8
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
import org.apache.dubbo.common.utils.ReflectUtils;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
Expand All @@ -48,6 +51,8 @@ public class ReflectionMethodDescriptor implements MethodDescriptor {
private final Method method;
private final boolean generic;
private final RpcType rpcType;
private Class<?>[] actualRequestTypes;
private Class<?> actualResponseType;

public ReflectionMethodDescriptor(Method method) {
this.method = method;
Expand Down Expand Up @@ -82,13 +87,17 @@ private RpcType determineRpcType() {
if (parameterClasses.length > 2) {
return RpcType.UNARY;
}
Type[] genericParameterTypes = method.getGenericParameterTypes();
if (parameterClasses.length == 1 && isStreamType(parameterClasses[0]) && isStreamType(returnClass)) {
this.actualRequestTypes = new Class<?>[] {
obtainActualTypeInStreamObserver(
((ParameterizedType) method.getGenericReturnType()).getActualTypeArguments()[0])
};
actualResponseType = obtainActualTypeInStreamObserver(
((ParameterizedType) genericParameterTypes[0]).getActualTypeArguments()[0]);
return RpcType.BI_STREAM;
}
if (parameterClasses.length == 2
&& !isStreamType(parameterClasses[0])
&& isStreamType(parameterClasses[1])
&& returnClass.getName().equals(void.class.getName())) {
if (parameterClasses.length >= 1 && hasStreamType(parameterClasses)) {
return RpcType.SERVER_STREAM;
}
if (Arrays.stream(parameterClasses).anyMatch(this::isStreamType) || isStreamType(returnClass)) {
Expand All @@ -99,10 +108,30 @@ && isStreamType(parameterClasses[1])
return RpcType.UNARY;
}

private boolean hasStreamType(Class<?>[] parameterClasses) {
List<Class<?>> parameterClassList = new ArrayList<>();
for (Class<?> parameterClass : parameterClasses) {
if (isStreamType(parameterClass)) {
actualResponseType = parameterClass;
} else {
parameterClassList.add(parameterClass);
}
}
actualRequestTypes = parameterClassList.toArray(new Class<?>[0]);
return actualResponseType != null;
}

private boolean isStreamType(Class<?> classType) {
return StreamObserver.class.isAssignableFrom(classType);
}

private static Class<?> obtainActualTypeInStreamObserver(Type typeInStreamObserver) {
return (Class<?>)
(typeInStreamObserver instanceof ParameterizedType
? ((ParameterizedType) typeInStreamObserver).getRawType()
: typeInStreamObserver);
}

@Override
public String getMethodName() {
return methodName;
Expand Down Expand Up @@ -156,6 +185,14 @@ public Object getAttribute(String key) {
return this.attributeMap.get(key);
}

public Class<?>[] getActualRequestTypes() {
return actualRequestTypes;
}

public Class<?> getActualResponseType() {
return actualResponseType;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public interface GreeterService {
*/
void sayHelloServerStream(HelloRequest request, StreamObserver<HelloReply> responseObserver);

void sayHelloServerStreamNoParameter(StreamObserver<HelloReply> responseObserver);

/**
* Sends greetings with bi streaming
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ public void sayHelloServerStream(HelloRequest request, StreamObserver<HelloReply
responseObserver.onCompleted();
}

@Override
public void sayHelloServerStreamNoParameter(StreamObserver<HelloReply> responseObserver) {
LOGGER.info("Received sayHelloServerStream request");
for (int i = 1; i < 6; i++) {
LOGGER.info("sayHelloServerStream onNext: {} times", i);
responseObserver.onNext(toReply("Hello " + ' ' + i + " times"));
}
LOGGER.info("sayHelloServerStream onCompleted");
responseObserver.onCompleted();
}

@Override
public StreamObserver<HelloRequest> sayHelloBiStream(StreamObserver<HelloReply> responseObserver) {
LOGGER.info("Received sayHelloBiStream request");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
import org.apache.dubbo.rpc.model.ReflectionMethodDescriptor;
import org.apache.dubbo.rpc.model.StubMethodDescriptor;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class MethodMetadata {

private final Class<?>[] actualRequestTypes;
Expand Down Expand Up @@ -64,19 +61,9 @@ private static MethodMetadata doResolveReflection(ReflectionMethodDescriptor met
switch (method.getRpcType()) {
case CLIENT_STREAM:
case BI_STREAM:
actualRequestTypes = new Class<?>[] {
obtainActualTypeInStreamObserver(
((ParameterizedType) method.getMethod().getGenericReturnType()).getActualTypeArguments()[0])
};
actualResponseType = obtainActualTypeInStreamObserver(
((ParameterizedType) method.getMethod().getGenericParameterTypes()[0])
.getActualTypeArguments()[0]);
return new MethodMetadata(actualRequestTypes, actualResponseType);
case SERVER_STREAM:
actualRequestTypes = new Class[] {method.getMethod().getParameterTypes()[0]};
actualResponseType = obtainActualTypeInStreamObserver(
((ParameterizedType) method.getMethod().getGenericParameterTypes()[1])
.getActualTypeArguments()[0]);
actualRequestTypes = method.getActualRequestTypes();
actualResponseType = method.getActualResponseType();
return new MethodMetadata(actualRequestTypes, actualResponseType);
case UNARY:
actualRequestTypes = method.getParameterClasses();
Expand All @@ -85,11 +72,4 @@ private static MethodMetadata doResolveReflection(ReflectionMethodDescriptor met
}
throw new IllegalStateException("Can not reach here");
}

static Class<?> obtainActualTypeInStreamObserver(Type typeInStreamObserver) {
return (Class<?>)
(typeInStreamObserver instanceof ParameterizedType
? ((ParameterizedType) typeInStreamObserver).getRawType()
: typeInStreamObserver);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ public void onReturn(Object value) {}

@Override
public void onMessage(Object message) {
Class<?>[] params = invocation.getParameterTypes();
if (params.length == 1) {
if (params[0].isInstance(responseObserver)) {
invocation.setArguments(new Object[] {responseObserver});
return;
}
}
if (message instanceof Object[]) {
message = ((Object[]) message)[0];
}
Expand Down