-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20579 from mkouba/issue-17428
Dev mode - gRPC: close streams on reload for non-mutiny services
- Loading branch information
Showing
6 changed files
with
237 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
extensions/grpc/runtime/src/main/java/io/quarkus/grpc/runtime/devmode/CollectStreams.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.quarkus.grpc.runtime.devmode; | ||
|
||
import static java.lang.annotation.ElementType.TYPE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import javax.interceptor.InterceptorBinding; | ||
|
||
/** | ||
* | ||
* @see StreamCollectorInterceptor | ||
*/ | ||
@InterceptorBinding | ||
@Target({ TYPE }) | ||
@Retention(RUNTIME) | ||
public @interface CollectStreams { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...rpc/runtime/src/main/java/io/quarkus/grpc/runtime/devmode/StreamCollectorInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package io.quarkus.grpc.runtime.devmode; | ||
|
||
import javax.annotation.Priority; | ||
import javax.interceptor.AroundInvoke; | ||
import javax.interceptor.Interceptor; | ||
import javax.interceptor.InvocationContext; | ||
|
||
import io.grpc.stub.StreamObserver; | ||
import io.quarkus.grpc.runtime.ServerCalls; | ||
import io.quarkus.grpc.runtime.StreamCollector; | ||
|
||
@CollectStreams | ||
@Priority(1) | ||
@Interceptor | ||
public class StreamCollectorInterceptor { | ||
|
||
private final StreamCollector streamCollector; | ||
|
||
public StreamCollectorInterceptor() { | ||
this.streamCollector = ServerCalls.getStreamCollector(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@AroundInvoke | ||
Object collect(InvocationContext context) throws Exception { | ||
// Wraps the first StreamObserver parameter if available | ||
Object[] params = context.getParameters(); | ||
int streamIndex = 0; | ||
StreamObserver<Object> stream = null; | ||
for (int i = 0; i < params.length; i++) { | ||
Object param = params[i]; | ||
if (param == null) { | ||
continue; | ||
} | ||
if (StreamObserver.class.isAssignableFrom(param.getClass())) { | ||
stream = (StreamObserver<Object>) param; | ||
streamIndex = i; | ||
break; | ||
} | ||
} | ||
if (stream == null) { | ||
return context.proceed(); | ||
} | ||
streamCollector.add(stream); | ||
Object[] newParams = new Object[params.length]; | ||
for (int i = 0; i < params.length; i++) { | ||
if (i == streamIndex) { | ||
newParams[i] = new StreamObserverWrapper<>(stream); | ||
} else { | ||
newParams[i] = params[i]; | ||
} | ||
} | ||
context.setParameters(newParams); | ||
return context.proceed(); | ||
} | ||
|
||
private final class StreamObserverWrapper<T> implements StreamObserver<T> { | ||
|
||
private final StreamObserver<T> delegate; | ||
|
||
public StreamObserverWrapper(StreamObserver<T> delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public void onNext(T value) { | ||
delegate.onNext(value); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable t) { | ||
delegate.onError(t); | ||
streamCollector.remove(delegate); | ||
} | ||
|
||
@Override | ||
public void onCompleted() { | ||
delegate.onCompleted(); | ||
streamCollector.remove(delegate); | ||
} | ||
|
||
} | ||
|
||
} |